Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SearchObjectTypeResolver | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Graphql\TypeResolvers; |
| 3 | |
| 4 | use Apie\Common\Actions\GetListAction; |
| 5 | use Apie\Common\Interfaces\ApieFacadeInterface; |
| 6 | use Apie\Core\Actions\ActionResponse; |
| 7 | use Apie\Core\BoundedContext\BoundedContextId; |
| 8 | use Apie\Core\Context\ApieContext; |
| 9 | use Apie\Core\ContextConstants; |
| 10 | use Apie\Core\Datalayers\ApieDatalayer; |
| 11 | use Apie\Core\Datalayers\Search\QuerySearch; |
| 12 | use Apie\Serializer\Serializer; |
| 13 | |
| 14 | class SearchObjectTypeResolver |
| 15 | { |
| 16 | public function __construct(private readonly string $resourceName) |
| 17 | { |
| 18 | } |
| 19 | public function __invoke(ApieContext $context, array $args): array |
| 20 | { |
| 21 | $apieDatalayer = $context->getContext(ApieDatalayer::class); |
| 22 | assert($apieDatalayer instanceof ApieDatalayer); |
| 23 | $serializer = $context->getContext(Serializer::class); |
| 24 | assert($serializer instanceof Serializer); |
| 25 | $resourceName = new \ReflectionClass($this->resourceName); |
| 26 | $boundedContextId = new BoundedContextId($context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)); |
| 27 | |
| 28 | $context = $context->withContext(ContextConstants::APIE_ACTION, GetListAction::class) |
| 29 | ->withMultipleContext(GetListAction::getRouteAttributes($resourceName)); |
| 30 | $context->checkAuthorization(); |
| 31 | $list = $apieDatalayer->all($resourceName, $boundedContextId) |
| 32 | ->toPaginatedResult(QuerySearch::fromCamelCaseArray( |
| 33 | $args['filter'] ?? [], |
| 34 | $context |
| 35 | )); |
| 36 | $actionResponse = ActionResponse::createRunSuccess( |
| 37 | $context->getContext(ApieFacadeInterface::class), |
| 38 | $context, |
| 39 | $list, |
| 40 | $list |
| 41 | ); |
| 42 | return $serializer->normalize($list, $context->withContext(ActionResponse::class, $actionResponse)) |
| 43 | ->toArray(); |
| 44 | } |
| 45 | } |