Completed
Push — master ( c27464...b3cf3b )
by Kévin
07:23 queued 04:19
created

IriConverter::getIriFromItem()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
eloc 16
nc 6
nop 2
dl 0
loc 25
rs 8.5806
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
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
81
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
82
83
            if ($propertyMetadata->isIdentifier()) {
84
                $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$identifiers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $identifiers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
85
            }
86
        }
87
88
        if (1 === count($identifiers)) {
89
            $identifiers = array_map(function ($identifierValue) {
90
                return rawurlencode($identifierValue);
91
            }, $identifiers);
0 ignored issues
show
Bug introduced by
The variable $identifiers does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
92
        } else {
93
            $identifiers = array_map(function ($identifierName, $identifierValue) {
94
                return sprintf('%s=%s', $identifierName, rawurlencode($identifierValue));
95
            }, array_keys($identifiers), $identifiers);
96
        }
97
98
        return $this->router->generate($routeName, ['id' => implode(';', $identifiers)], $referenceType);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getIriFromResourceClass(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : string
105
    {
106
        try {
107
            return $this->router->generate($this->getRouteName($resourceClass, true), [], $referenceType);
108
        } catch (ExceptionInterface $e) {
109
            throw new InvalidArgumentException(sprintf('Unable to generate an IRI for "%s".', $resourceClass), $e->getCode(), $e);
110
        }
111
    }
112
113
    /**
114
     * Finds the route name for this resource.
115
     *
116
     * @param string $resourceClass
117
     * @param bool   $collection
118
     *
119
     * @throws InvalidArgumentException
120
     *
121
     * @return string
122
     */
123
    private function getRouteName(string $resourceClass, bool $collection) : string
124
    {
125
        $operationType = $collection ? 'collection' : 'item';
126
127
        foreach ($this->router->getRouteCollection()->all() as $routeName => $route) {
128
            $currentResourceClass = $route->getDefault('_resource_class');
129
            $operation = $route->getDefault(sprintf('_%s_operation_name', $operationType));
130
            $methods = $route->getMethods();
131
132
            if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || in_array('GET', $methods))) {
133
                $found = true;
134
                break;
135
            }
136
        }
137
138
        if (!isset($found)) {
139
            throw new InvalidArgumentException(sprintf('No route associated with the type "%s".', $resourceClass));
140
        }
141
142
        return $routeName;
0 ignored issues
show
Bug introduced by
The variable $routeName seems to be defined by a foreach iteration on line 127. 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...
143
    }
144
}
145