Преглед на файлове

create the CreateOrganization services chain

Olivier Massot преди 1 година
родител
ревизия
d226392882

+ 29 - 0
src/ApiResources/Organization/OrganizationCreationRequest.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\ApiResources\Organization;
+
+use ApiPlatform\Metadata\ApiProperty;
+use ApiPlatform\Metadata\ApiResource;
+use ApiPlatform\Metadata\Post;
+use App\State\Processor\Export\LicenceCmf\ExportRequestProcessor;
+
+#[ApiResource(
+    operations: [
+        new Post(
+            uriTemplate: '/internal/organization/create',
+            security: 'false'
+        ),
+    ],
+    processor: OrganizationCreationRequestProcessor::class
+)]
+class OrganizationCreationRequest
+{
+    /**
+     * Id 'bidon' ajouté par défaut pour permettre la construction
+     * de l'IRI par api platform.
+     */
+    #[ApiProperty(identifier: true)]
+    protected int $id = 0;
+
+
+}

+ 28 - 0
src/Message/Command/OrganizationCreationCommand.php

@@ -0,0 +1,28 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Message\Command;
+
+use App\ApiResources\Organization\OrganizationCreationRequest;
+
+/**
+ * Transmission d'une requête de création d'organisation au service dédié.
+ */
+class OrganizationCreationCommand
+{
+    public function __construct(
+        private OrganizationCreationRequest $organizationCreationRequest
+    ) {
+    }
+
+    public function getOrganizationCreationRequest(): OrganizationCreationRequest
+    {
+        return $this->organizationCreationRequest;
+    }
+
+    public function setOrganizationCreationRequest(OrganizationCreationRequest $organizationCreationRequest): void
+    {
+        $this->organizationCreationRequest = $organizationCreationRequest;
+    }
+}

+ 26 - 0
src/Message/Handler/OrganizationCreationHandler.php

@@ -0,0 +1,26 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Message\Handler;
+
+use App\Message\Command\OrganizationCreationCommand;
+use App\Service\Organization\OrganizationFactory;
+use Symfony\Component\Messenger\Attribute\AsMessageHandler;
+
+#[AsMessageHandler(priority: 1)]
+class OrganizationCreationHandler
+{
+    public function __construct(
+        private readonly OrganizationFactory $organizationFactory
+    ) {}
+
+    public function __invoke(OrganizationCreationCommand $organizationCreationCommand): void
+    {
+        $organizationCreationRequest = $organizationCreationCommand->getOrganizationCreationRequest();
+
+        $organization = $this->organizationFactory->create($organizationCreationRequest);
+
+        // TODO: send confirmation email
+    }
+}

+ 14 - 0
src/Service/Organization/OrganizationFactory.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Service\Organization;
+
+use App\ApiResources\Organization\OrganizationCreationRequest;
+use App\Entity\Organization\Organization;
+
+class OrganizationFactory
+{
+    public function create(OrganizationCreationRequest $organizationCreationRequest)
+    {
+
+    }
+}

+ 42 - 0
src/State/Processor/Organization/OrganizationCreationRequestProcessor.php

@@ -0,0 +1,42 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\State\Processor\Organization;
+
+use ApiPlatform\Metadata\Delete;
+use ApiPlatform\Metadata\Operation;
+use ApiPlatform\State\ProcessorInterface;
+use App\ApiResources\Organization\OrganizationCreationRequest;
+use App\Message\Command\OrganizationCreationCommand;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Messenger\MessageBusInterface;
+
+class OrganizationCreationRequestProcessor implements ProcessorInterface
+{
+    public function __construct(
+        private MessageBusInterface $messageBus,
+    ) {
+    }
+
+    /**
+     * @param OrganizationCreationRequest $organizationCreationRequest
+     * @param mixed[]       $uriVariables
+     * @param mixed[]       $context
+     *
+     * @throws \Exception
+     */
+    public function process(mixed $organizationCreationRequest, Operation $operation, array $uriVariables = [], array $context = []): OrganizationCreationRequest
+    {
+        if ($operation instanceof Delete) {
+            throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
+        }
+
+        // Send the export request to Messenger (@see App\Message\Handler\OrganizationCreationHandler)
+        $this->messageBus->dispatch(
+            new OrganizationCreationCommand($organizationCreationRequest)
+        );
+
+        return $organizationCreationRequest;
+    }
+}