Completed
Pull Request — master (#904)
by Antoine
03:30
created

ResourceClassResolver::getResourceClass()   C

Complexity

Conditions 11
Paths 10

Size

Total Lines 23
Code Lines 14

Duplication

Lines 17
Ratio 73.91 %

Importance

Changes 0
Metric Value
dl 17
loc 23
rs 5.3929
c 0
b 0
f 0
cc 11
eloc 14
nc 10
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
namespace ApiPlatform\Core\Api;
13
14
use ApiPlatform\Core\DataProvider\PaginatorInterface;
15
use ApiPlatform\Core\Exception\InvalidArgumentException;
16
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
17
use ApiPlatform\Core\Util\ClassInfoTrait;
18
19
/**
20
 * {@inheritdoc}
21
 *
22
 * @author Kévin Dunglas <[email protected]>
23
 * @author Samuel ROZE <[email protected]>
24
 */
25
final class ResourceClassResolver implements ResourceClassResolverInterface
26
{
27
    use ClassInfoTrait;
28
29
    private $resourceNameCollectionFactory;
30
31
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
32
    {
33
        $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string
40
    {
41 View Code Duplication
        if (is_object($value) && !$value instanceof PaginatorInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            $typeToFind = $type = $this->getObjectClass($value);
43
            if (null === $resourceClass) {
44
                $resourceClass = $typeToFind;
45
            }
46
        } elseif (null === $resourceClass) {
47
            throw new InvalidArgumentException(sprintf('No resource class found.'));
48
        } else {
49
            $typeToFind = $type = $resourceClass;
50
        }
51
52 View Code Duplication
        if (($strict && isset($type) && $resourceClass !== $type) || !$this->isResourceClass($typeToFind)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            if (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) {
54
                return $type;
55
            }
56
57
            throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s"', $typeToFind));
58
        }
59
60
        return $resourceClass;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getResourceClassFromContext($value, array $context = null, bool $strict = false): string
67
    {
68
        $resourceClass = $context['resource_class'] ?? null;
69
70
        if (isset($context['subcollection_resource_class']) && null !== $context['subcollection_resource_class']) {
71
            $resourceClass = $context['subcollection_resource_class'];
72
        }
73
74 View Code Duplication
        if (is_object($value) && !$value instanceof PaginatorInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            $typeToFind = $type = $this->getObjectClass($value);
76
            if (null === $resourceClass) {
77
                $resourceClass = $typeToFind;
78
            }
79
        } elseif (null === $resourceClass) {
80
            throw new InvalidArgumentException(sprintf('No resource class found.'));
81
        } else {
82
            $typeToFind = $type = $resourceClass;
83
        }
84
85 View Code Duplication
        if (($strict && isset($type) && $resourceClass !== $type) || !$this->isResourceClass($typeToFind)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
            if (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) {
87
                return $type;
88
            }
89
90
            throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s"', $typeToFind));
91
        }
92
93
        return $resourceClass;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function isResourceClass(string $type): bool
100
    {
101
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
102
            if ($type === $resourceClass) {
103
                return true;
104
            }
105
        }
106
107
        return false;
108
    }
109
}
110