Passed
Pull Request — 2.4 (#2714)
by Maxime
03:15
created

IriConverter::getIriFromItemWithResource()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 5
nop 3
dl 0
loc 18
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[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 ApiPlatform\Core\Bridge\Symfony\Routing;
15
16
use ApiPlatform\Core\Api\IdentifiersExtractor;
17
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
18
use ApiPlatform\Core\Api\OperationType;
19
use ApiPlatform\Core\Api\ResourceIriConverterInterface;
20
use ApiPlatform\Core\Api\UrlGeneratorInterface;
21
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
22
use ApiPlatform\Core\DataProvider\OperationDataProviderTrait;
23
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
24
use ApiPlatform\Core\Exception\InvalidArgumentException;
25
use ApiPlatform\Core\Exception\InvalidIdentifierException;
26
use ApiPlatform\Core\Exception\ItemNotFoundException;
27
use ApiPlatform\Core\Exception\RuntimeException;
28
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
29
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
30
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
31
use ApiPlatform\Core\Util\AttributesExtractor;
32
use ApiPlatform\Core\Util\ClassInfoTrait;
33
use Symfony\Component\PropertyAccess\PropertyAccess;
34
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
35
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
36
use Symfony\Component\Routing\RouterInterface;
37
38
/**
39
 * {@inheritdoc}
40
 *
41
 * @author Kévin Dunglas <[email protected]>
42
 */
43
final class IriConverter implements ResourceIriConverterInterface
44
{
45
    use ClassInfoTrait;
46
    use OperationDataProviderTrait;
47
48
    private $routeNameResolver;
49
    private $router;
50
    private $identifiersExtractor;
51
52
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null, IdentifiersExtractorInterface $identifiersExtractor = null, SubresourceDataProviderInterface $subresourceDataProvider = null, IdentifierConverterInterface $identifierConverter = null)
53
    {
54
        $this->itemDataProvider = $itemDataProvider;
55
        $this->routeNameResolver = $routeNameResolver;
56
        $this->router = $router;
57
        $this->identifiersExtractor = $identifiersExtractor;
58
        $this->subresourceDataProvider = $subresourceDataProvider;
59
        $this->identifierConverter = $identifierConverter;
60
61
        if (null === $identifiersExtractor) {
62
            @trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3', IdentifiersExtractorInterface::class), E_USER_DEPRECATED);
63
            $this->identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactory, $propertyMetadataFactory, $propertyAccessor ?? PropertyAccess::createPropertyAccessor());
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getItemFromIri(string $iri, array $context = [])
71
    {
72
        try {
73
            $parameters = $this->router->match($iri);
74
        } catch (RoutingExceptionInterface $e) {
75
            throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e);
76
        }
77
78
        if (!isset($parameters['_api_resource_class'])) {
79
            throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
80
        }
81
82
        if (isset($parameters['_api_collection_operation_name'])) {
83
            throw new InvalidArgumentException(sprintf('The iri "%s" references a collection not an item.', $iri));
84
        }
85
86
        $attributes = AttributesExtractor::extractAttributes($parameters);
87
88
        try {
89
            $identifiers = $this->extractIdentifiers($parameters, $attributes);
90
        } catch (InvalidIdentifierException $e) {
91
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
92
        }
93
94
        if ($this->identifierConverter) {
95
            $context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] = true;
96
        }
97
98
        if (isset($attributes['subresource_operation_name'])) {
99
            if (($item = $this->getSubresourceData($identifiers, $attributes, $context)) && !\is_array($item)) {
100
                return $item;
101
            }
102
103
            throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
104
        }
105
106
        if ($item = $this->getItemData($identifiers, $attributes, $context)) {
107
            return $item;
108
        }
109
110
        throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     *
116
     * @deprecated use `getIriFromItemWithResource` instead to be removed in ApiPlatform 3.0
117
     */
118
    public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
119
    {
120
        $resourceClass = $this->getObjectClass($item);
121
122
        return $this->getIriFromItemWithResource($item, $resourceClass, $referenceType);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getIriFromItemWithResource($item, string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
129
    {
130
        $routeName = $this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM);
131
132
        try {
133
            $identifiers = $this->generateIdentifiersUrl($this->identifiersExtractor->getIdentifiersFromItem($item), $resourceClass);
0 ignored issues
show
Bug introduced by
The method getIdentifiersFromItem() does not exist on null. ( Ignorable by Annotation )

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

133
            $identifiers = $this->generateIdentifiersUrl($this->identifiersExtractor->/** @scrutinizer ignore-call */ getIdentifiersFromItem($item), $resourceClass);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
135
            return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType);
136
        } catch (RuntimeException $e) {
137
            throw new InvalidArgumentException(sprintf(
138
                'Unable to generate an IRI for the item of type "%s"',
139
                $resourceClass
140
            ), $e->getCode(), $e);
141
        } catch (RoutingExceptionInterface $e) {
142
            throw new InvalidArgumentException(sprintf(
143
                'Unable to generate an IRI for the item of type "%s"',
144
                $resourceClass
145
            ), $e->getCode(), $e);
146
        }
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
153
    {
154
        try {
155
            return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::COLLECTION), [], $referenceType);
156
        } catch (RoutingExceptionInterface $e) {
157
            throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e);
158
        }
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function getItemIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
165
    {
166
        try {
167
            return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM), $identifiers, $referenceType);
168
        } catch (RoutingExceptionInterface $e) {
169
            throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e);
170
        }
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function getSubresourceIriFromResourceClass(string $resourceClass, array $context, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
177
    {
178
        try {
179
            return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::SUBRESOURCE, $context), $context['subresource_identifiers'], $referenceType);
180
        } catch (RoutingExceptionInterface $e) {
181
            throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e);
182
        }
183
    }
184
185
    /**
186
     * Generate the identifier url.
187
     *
188
     * @throws InvalidArgumentException
189
     *
190
     * @return string[]
191
     */
192
    private function generateIdentifiersUrl(array $identifiers, string $resourceClass): array
193
    {
194
        if (0 === \count($identifiers)) {
195
            throw new InvalidArgumentException(sprintf(
196
                'No identifiers defined for resource of type "%s"',
197
                $resourceClass
198
            ));
199
        }
200
201
        if (1 === \count($identifiers)) {
202
            return [rawurlencode((string) reset($identifiers))];
203
        }
204
205
        foreach ($identifiers as $name => $value) {
206
            $identifiers[$name] = sprintf('%s=%s', $name, $value);
207
        }
208
209
        return array_values($identifiers);
210
    }
211
}
212