Completed
Pull Request — master (#552)
by Antoine
04:33 queued 01:12
created

IriConverter::getRouteName()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 7.1428
cc 8
eloc 12
nc 12
nop 2
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\ItemDataProviderInterface;
16
use ApiPlatform\Core\Api\UrlGeneratorInterface;
17
use ApiPlatform\Core\Exception\InvalidArgumentException;
18
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
20
use ApiPlatform\Core\Util\ClassInfoTrait;
21
use Symfony\Component\PropertyAccess\PropertyAccess;
22
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
23
use Symfony\Component\Routing\Exception\ExceptionInterface;
24
use Symfony\Component\Routing\RouterInterface;
25
26
/**
27
 * {@inheritdoc}
28
 *
29
 * @author Kévin Dunglas <[email protected]>
30
 */
31
final class IriConverter implements IriConverterInterface
32
{
33
    use ClassInfoTrait;
34
35
    private $propertyNameCollectionFactory;
36
    private $propertyMetadataFactory;
37
    private $itemDataProvider;
38
    private $router;
39
    private $propertyAccessor;
40
41
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null)
42
    {
43
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
44
        $this->propertyMetadataFactory = $propertyMetadataFactory;
45
        $this->itemDataProvider = $itemDataProvider;
46
        $this->router = $router;
47
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getItemFromIri(string $iri, bool $fetchData = false)
54
    {
55
        try {
56
            $parameters = $this->router->match($iri);
57
        } catch (ExceptionInterface $exception) {
58
            throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $exception->getCode(), $exception);
59
        }
60
61
        if (!isset($parameters['_resource_class']) || !isset($parameters['id'])) {
62
            throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
63
        }
64
65
        if ($item = $this->itemDataProvider->getItem($parameters['_resource_class'], $parameters['id'], null, $fetchData)) {
66
            return $item;
67
        }
68
69
        throw new InvalidArgumentException(sprintf('Item not found for "%s".', $iri));
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH) : string
76
    {
77
        $resourceClass = $this->getObjectClass($item);
78
        $routeName = $this->getRouteName($resourceClass, false);
79
80
        $identifiers = $this->generateIdentifiersUrl($this->getIdentifiersFromItem($item));
81
82
        return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType);
83
    }
84
85
    /**
86
     * Generate the identifier url.
87
     *
88
     * @param array $identifiers
89
     *
90
     * @return array
91
     */
92
    public function generateIdentifiersUrl(array $identifiers) : array
93
    {
94
        if (1 === count($identifiers)) {
95
            return [rawurlencode(array_values($identifiers)[0])];
96
        }
97
98
        foreach ($identifiers as $name => $value) {
99
            $identifiers[$name] = sprintf('%s=%s', $name, $value);
100
        }
101
102
        return $identifiers;
103
    }
104
105
    /**
106
     * Find identifiers from an Item (Object).
107
     *
108
     * @param object $item
109
     *
110
     * @throws RuntimeException
111
     *
112
     * @return array
113
     */
114
    private function getIdentifiersFromItem($item) : array
115
    {
116
        $identifiers = [];
117
        $resourceClass = $this->getObjectClass($item);
118
119
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
120
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
121
122
            if (!$propertyMetadata->isIdentifier()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $propertyMetadata->isIdentifier() of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
123
                continue;
124
            }
125
126
            $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName);
127
128
            if (!is_object($identifiers[$propertyName])) {
129
                continue;
130
            }
131
132
            $relatedResourceClass = $this->getObjectClass($identifiers[$propertyName]);
133
            $relatedItem = $identifiers[$propertyName];
134
135
            foreach ($this->propertyNameCollectionFactory->create($relatedResourceClass) as $relatedPropertyName) {
136
                $propertyMetadata = $this->propertyMetadataFactory->create($relatedResourceClass, $relatedPropertyName);
137
138
                if ($propertyMetadata->isIdentifier()) {
139
                    $identifiers[$propertyName] = $this->propertyAccessor->getValue($relatedItem, $relatedPropertyName);
140
                }
141
            }
142
143
            if (empty($identifiers[$propertyName])) {
144
                throw new \RuntimeException(sprintf('%s identifiers can not be found', $resourceClass));
145
            }
146
        }
147
148
        return $identifiers;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : string
155
    {
156
        try {
157
            return $this->router->generate($this->getRouteName($resourceClass, true), [], $referenceType);
158
        } catch (ExceptionInterface $e) {
159
            throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e);
160
        }
161
    }
162
163
    /**
164
     * Finds the route name for this resource.
165
     *
166
     * @param string $resourceClass
167
     * @param bool   $collection
168
     *
169
     * @throws InvalidArgumentException
170
     *
171
     * @return string
172
     */
173
    private function getRouteName(string $resourceClass, bool $collection) : string
174
    {
175
        $operationType = $collection ? 'collection' : 'item';
176
177
        foreach ($this->router->getRouteCollection()->all() as $routeName => $route) {
178
            $currentResourceClass = $route->getDefault('_resource_class');
179
            $operation = $route->getDefault(sprintf('_%s_operation_name', $operationType));
180
            $methods = $route->getMethods();
181
182
            if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || in_array('GET', $methods))) {
183
                $found = true;
184
                break;
185
            }
186
        }
187
188
        if (!isset($found)) {
189
            throw new InvalidArgumentException(sprintf('No route associated with the type "%s".', $resourceClass));
190
        }
191
192
        return $routeName;
0 ignored issues
show
Bug introduced by
The variable $routeName seems to be defined by a foreach iteration on line 177. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
193
    }
194
}
195