refs #487. Software and softwaare profile Endpoints and firs logic

pull/7/head
Manuel Aranda Rosales 2024-07-02 15:53:23 +02:00
parent 2267a52e06
commit 406f135804
18 changed files with 839 additions and 0 deletions

View File

@ -0,0 +1,31 @@
resources:
App\Entity\Software:
processor: App\State\Processor\SoftwareProcessor
input: App\Dto\Input\SoftwareInput
output: App\Dto\Output\SoftwareOutput
normalizationContext:
groups: ['default', 'software:read']
denormalizationContext:
groups: ['software:write']
operations:
ApiPlatform\Metadata\GetCollection:
provider: App\State\Provider\SoftwareProvider
filters:
- 'api_platform.filter.software.order'
- 'api_platform.filter.software.search'
ApiPlatform\Metadata\Get:
provider: App\State\Provider\SoftwareProvider
ApiPlatform\Metadata\Put:
provider: App\State\Provider\SoftwareProvider
ApiPlatform\Metadata\Patch:
provider: App\State\Provider\SoftwareProvider
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
properties:
App\Entity\Software:
id:
identifier: false
uuid:
identifier: true

View File

@ -0,0 +1,30 @@
resources:
App\Entity\SoftwareProfile:
processor: App\State\Processor\SoftwareProfileProcessor
input: App\Dto\Input\SoftwareProfileInput
output: App\Dto\Output\SoftwareProfileOutput
normalizationContext:
groups: ['default', 'software-profile:read']
denormalizationContext:
groups: ['software-profile:write']
operations:
ApiPlatform\Metadata\GetCollection:
provider: App\State\Provider\SoftwareProfileProvider
filters:
- 'api_platform.filter.software.order'
- 'api_platform.filter.software.search'
ApiPlatform\Metadata\Get:
provider: App\State\Provider\SoftwareProfileProvider
ApiPlatform\Metadata\Put:
provider: App\State\Provider\SoftwareProfileProvider
ApiPlatform\Metadata\Patch:
provider: App\State\Provider\SoftwareProfileProvider
ApiPlatform\Metadata\Post: ~
ApiPlatform\Metadata\Delete: ~
properties:
App\Entity\SoftwareProfile:
id:
identifier: false
uuid:
identifier: true

View File

