Completed
Pull Request — 2.2 (#2071)
by Kévin
08:17 queued 02:03
created

ResourceClassResolver::getResourceClass()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 7.3166
c 0
b 0
f 0
cc 11
nc 13
nop 3

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
        if (\is_object($value) && !$value instanceof \Traversable) {
44
            $typeToFind = $type = $this->getObjectClass($value);
45
            if (null === $resourceClass) {
46
                $resourceClass = $typeToFind;
47
            }
48
        } elseif (null === $resourceClass) {
49
            throw new InvalidArgumentException(sprintf('No resource class found.'));
50
        } else {
51
            $typeToFind = $type = $resourceClass;
52
        }
53
54
        if (($strict && $resourceClass !== $type) || false === $isResourceClass = $this->isResourceClass($typeToFind)) {
55
            if (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) {
56
                return $type;
57
            }
58
            if ($isResourceClass ?? $this->isResourceClass($typeToFind)) {
59
                return $typeToFind;
60
            }
61
62
            throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $typeToFind));
63
        }
64
65
        return $resourceClass;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function isResourceClass(string $type): bool
72
    {
73
        if (isset($this->localIsResourceClassCache[$type])) {
74
            return $this->localIsResourceClassCache[$type];
75
        }
76
77
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
78
            if ($type === $resourceClass) {
79
                return $this->localIsResourceClassCache[$type] = true;
80
            }
81
        }
82
83
        return $this->localIsResourceClassCache[$type] = false;
84
    }
85
}
86