Passed
Pull Request — master (#3084)
by
unknown
04:28
created

IriConverter   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 76
dl 0
loc 169
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubresourceIriFromResourceClass() 0 6 2
A getIriFromResourceClass() 0 6 2
A __construct() 0 14 2
A getItemIriFromResourceClass() 0 13 2
A getIriFromItem() 0 14 2
B getItemFromIri() 0 41 10
A generate() 0 3 2
A generateIdentifiersUrl() 0 18 4
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\IriConverterInterface;
19
use ApiPlatform\Core\Api\OperationType;
20
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
21
use ApiPlatform\Core\Api\UrlGeneratorInterface;
22
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
23
use ApiPlatform\Core\DataProvider\OperationDataProviderTrait;
24
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
25
use ApiPlatform\Core\Exception\InvalidArgumentException;
26
use ApiPlatform\Core\Exception\InvalidIdentifierException;
27
use ApiPlatform\Core\Exception\ItemNotFoundException;
28
use ApiPlatform\Core\Exception\RuntimeException;
29
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
30
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
31
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
32
use ApiPlatform\Core\Util\AttributesExtractor;
33
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
34
use Symfony\Component\PropertyAccess\PropertyAccess;
35
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
36
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
37
use Symfony\Component\Routing\RouterInterface;
38
39
/**
40
 * {@inheritdoc}
41
 *
42
 * @author Kévin Dunglas <[email protected]>
43
 */
44
final class IriConverter implements IriConverterInterface
45
{
46
    use ResourceClassInfoTrait;
47
    use OperationDataProviderTrait;
48
49
    private $routeNameResolver;
50
    private $router;
51
    private $identifiersExtractor;
52
    private $absoluteUrl;
53
54
    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, ResourceClassResolverInterface $resourceClassResolver = null, bool $absoluteUrl = false)
55
    {
56
        $this->itemDataProvider = $itemDataProvider;
57
        $this->routeNameResolver = $routeNameResolver;
58
        $this->router = $router;
59
        $this->identifiersExtractor = $identifiersExtractor;
60
        $this->subresourceDataProvider = $subresourceDataProvider;
61
        $this->identifierConverter = $identifierConverter;
62
        $this->resourceClassResolver = $resourceClassResolver;
63
        $this->absoluteUrl = $absoluteUrl;
64
65
        if (null === $identifiersExtractor) {
66
            @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);
67
            $this->identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactory, $propertyMetadataFactory, $propertyAccessor ?? PropertyAccess::createPropertyAccessor());
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getItemFromIri(string $iri, array $context = [])
75
    {
76
        try {
77
            $parameters = $this->router->match($iri);
78
        } catch (RoutingExceptionInterface $e) {
79
            throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e);
80
        }
81
82
        if (!isset($parameters['_api_resource_class'])) {
83
            throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
84
        }
85
86
        if (isset($parameters['_api_collection_operation_name'])) {
87
            throw new InvalidArgumentException(sprintf('The iri "%s" references a collection not an item.', $iri));
88
        }
89
90
        $attributes = AttributesExtractor::extractAttributes($parameters);
91
92
        try {
93
            $identifiers = $this->extractIdentifiers($parameters, $attributes);
94
        } catch (InvalidIdentifierException $e) {
95
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
96
        }
97
98
        if ($this->identifierConverter) {
99
            $context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] = true;
100
        }
101
102
        if (isset($attributes['subresource_operation_name'])) {
103
            if (($item = $this->getSubresourceData($identifiers, $attributes, $context)) && !\is_array($item)) {
104
                return $item;
105
            }
106
107
            throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
108
        }
109
110
        if ($item = $this->getItemData($identifiers, $attributes, $context)) {
111
            return $item;
112
        }
113
114
        throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
121
    {
122
        $resourceClass = $this->getResourceClass($item, true);
123
124
        try {
125
            $identifiers = $this->identifiersExtractor->getIdentifiersFromItem($item);
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

125
            /** @scrutinizer ignore-call */ 
126
            $identifiers = $this->identifiersExtractor->getIdentifiersFromItem($item);

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