Completed
Push — master ( d2b1b9...c80d49 )
by Antoine
22s queued 13s
created

ResourceClassResolver::getResourceClass()   C

Complexity

Conditions 14
Paths 20

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 15
nc 20
nop 3
dl 0
loc 29
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        // The Resource is an interface
58
        if ($value instanceof $resourceClass && $type !== $resourceClass && interface_exists($resourceClass)) {
59
            throw new InvalidArgumentException(sprintf('The given object\'s resource is the interface "%s", finding a class is not possible.', $resourceClass));
60
        }
61
62
        if (
63
            ($isResourceClass ?? $this->isResourceClass($type))
64
            || (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass))
65
        ) {
66
            return $type;
67
        }
68
69
        throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $type));
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function isResourceClass(string $type): bool
76
    {
77
        if (isset($this->localIsResourceClassCache[$type])) {
78
            return $this->localIsResourceClassCache[$type];
79
        }
80
81
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
82
            if ($type === $resourceClass) {
83
                return $this->localIsResourceClassCache[$type] = true;
84
            }
85
        }
86
87
        return $this->localIsResourceClassCache[$type] = false;
88
    }
89
}
90