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