Passed
Pull Request — master (#64)
by Daniel
06:35
created

CollectionOutputDataTransformer::transform()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
c 1
b 0
f 0
nc 18
nop 3
dl 0
loc 34
ccs 0
cts 20
cp 0
crap 56
rs 8.6346
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\DataTransformer;
15
16
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
17
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
18
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
19
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
20
use ApiPlatform\Core\Util\AttributesExtractor;
21
use ApiPlatform\Core\Util\RequestParser;
22
use Silverback\ApiComponentsBundle\ApiPlatform\CollectionHelper;
23
use Silverback\ApiComponentsBundle\Entity\Component\Collection;
24
use Silverback\ApiComponentsBundle\Exception\OutOfBoundsException;
25
use Symfony\Component\HttpFoundation\RequestStack;
26
27
/**
28
 * @author Daniel West <[email protected]>
29
 */
30
class CollectionOutputDataTransformer implements DataTransformerInterface
31
{
32
    private CollectionHelper $iriConverter;
33
    private CollectionDataProviderInterface $collectionDataProvider;
34
    private RequestStack $requestStack;
35
    private SerializerContextBuilderInterface $serializerContextBuilder;
36
37
    public function __construct(CollectionHelper $iriConverter, CollectionDataProviderInterface $collectionDataProvider, RequestStack $requestStack, SerializerContextBuilderInterface $serializerContextBuilder)
38
    {
39
        $this->iriConverter = $iriConverter;
40
        $this->collectionDataProvider = $collectionDataProvider;
41
        $this->requestStack = $requestStack;
42
        $this->serializerContextBuilder = $serializerContextBuilder;
43
    }
44
45
    public function supportsTransformation($data, string $to, array $context = []): bool
46
    {
47
        return $data instanceof Collection && Collection::class === $to;
48
    }
49
50
    /**
51
     * @param Collection $object
52
     */
53
    public function transform($object, string $to, array $context = [])
54
    {
55
        $parameters = $this->iriConverter->getRouterParametersFromIri($object->getResourceIri());
56
        $attributes = AttributesExtractor::extractAttributes($parameters);
57
        $request = $this->requestStack->getMasterRequest();
58
        if (!$request) {
59
            $filters = null;
60
        } else {
61
            $queryString = RequestParser::getQueryString($request);
62
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
63
        }
64
65
        if ($this->collectionDataProvider instanceof ContextAwareCollectionDataProviderInterface) {
66
            $collectionContext = null === $filters ? [] : ['filters' => $filters];
67
68
            // Comment copied from ApiPlatform\Core\EventListener\ReadListener
69
            // Builtin data providers are able to use the serialization context to automatically add join clauses
70
            $collectionContext += $normalizationContext = $this->serializerContextBuilder->createFromRequest(
71
                $request,
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of ApiPlatform\Core\Seriali...ce::createFromRequest() does only seem to accept Symfony\Component\HttpFoundation\Request, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
                /** @scrutinizer ignore-type */ $request,
Loading history...
72
                true,
73
                $attributes
74
            );
75
            $request->attributes->set('_api_normalization_context', $normalizationContext);
76
77
            $collectionData = $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $collectionContext);
0 ignored issues
show
Unused Code introduced by
The call to ApiPlatform\Core\DataPro...erface::getCollection() has too many arguments starting with $collectionContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            /** @scrutinizer ignore-call */ 
78
            $collectionData = $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $collectionContext);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
        } else {
79
            $collectionData = $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name']);
80
        }
81
        if (!$collectionData instanceof \Traversable) {
82
            throw new OutOfBoundsException('$collectionData should be Traversable');
83
        }
84
        $object->setCollection(iterator_count($collectionData) ? $collectionData : null);
85
86
        return $object;
87
    }
88
}
89