Passed
Pull Request — 2.2 (#2051)
by
unknown
03:08
created

ResourceClassResolver::isResourceClass()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 1
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
33
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
34
    {
35
        $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string
42
    {
43
        $type = \is_object($value) && !$value instanceof \Traversable ? $this->getObjectClass($value) : $resourceClass;
44
        $resourceClass = $resourceClass ?? $type;
45
46
        if (null === $resourceClass) {
47
            throw new InvalidArgumentException(sprintf('No resource class found.'));
48
        }
49
50
        if (
51
            null === $type
52
            || ((!$strict || $resourceClass === $type) && $isResourceClass = $this->isResourceClass($type))
53
        ) {
54
            return $resourceClass;
55
        }
56
57
        if (
58
            ($isResourceClass ?? $this->isResourceClass($type))
59
            || (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass))
60
        ) {
61
            return $type;
62
        }
63
64
        throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $type));
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function isResourceClass(string $type): bool
71
    {
72
        if (isset($this->localIsResourceClassCache[$type])) {
73
            return $this->localIsResourceClassCache[$type];
74
        }
75
76
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
77
            if ($type === $resourceClass) {
78
                return $this->localIsResourceClassCache[$type] = true;
79
            }
80
        }
81
82
        return $this->localIsResourceClassCache[$type] = false;
83
    }
84
}
85