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

api-platform / core / 6418833342

05 Oct 2023 12:11PM UTC coverage: 37.157% (+0.4%) from 36.79%
6418833342

push

github

web-flow
chore: move namespace Api Metadata (#5857)

57 of 57 new or added lines in 8 files covered. (100.0%)

10191 of 27427 relevant lines covered (37.16%)

17.94 hits per line

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

0.0
/src/Api/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\Api;
15

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

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

34
    public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory)
35
    {
36
    }
×
37

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

47
        $objectClass = \is_object($value) ? $this->getObjectClass($value) : null;
×
48
        $actualClass = ($objectClass && (!$value instanceof \Traversable || $this->isResourceClass($objectClass))) ? $this->getObjectClass($value) : null;
×
49

50
        if (null === $actualClass && null === $resourceClass) {
×
51
            throw new InvalidArgumentException('Resource type could not be determined. Resource class must be specified.');
×
52
        }
53

54
        if (null !== $actualClass && !$this->isResourceClass($actualClass)) {
×
55
            throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $actualClass));
×
56
        }
57

58
        if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) {
×
59
            throw new InvalidArgumentException(sprintf('Specified class "%s" is not a resource class.', $resourceClass));
×
60
        }
61

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

66
        $targetClass = $actualClass ?? $resourceClass;
×
67

68
        if (isset($this->localMostSpecificResourceClassCache[$targetClass])) {
×
69
            return $this->localMostSpecificResourceClassCache[$targetClass];
×
70
        }
71

72
        $mostSpecificResourceClass = null;
×
73

74
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClassName) {
×
75
            if (!is_a($targetClass, $resourceClassName, true)) {
×
76
                continue;
×
77
            }
78

79
            if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass)) {
×
80
                $mostSpecificResourceClass = $resourceClassName;
×
81
            }
82
        }
83

84
        if (null === $mostSpecificResourceClass) {
×
85
            throw new \LogicException('Unexpected execution flow.');
×
86
        }
87

88
        $this->localMostSpecificResourceClassCache[$targetClass] = $mostSpecificResourceClass;
×
89

90
        return $mostSpecificResourceClass;
×
91
    }
92

93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function isResourceClass(string $type): bool
97
    {
98
        if (isset($this->localIsResourceClassCache[$type])) {
×
99
            return $this->localIsResourceClassCache[$type];
×
100
        }
101

102
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
×
103
            if (is_a($type, $resourceClass, true)) {
×
104
                return $this->localIsResourceClassCache[$type] = true;
×
105
            }
106
        }
107

108
        return $this->localIsResourceClassCache[$type] = false;
×
109
    }
110
}
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