Issues (332)

src/Api/ResourceClassResolver.php (2 issues)

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\Api;
15
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
18
use ApiPlatform\Core\Util\ClassInfoTrait;
19
20
/**
21
 * {@inheritdoc}
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 * @author Samuel ROZE <[email protected]>
25
 */
26
final class ResourceClassResolver implements ResourceClassResolverInterface
27
{
28
    use ClassInfoTrait;
29
30
    private $resourceNameCollectionFactory;
31
    private $localIsResourceClassCache = [];
32
    private $localMostSpecificResourceClassCache = [];
33
34
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
35
    {
36
        $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string
43
    {
44
        if ($strict && null === $resourceClass) {
45
            throw new InvalidArgumentException('Strict checking is only possible when resource class is specified.');
46
        }
47
48
        $actualClass = \is_object($value) && !$value instanceof \Traversable ? $this->getObjectClass($value) : null;
49
50
        if (null === $actualClass && null === $resourceClass) {
51
            throw new InvalidArgumentException('Resource type could not be determined. Resource class must be specified.');
52
        }
53
54
        if (null !== $actualClass && !$this->isResourceClass($actualClass)) {
55
            throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $actualClass));
56
        }
57
58
        if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) {
59
            throw new InvalidArgumentException(sprintf('Specified class "%s" is not a resource class.', $resourceClass));
60
        }
61
62
        if ($strict && null !== $actualClass && !is_a($actualClass, $resourceClass, true)) {
63
            throw new InvalidArgumentException(sprintf('Object of type "%s" does not match "%s" resource class.', $actualClass, $resourceClass));
64
        }
65
66
        $targetClass = $actualClass ?? $resourceClass;
67
68
        if (isset($this->localMostSpecificResourceClassCache[$targetClass])) {
69
            return $this->localMostSpecificResourceClassCache[$targetClass];
70
        }
71
72
        $mostSpecificResourceClass = null;
73
74
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClassName) {
75
            if (!is_a($targetClass, $resourceClassName, true)) {
76
                continue;
77
            }
78
79
            if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass)) {
0 ignored issues
show
$mostSpecificResourceClass of type void is incompatible with the type string expected by parameter $class_name of is_subclass_of(). ( Ignorable by Annotation )

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

79
            if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, /** @scrutinizer ignore-type */ $mostSpecificResourceClass)) {
Loading history...
80
                $mostSpecificResourceClass = $resourceClassName;
81
            }
82
        }
83
84
        if (null === $mostSpecificResourceClass) {
0 ignored issues
show
The condition null === $mostSpecificResourceClass is always true.
Loading history...
85
            throw new \LogicException('Unexpected execution flow.');
86
        }
87
88
        $this->localMostSpecificResourceClassCache[$targetClass] = $mostSpecificResourceClass;
89
90
        return $mostSpecificResourceClass;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function isResourceClass(string $type): bool
97
    {
98
        if (isset($this->localIsResourceClassCache[$type])) {
99
            return $this->localIsResourceClassCache[$type];
100
        }
101
102
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
103
            if (is_a($type, $resourceClass, true)) {
104
                return $this->localIsResourceClassCache[$type] = true;
105
            }
106
        }
107
108
        return $this->localIsResourceClassCache[$type] = false;
109
    }
110
}
111