Passed
Pull Request — master (#64)
by Daniel
05:48
created

CollectionOutputDataTransformer::getDataProviderContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 3
dl 0
loc 19
ccs 0
cts 14
cp 0
crap 6
rs 9.8333
c 0
b 0
f 0
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 Symfony\Component\HttpFoundation\RequestStack;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 */
29
class CollectionOutputDataTransformer implements DataTransformerInterface
30
{
31
    private CollectionHelper $iriConverter;
32
    private CollectionDataProviderInterface $collectionDataProvider;
33
    private RequestStack $requestStack;
34
    private SerializerContextBuilderInterface $serializerContextBuilder;
35
36
    public function __construct(CollectionHelper $iriConverter, CollectionDataProviderInterface $collectionDataProvider, RequestStack $requestStack, SerializerContextBuilderInterface $serializerContextBuilder)
37
    {
38
        $this->iriConverter = $iriConverter;
39
        $this->collectionDataProvider = $collectionDataProvider;
40
        $this->requestStack = $requestStack;
41
        $this->serializerContextBuilder = $serializerContextBuilder;
42
    }
43
44
    public function supportsTransformation($data, string $to, array $context = []): bool
45
    {
46
        return $data instanceof Collection && Collection::class === $to;
47
    }
48
49
    /**
50
     * @param Collection $object
51
     */
52
    public function transform($object, string $to, array $context = [])
53
    {
54
        $parameters = $this->iriConverter->getRouterParametersFromIri($object->getResourceIri());
55
        $attributes = AttributesExtractor::extractAttributes($parameters);
56
        $request = $this->requestStack->getMasterRequest();
57
        if (!$request) {
58
            $filters = null;
59
        } else {
60
            $queryString = RequestParser::getQueryString($request);
61
            $filters = $queryString ? RequestParser::parseRequestParams($queryString) : null;
62
        }
63
64
        if ($this->collectionDataProvider instanceof ContextAwareCollectionDataProviderInterface) {
65
            $collectionContext = null === $filters ? [] : ['filters' => $filters];
66
67
            // Comment copied from ApiPlatform\Core\EventListener\ReadListener
68
            // Builtin data providers are able to use the serialization context to automatically add join clauses
69
            $collectionContext += $normalizationContext = $this->serializerContextBuilder->createFromRequest(
70
                $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

70
                /** @scrutinizer ignore-type */ $request,
Loading history...
71
                true,
72
                $attributes
73
            );
74
            $request->attributes->set('_api_normalization_context', $normalizationContext);
75
76
            $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

76
            /** @scrutinizer ignore-call */ 
77
            $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...
77
        } else {
78
            $collectionData = $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name']);
79
        }
80
        $object->setCollection($collectionData);
81
82
        return $object;
83
    }
84
}
85