Completed
Pull Request — master (#904)
by Antoine
04:19 queued 01:13
created

IriConverter::getSubresourceIriFromResourceClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
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
namespace ApiPlatform\Core\Bridge\Symfony\Routing;
13
14
use ApiPlatform\Core\Api\IriConverterInterface;
15
use ApiPlatform\Core\Api\OperationType;
16
use ApiPlatform\Core\Api\UrlGeneratorInterface;
17
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
18
use ApiPlatform\Core\Exception\InvalidArgumentException;
19
use ApiPlatform\Core\Exception\ItemNotFoundException;
20
use ApiPlatform\Core\Exception\RuntimeException;
21
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
22
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
23
use ApiPlatform\Core\Util\ClassInfoTrait;
24
use Symfony\Component\PropertyAccess\PropertyAccess;
25
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
26
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
27
use Symfony\Component\Routing\RouterInterface;
28
29
/**
30
 * {@inheritdoc}
31
 *
32
 * @author Kévin Dunglas <[email protected]>
33
 */
34
final class IriConverter implements IriConverterInterface
35
{
36
    use ClassInfoTrait;
37
38
    private $propertyNameCollectionFactory;
39
    private $propertyMetadataFactory;
40
    private $itemDataProvider;
41
    private $routeNameResolver;
42
    private $router;
43
    private $propertyAccessor;
44
45
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null)
46
    {
47
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
48
        $this->propertyMetadataFactory = $propertyMetadataFactory;
49
        $this->itemDataProvider = $itemDataProvider;
50
        $this->routeNameResolver = $routeNameResolver;
51
        $this->router = $router;
52
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getItemFromIri(string $iri, array $context = [])
59
    {
60
        try {
61
            $parameters = $this->router->match($iri);
62
        } catch (RoutingExceptionInterface $e) {
63
            throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e);
64
        }
65
66
        if (!isset($parameters['_api_resource_class'], $parameters['id'])) {
67
            throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
68
        }
69
70
        if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $parameters['id'], null, $context)) {
71
            return $item;
72
        }
73
74
        throw new ItemNotFoundException(sprintf('Item not found for "%s".', $iri));
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
81
    {
82
        $resourceClass = $this->getObjectClass($item);
83
        $routeName = $this->routeNameResolver->getRouteName($resourceClass, false);
84
85
        $identifiers = $this->generateIdentifiersUrl($this->getIdentifiersFromItem($item));
86
87
        return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 View Code Duplication
    public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        try {
96
            return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::COLLECTION), [], $referenceType);
97
        } catch (RoutingExceptionInterface $e) {
98
            throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e);
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 View Code Duplication
    public function getSubresourceIriFromResourceClass(string $resourceClass, array $identifiers, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        try {
108
            return $this->router->generate($this->routeNameResolver->getRouteName($resourceClass, OperationType::SUBRESOURCE), $identifiers, $referenceType);
109
        } catch (RoutingExceptionInterface $e) {
110
            throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e);
111
        }
112
    }
113
114
    /**
115
     * Find identifiers from an Item (Object).
116
     *
117
     * @param object $item
118
     *
119
     * @throws RuntimeException
120
     *
121
     * @return array
122
     */
123
    private function getIdentifiersFromItem($item): array
124
    {
125
        $identifiers = [];
126
        $resourceClass = $this->getObjectClass($item);
127
128
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
129
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
130
131
            $identifier = $propertyMetadata->isIdentifier();
132
            if (null === $identifier || false === $identifier) {
133
                continue;
134
            }
135
136
            $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName);
137
138
            if (!is_object($identifiers[$propertyName])) {
139
                continue;
140
            }
141
142
            $relatedResourceClass = $this->getObjectClass($identifiers[$propertyName]);
143
            $relatedItem = $identifiers[$propertyName];
144
145
            unset($identifiers[$propertyName]);
146
147
            foreach ($this->propertyNameCollectionFactory->create($relatedResourceClass) as $relatedPropertyName) {
148
                $propertyMetadata = $this->propertyMetadataFactory->create($relatedResourceClass, $relatedPropertyName);
149
150
                if ($propertyMetadata->isIdentifier()) {
151
                    if (isset($identifiers[$propertyName])) {
152
                        throw new RuntimeException(sprintf('Composite identifiers not supported in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass));
153
                    }
154
155
                    $identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedPropertyName);
156
                }
157
            }
158
159
            if (!isset($identifiers[$propertyName])) {
160
                throw new RuntimeException(sprintf('No identifier found in "%s" through relation "%s" of "%s" used as identifier', $relatedResourceClass, $propertyName, $resourceClass));
161
            }
162
        }
163
164
        return $identifiers;
165
    }
166
167
    /**
168
     * Generate the identifier url.
169
     *
170
     * @param array $identifiers
171
     *
172
     * @return string[]
173
     */
174
    private function generateIdentifiersUrl(array $identifiers): array
175
    {
176
        if (1 === count($identifiers)) {
177
            return [rawurlencode(array_values($identifiers)[0])];
178
        }
179
180
        foreach ($identifiers as $name => $value) {
181
            $identifiers[$name] = sprintf('%s=%s', $name, $value);
182
        }
183
184
        return $identifiers;
185
    }
186
}
187