Add embedded video data to project resource

This commit is contained in:
2025-02-06 14:50:49 +01:00
parent a06dc9dfd8
commit 41953e2a95
4 changed files with 75 additions and 0 deletions
@@ -9,10 +9,13 @@ use App\ApiResource\Accounting\AccountingApiResource;
use App\ApiResource\User\UserApiResource;
use App\Entity\Project\Project;
use App\Entity\Project\ProjectStatus;
use App\Entity\Project\ProjectVideo;
use App\Mapping\Transformer\BudgetMapTransformer;
use App\Mapping\Transformer\ProjectVideoMapTransformer;
use App\State\ApiResourceStateProvider;
use App\State\Project\ProjectStateProcessor;
use AutoMapper\Attribute\MapFrom;
use AutoMapper\Attribute\MapTo;
use Symfony\Component\Validator\Constraints as Assert;
/**
@@ -82,6 +85,21 @@ class ProjectApiResource
#[Assert\NotBlank()]
public string $description;
/**
* A URL to a video showcasing the Project.
*/
#[Assert\Url()]
#[API\ApiProperty(readable: false)]
#[MapTo(target: Project::class, transformer: ProjectVideoMapTransformer::class)]
public string $video;
/**
* Extracted embedding data from the Project's video.
*/
#[API\ApiProperty(writable: false)]
#[MapFrom(source: Project::class, property: 'video')]
public ProjectVideo $videoEmbed;
/**
* The status of a Project represents how far it is in it's life-cycle.
*/
+18
View File
@@ -62,6 +62,12 @@ class Project implements UserOwnedInterface, AccountingOwnerInterface, Localized
#[ORM\Column(type: Types::TEXT)]
private ?string $description = null;
/**
* A video showcasing the Project.
*/
#[ORM\Embedded(class: ProjectVideo::class)]
private ?ProjectVideo $video = null;
/**
* Since Projects can be recipients of funding, they are assigned an Accounting when created.
* A Project's Accounting represents how much money the Project has raised from the community.
@@ -162,6 +168,18 @@ class Project implements UserOwnedInterface, AccountingOwnerInterface, Localized
return $this;
}
public function getVideo(): ?ProjectVideo
{
return $this->video;
}
public function setVideo(?ProjectVideo $video): static
{
$this->video = $video;
return $this;
}
public function getAccounting(): ?Accounting
{
return $this->accounting;
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Entity\Project;
use App\Service\Embed\EmbedVideo;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Embeddable]
class ProjectVideo extends EmbedVideo
{
public function __construct(
#[ORM\Column(type: Types::STRING, nullable: true)]
public readonly string $src = '',
#[ORM\Column(type: Types::STRING, nullable: true)]
public readonly string $thumbnail = '',
) {}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Mapping\Transformer;
use App\Entity\Project\ProjectVideo;
use App\Service\Embed\EmbedService;
use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerInterface;
class ProjectVideoMapTransformer implements PropertyTransformerInterface
{
public function __construct(
private EmbedService $embedService,
) {}
public function transform(mixed $value, object|array $source, array $context): mixed
{
$video = $this->embedService->getVideo($value);
return new ProjectVideo($video->src, $video->thumbnail);
}
}