@ -69,6 +69,16 @@ services:
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'
App\State\Provider\HardwareTypeProvider:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'
App\State\Provider\SoftwareProvider:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'
App\State\Provider\SoftwareProfileProvider:
bind:
$collectionProvider: '@api_platform.doctrine.orm.state.collection_provider'
$itemProvider: '@api_platform.doctrine.orm.state.item_provider'

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240702132742 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE software (id INT AUTO_INCREMENT NOT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', migration_id VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, created_by VARCHAR(255) DEFAULT NULL, updated_by VARCHAR(255) DEFAULT NULL, description VARCHAR(255) DEFAULT NULL, name VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_77D068CFD17F50A6 (uuid), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE software_profile (id INT AUTO_INCREMENT NOT NULL, organizational_unit_id INT NOT NULL, uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', migration_id VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, created_by VARCHAR(255) DEFAULT NULL, updated_by VARCHAR(255) DEFAULT NULL, description VARCHAR(255) DEFAULT NULL, comments VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_B70C3C9BD17F50A6 (uuid), INDEX IDX_B70C3C9BFB84408A (organizational_unit_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE software_profile_software (software_profile_id INT NOT NULL, software_id INT NOT NULL, INDEX IDX_3DDFEC7ED42A742 (software_profile_id), INDEX IDX_3DDFEC7D7452741 (software_id), PRIMARY KEY(software_profile_id, software_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE software_profile ADD CONSTRAINT FK_B70C3C9BFB84408A FOREIGN KEY (organizational_unit_id) REFERENCES organizational_unit (id)');
$this->addSql('ALTER TABLE software_profile_software ADD CONSTRAINT FK_3DDFEC7ED42A742 FOREIGN KEY (software_profile_id) REFERENCES software_profile (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE software_profile_software ADD CONSTRAINT FK_3DDFEC7D7452741 FOREIGN KEY (software_id) REFERENCES software (id) ON DELETE CASCADE');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE software_profile DROP FOREIGN KEY FK_B70C3C9BFB84408A');
$this->addSql('ALTER TABLE software_profile_software DROP FOREIGN KEY FK_3DDFEC7ED42A742');
$this->addSql('ALTER TABLE software_profile_software DROP FOREIGN KEY FK_3DDFEC7D7452741');
$this->addSql('DROP TABLE software');
$this->addSql('DROP TABLE software_profile');
$this->addSql('DROP TABLE software_profile_software');
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Entity\Software;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
final class SoftwareInput
{
#[Assert\NotBlank]
#[Groups(['software:write'])]
#[ApiProperty(description: 'The name of the software', example: "Software 1")]
public ?string $name = null;
#[Groups(['software:write'])]
#[ApiProperty(description: 'The description of the software', example: "Software 1 description")]
public ?string $description = null;
public function __construct(?Software $software = null)
{
if (!$software) {
return;
}
$this->name = $software->getName();
$this->description = $software->getDescription();
}
public function createOrUpdateEntity(?Software $software = null): Software
{
if (!$software) {
$software = new Software();
}
$software->setName($this->name);
$software->setDescription($this->description);
return $software;
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace App\Dto\Input;
use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\SoftwareProfile;
use Symfony\Component\Serializer\Annotation\Groups;
final class SoftwareProfileInput
{
#[Groups(['software-profile:write'])]
#[ApiProperty(description: 'The description of the software profile', example: "Software 1 description")]
public ?string $description = null;
#[Groups(['software-profile:write'])]
#[ApiProperty(description: 'Comments of the software profile', example: "Software 1")]
public ?string $comments = null;
#[Groups(['software-profile:write'])]
#[ApiProperty(description: 'The organizational unit of the software profile')]
public ?OrganizationalUnitOutput $organizationalUnit = null;
public function __construct(?SoftwareProfile $softwareProfile = null)
{
if (!$softwareProfile) {
return;
}
$this->description = $softwareProfile->getDescription();
$this->comments = $softwareProfile->getComments();
if($softwareProfile->getOrganizationalUnit()) {
$this->organizationalUnit = new OrganizationalUnitOutput($softwareProfile->getOrganizationalUnit());
}
}
public function createOrUpdateEntity(?SoftwareProfile $softwareProfile = null): SoftwareProfile
{
if (!$softwareProfile) {
$softwareProfile = new SoftwareProfile();
}
$softwareProfile->setDescription($this->description);
$softwareProfile->setComments($this->comments);
if ($this->organizationalUnit) {
$softwareProfile->setOrganizationalUnit($this->organizationalUnit->getEntity());
}
return $softwareProfile;
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Dto\Output;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\Software;
use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'Software')]
final class SoftwareOutput extends AbstractOutput
{
#[Groups(['software:read', 'software-profile:read'])]
public string $name;
#[Groups(['software:read'])]
public ?string $description = '';
#[Groups(['software:read'])]
public \DateTime $createdAt;
#[Groups(['software:read'])]
public ?string $createdBy = null;
public function __construct(Software $software)
{
parent::__construct($software);
$this->name = $software->getName();
$this->description = $software->getDescription();
$this->createdAt = $software->getCreatedAt();
$this->createdBy = $software->getCreatedBy();
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Dto\Output;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\Software;
use App\Entity\SoftwareProfile;
use Symfony\Component\Serializer\Annotation\Groups;
#[Get(shortName: 'SoftwareProfile')]
final class SoftwareProfileOutput extends AbstractOutput
{
#[Groups(['software-profile:read'])]
public ?string $description = '';
#[Groups(['software-profile:read'])]
public ?string $comments = '';
#[Groups(['software-profile:read'])]
public ?OrganizationalUnitOutput $organizationalUnit = null;
#[Groups(['software-profile:read'])]
public ?array $softwareCollection = [];
#[Groups(['software-profile:read'])]
public \DateTime $createdAt;
#[Groups(['software-profile:read'])]
public ?string $createdBy = null;
public function __construct(SoftwareProfile $softwareProfile)
{
parent::__construct($softwareProfile);
$this->description = $softwareProfile->getDescription();
$this->comments = $softwareProfile->getComments();
if($softwareProfile->getOrganizationalUnit()) {
$this->organizationalUnit = new OrganizationalUnitOutput($softwareProfile->getOrganizationalUnit());
}
$this->softwareCollection = $softwareProfile->getSoftwareCollection()->map(
fn(Software $hardware) => new SoftwareOutput($hardware)
)->toArray();
$this->createdAt = $softwareProfile->getCreatedAt();
$this->createdBy = $softwareProfile->getCreatedBy();
}
}

View File

@ -78,12 +78,19 @@ class OrganizationalUnit extends AbstractEntity
#[ORM\Column(nullable: true)]
private ?int $capacity = null;
/**
* @var Collection<int, SoftwareProfile>
*/
#[ORM\OneToMany(mappedBy: 'organizationalUnit', targetEntity: SoftwareProfile::class)]
private Collection $softwareProfiles;
public function __construct()
{
parent::__construct();
$this->organizationalUnits = new ArrayCollection();
$this->users = new ArrayCollection();
$this->clients = new ArrayCollection();
$this->softwareProfiles = new ArrayCollection();
}
public function getDescription(): ?string
@ -331,4 +338,34 @@ class OrganizationalUnit extends AbstractEntity
$childUnit->updateNetworkSettingsRecursively($networkSettings);
}
}
/**
* @return Collection<int, SoftwareProfile>
*/
public function getSoftwareProfiles(): Collection
{
return $this->softwareProfiles;
}
public function addSoftwareProfile(SoftwareProfile $softwareProfile): static
{
if (!$this->softwareProfiles->contains($softwareProfile)) {
$this->softwareProfiles->add($softwareProfile);
$softwareProfile->setOrganizationalUnit($this);
}
return $this;
}
public function removeSoftwareProfile(SoftwareProfile $softwareProfile): static
{
if ($this->softwareProfiles->removeElement($softwareProfile)) {
// set the owning side to null (unless already changed)
if ($softwareProfile->getOrganizationalUnit() === $this) {
$softwareProfile->setOrganizationalUnit(null);
}
}
return $this;
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\Entity;
use App\Repository\SoftwareRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SoftwareRepository::class)]
class Software extends AbstractEntity
{
use NameableTrait;
#[ORM\Column(length: 255, nullable: true)]
private ?string $description = null;
/**
* @var Collection<int, SoftwareProfile>
*/
#[ORM\ManyToMany(targetEntity: SoftwareProfile::class, mappedBy: 'softwareCollection')]
private Collection $softwareProfiles;
public function __construct()
{
parent::__construct();
$this->softwareProfiles = new ArrayCollection();
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
/**
* @return Collection<int, SoftwareProfile>
*/
public function getSoftwareProfiles(): Collection
{
return $this->softwareProfiles;
}
public function addSoftwareProfile(SoftwareProfile $softwareProfile): static
{
if (!$this->softwareProfiles->contains($softwareProfile)) {
$this->softwareProfiles->add($softwareProfile);
$softwareProfile->addSoftwareCollection($this);
}
return $this;
}
public function removeSoftwareProfile(SoftwareProfile $softwareProfile): static
{
if ($this->softwareProfiles->removeElement($softwareProfile)) {
$softwareProfile->removeSoftwareCollection($this);
}
return $this;
}
}

View File

@ -0,0 +1,100 @@
<?php
namespace App\Entity;
use App\Repository\SoftwareProfileRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SoftwareProfileRepository::class)]
class SoftwareProfile extends AbstractEntity
{
#[ORM\Column(length: 255, nullable: true)]
private ?string $description = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $comments = null;
#[ORM\ManyToOne(inversedBy: 'softwareProfiles')]
#[ORM\JoinColumn(nullable: false)]
private ?OrganizationalUnit $organizationalUnit = null;
/**
* @var Collection<int, Software>
*/
#[ORM\ManyToMany(targetEntity: Software::class, inversedBy: 'softwareProfiles')]
private Collection $softwareCollection;
public function __construct()
{
parent::__construct();
$this->softwareCollection = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getComments(): ?string
{
return $this->comments;
}
public function setComments(?string $comments): static
{
$this->comments = $comments;
return $this;
}
public function getOrganizationalUnit(): ?OrganizationalUnit
{
return $this->organizationalUnit;
}
public function setOrganizationalUnit(?OrganizationalUnit $organizationalUnit): static
{
$this->organizationalUnit = $organizationalUnit;
return $this;
}
/**
* @return Collection<int, Software>
*/
public function getSoftwareCollection(): Collection
{
return $this->softwareCollection;
}
public function addSoftwareCollection(Software $softwareCollection): static
{
if (!$this->softwareCollection->contains($softwareCollection)) {
$this->softwareCollection->add($softwareCollection);
}
return $this;
}
public function removeSoftwareCollection(Software $softwareCollection): static
{
$this->softwareCollection->removeElement($softwareCollection);
return $this;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Model;
final class SoftwareTypes
{
public const string OPERATIVE_SYSTEM = 'operative-system';
public const string FILE = 'file';
public const string APPLICATION = 'application';
private const array SOFTWARE_TYPES = [
self::OPERATIVE_SYSTEM => 'Sistema Operativo',
self::FILE => 'Fichero',
self::APPLICATION => 'Aplicación',
];
public static function getSoftwareTypes(): array
{
return self::SOFTWARE_TYPES;
}
public static function getSoftwareType(string $software): ?string
{
return self::SOFTWARE_TYPES[$software] ?? null;
}
public static function getSoftwareTypeKeys(): array
{
return array_keys(self::SOFTWARE_TYPES);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Repository;
use App\Entity\SoftwareProfile;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<SoftwareProfile>
*/
class SoftwareProfileRepository extends AbstractRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, SoftwareProfile::class);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Repository;
use App\Entity\Software;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Software>
*/
class SoftwareRepository extends AbstractRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Software::class);
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\State\Processor;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\SoftwareInput;
use App\Dto\Output\SoftwareOutput;
use App\Repository\SoftwareRepository;
readonly class SoftwareProcessor implements ProcessorInterface
{
public function __construct(
private SoftwareRepository $softwareRepository,
private ValidatorInterface $validator
)
{
}
/**
* @throws \Exception
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): SoftwareOutput
{
switch ($operation){
case $operation instanceof Post:
case $operation instanceof Put:
case $operation instanceof Patch:
return $this->processCreateOrUpdate($data, $operation, $uriVariables, $context);
case $operation instanceof Delete:
return $this->processDelete($data, $operation, $uriVariables, $context);
}
}
/**
* @throws \Exception
*/
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): SoftwareOutput
{
if (!($data instanceof SoftwareInput)) {
throw new \Exception(sprintf('data is not instance of %s', SoftwareInput::class));
}
$entity = null;
if (isset($uriVariables['uuid'])) {
$entity = $this->softwareRepository->findOneByUuid($uriVariables['uuid']);
}
$software = $data->createOrUpdateEntity($entity);
$this->validator->validate($software);
$this->softwareRepository->save($software);
return new SoftwareOutput($software);
}
private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
{
$user = $this->softwareRepository->findOneByUuid($uriVariables['uuid']);
$this->softwareRepository->delete($user);
return null;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\State\Processor;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\SoftwareProfileInput;
use App\Dto\Output\SoftwareProfileOutput;
use App\Repository\SoftwareProfileRepository;
readonly class SoftwareProfileProcessor implements ProcessorInterface
{
public function __construct(
private SoftwareProfileRepository $softwareProfileRepository,
private ValidatorInterface $validator
)
{
}
/**
* @throws \Exception
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): SoftwareProfileOutput
{
switch ($operation){
case $operation instanceof Post:
case $operation instanceof Put:
case $operation instanceof Patch:
return $this->processCreateOrUpdate($data, $operation, $uriVariables, $context);
case $operation instanceof Delete:
return $this->processDelete($data, $operation, $uriVariables, $context);
}
}
/**
* @throws \Exception
*/
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): SoftwareProfileOutput
{
if (!($data instanceof SoftwareProfileInput)) {
throw new \Exception(sprintf('data is not instance of %s', SoftwareProfileInput::class));
}
$entity = null;
if (isset($uriVariables['uuid'])) {
$entity = $this->softwareProfileRepository->findOneByUuid($uriVariables['uuid']);
}
$softwareProfile = $data->createOrUpdateEntity($entity);
$this->validator->validate($softwareProfile);
$this->softwareProfileRepository->save($softwareProfile);
return new SoftwareProfileOutput($softwareProfile);
}
private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
{
$user = $this->softwareProfileRepository->findOneByUuid($uriVariables['uuid']);
$this->softwareProfileRepository->delete($user);
return null;
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace App\State\Provider;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\Pagination\TraversablePaginator;
use ApiPlatform\State\ProviderInterface;
use App\Dto\Input\SoftwareProfileInput;
use App\Dto\Output\SoftwareProfileOutput;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
readonly class SoftwareProfileProvider implements ProviderInterface
{
public function __construct(
private ProviderInterface $collectionProvider,
private ProviderInterface $itemProvider
)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
switch ($operation){
case $operation instanceof GetCollection:
return $this->provideCollection($operation, $uriVariables, $context);
case $operation instanceof Patch:
case $operation instanceof Put:
return $this->provideInput($operation, $uriVariables, $context);
case $operation instanceof Get:
return $this->provideItem($operation, $uriVariables, $context);
}
}
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
{
$paginator = $this->collectionProvider->provide($operation, $uriVariables, $context);
$items = new \ArrayObject();
foreach ($paginator->getIterator() as $item){
$items[] = new SoftwareProfileInput($item);
}
return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems());
}
public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
if (!$item) {
throw new NotFoundHttpException('Software profile not found');
}
return new SoftwareProfileOutput($item);
}
public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (isset($uriVariables['uuid'])) {
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
return $item !== null ? new SoftwareProfileInput($item) : null;
}
return new SoftwareProfileInput();
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace App\State\Provider;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Put;
use ApiPlatform\State\Pagination\TraversablePaginator;
use ApiPlatform\State\ProviderInterface;
use App\Dto\Input\SoftwareInput;
use App\Dto\Output\SoftwareOutput;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
readonly class SoftwareProvider implements ProviderInterface
{
public function __construct(
private ProviderInterface $collectionProvider,
private ProviderInterface $itemProvider
)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
switch ($operation){
case $operation instanceof GetCollection:
return $this->provideCollection($operation, $uriVariables, $context);
case $operation instanceof Patch:
case $operation instanceof Put:
return $this->provideInput($operation, $uriVariables, $context);
case $operation instanceof Get:
return $this->provideItem($operation, $uriVariables, $context);
}
}
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
{
$paginator = $this->collectionProvider->provide($operation, $uriVariables, $context);
$items = new \ArrayObject();
foreach ($paginator->getIterator() as $item){
$items[] = new SoftwareOutput($item);
}
return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems());
}
public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
if (!$item) {
throw new NotFoundHttpException('Software not found');
}
return new SoftwareOutput($item);
}
public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (isset($uriVariables['uuid'])) {
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
return $item !== null ? new SoftwareInput($item) : null;
}
return new SoftwareInput();
}
}