Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateResourceRouteDefinition
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 createFrom
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOperationId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\RestApi\RouteDefinitions;
3
4use Apie\Common\ActionDefinitions\ActionDefinitionInterface;
5use Apie\Common\ActionDefinitions\CreateResourceActionDefinition;
6use Apie\Common\ActionDefinitions\ReplaceResourceActionDefinition;
7use Apie\Common\Actions\CreateObjectAction;
8use Apie\Common\Actions\ReplaceObjectAction;
9use Apie\Core\BoundedContext\BoundedContextId;
10use Apie\Core\Entities\EntityInterface;
11use Apie\Core\Enums\RequestMethod;
12use Apie\Core\ValueObjects\UrlRouteDefinition;
13use ReflectionClass;
14
15/**
16 * Route definition for creating an entity.
17 */
18class CreateResourceRouteDefinition extends AbstractRestApiRouteDefinition
19{
20    public static function createFrom(ActionDefinitionInterface $actionDefinition): ?AbstractRestApiRouteDefinition
21    {
22        if ($actionDefinition instanceof CreateResourceActionDefinition) {
23            return new self($actionDefinition->getResourceName(), $actionDefinition->getBoundedContextId());
24        }
25
26        if ($actionDefinition instanceof ReplaceResourceActionDefinition) {
27            return new self($actionDefinition->getResourceName(), $actionDefinition->getBoundedContextId(), true);
28        }
29
30        return null;
31    }
32
33    /**
34     * @param ReflectionClass<covariant EntityInterface> $className
35     */
36    public function __construct(ReflectionClass $className, BoundedContextId $boundedContextId, private bool $put = false)
37    {
38        parent::__construct($className, $boundedContextId);
39    }
40
41    public function getMethod(): RequestMethod
42    {
43        return /*$this->put ? RequestMethod::PUT : */RequestMethod::POST;
44    }
45
46    public function getOperationId(): string
47    {
48        return ($this->put ? 'put-' : 'post-') . $this->class->getShortName();
49    }
50
51    public function getUrl(): UrlRouteDefinition
52    {
53        return new UrlRouteDefinition($this->class->getShortName());
54    }
55
56    public function getAction(): string
57    {
58        return $this->put ? ReplaceObjectAction::class : CreateObjectAction::class;
59    }
60}