• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10749392760

07 Sep 2024 06:39AM UTC coverage: 7.646%. Remained the same
10749392760

push

github

soyuka
test: remove hydra prefix from guides

0 of 21 new or added lines in 6 files covered. (0.0%)

6834 existing lines in 225 files now uncovered.

12538 of 163980 relevant lines covered (7.65%)

22.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.38
/src/Metadata/ResourceClassResolver.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Metadata;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
18
use ApiPlatform\Metadata\Util\ClassInfoTrait;
19

20
/**
21
 * {@inheritdoc}
22
 *
23
 * @author Kévin Dunglas <dunglas@gmail.com>
24
 * @author Samuel ROZE <samuel.roze@gmail.com>
25
 */
26
final class ResourceClassResolver implements ResourceClassResolverInterface
27
{
28
    use ClassInfoTrait;
29
    private array $localIsResourceClassCache = [];
30
    private array $localMostSpecificResourceClassCache = [];
31

32
    public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
33
    {
UNCOV
34
    }
2,251✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getResourceClass(mixed $value, ?string $resourceClass = null, bool $strict = false): string
40
    {
UNCOV
41
        if ($strict && null === $resourceClass) {
2,071✔
42
            throw new InvalidArgumentException('Strict checking is only possible when resource class is specified.');
×
43
        }
44

UNCOV
45
        $objectClass = \is_object($value) ? $this->getObjectClass($value) : null;
2,071✔
UNCOV
46
        $actualClass = ($objectClass && (!$value instanceof \Traversable || $this->isResourceClass($objectClass))) ? $this->getObjectClass($value) : null;
2,071✔
47

UNCOV
48
        if (null === $actualClass && null === $resourceClass) {
2,071✔
49
            throw new InvalidArgumentException('Resource type could not be determined. Resource class must be specified.');
×
50
        }
51

UNCOV
52
        if (null !== $actualClass && !$this->isResourceClass($actualClass)) {
2,071✔
53
            throw new InvalidArgumentException(\sprintf('No resource class found for object of type "%s".', $actualClass));
15✔
54
        }
55

UNCOV
56
        if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) {
2,071✔
57
            throw new InvalidArgumentException(\sprintf('Specified class "%s" is not a resource class.', $resourceClass));
×
58
        }
59

UNCOV
60
        if ($strict && null !== $actualClass && !is_a($actualClass, $resourceClass, true)) {
2,071✔
61
            throw new InvalidArgumentException(\sprintf('Object of type "%s" does not match "%s" resource class.', $actualClass, $resourceClass));
×
62
        }
63

UNCOV
64
        $targetClass = $actualClass ?? $resourceClass;
2,071✔
65

UNCOV
66
        if (isset($this->localMostSpecificResourceClassCache[$targetClass])) {
2,071✔
UNCOV
67
            return $this->localMostSpecificResourceClassCache[$targetClass];
2,028✔
68
        }
69

UNCOV
70
        $mostSpecificResourceClass = null;
2,071✔
71

UNCOV
72
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClassName) {
2,071✔
UNCOV
73
            if (!is_a($targetClass, $resourceClassName, true)) {
2,071✔
UNCOV
74
                continue;
2,071✔
75
            }
76

UNCOV
77
            if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass)) {
2,071✔
UNCOV
78
                $mostSpecificResourceClass = $resourceClassName;
2,071✔
79
            }
80
        }
81

UNCOV
82
        if (null === $mostSpecificResourceClass) {
2,071✔
83
            throw new \LogicException('Unexpected execution flow.');
×
84
        }
85

UNCOV
86
        $this->localMostSpecificResourceClassCache[$targetClass] = $mostSpecificResourceClass;
2,071✔
87

UNCOV
88
        return $mostSpecificResourceClass;
2,071✔
89
    }
90

91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isResourceClass(string $type): bool
95
    {
UNCOV
96
        if (isset($this->localIsResourceClassCache[$type])) {
2,184✔
UNCOV
97
            return $this->localIsResourceClassCache[$type];
2,168✔
98
        }
99

UNCOV
100
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
2,184✔
UNCOV
101
            if (is_a($type, $resourceClass, true)) {
2,184✔
UNCOV
102
                return $this->localIsResourceClassCache[$type] = true;
2,173✔
103
            }
104
        }
105

UNCOV
106
        return $this->localIsResourceClassCache[$type] = false;
1,510✔
107
    }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc