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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

97.22
/src/Metadata/Resource/ResourceMetadataCollection.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\Resource;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\CollectionOperationInterface;
18
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
19
use ApiPlatform\Metadata\Operation;
20

21
/**
22
 * @extends \ArrayObject<int, ApiResource>
23
 */
24
final class ResourceMetadataCollection extends \ArrayObject
25
{
26
    private const GRAPHQL_PREFIX = 'g_';
27
    private const HTTP_PREFIX = 'h_';
28
    private const FORCE_COLLECTION = 'co_';
29
    private const HTTP_OPERATION = 'ht_';
30

31
    private array $operationCache = [];
32

33
    public function __construct(private readonly string $resourceClass, array $input = [])
34
    {
35
        parent::__construct($input);
844✔
36
    }
37

38
    public function getOperation(?string $operationName = null, bool $forceCollection = false, bool $httpOperation = false, bool $forceGraphQl = false): Operation
39
    {
40
        $operationName ??= '';
776✔
41
        $cachePrefix = ($forceCollection ? self::FORCE_COLLECTION : '').($httpOperation ? self::HTTP_OPERATION : '');
776✔
42
        $httpCacheKey = self::HTTP_PREFIX.$cachePrefix.$operationName;
776✔
43
        if (isset($this->operationCache[$httpCacheKey])) {
776✔
44
            return $this->operationCache[$httpCacheKey];
2✔
45
        }
46

47
        $gqlCacheKey = self::GRAPHQL_PREFIX.$cachePrefix.$operationName;
776✔
48
        if (isset($this->operationCache[$gqlCacheKey])) {
776✔
49
            return $this->operationCache[$gqlCacheKey];
×
50
        }
51

52
        $it = $this->getIterator();
776✔
53
        $metadata = null;
776✔
54

55
        while ($it->valid()) {
776✔
56
            /** @var ApiResource $metadata */
57
            $metadata = $it->current();
774✔
58

59
            if (!$forceGraphQl) {
774✔
60
                foreach ($metadata->getOperations() ?? [] as $name => $operation) {
774✔
61
                    $isCollection = $operation instanceof CollectionOperationInterface;
768✔
62
                    $method = $operation->getMethod();
768✔
63
                    $isGetOperation = 'GET' === $method || 'OPTIONS' === $method || 'HEAD' === $method;
768✔
64
                    if ('' === $operationName && $isGetOperation && ($forceCollection ? $isCollection : !$isCollection)) {
768✔
65
                        return $this->operationCache[$httpCacheKey] = $operation;
613✔
66
                    }
67

68
                    if ($name === $operationName) {
682✔
69
                        return $this->operationCache[$httpCacheKey] = $operation;
660✔
70
                    }
71

72
                    if ($operation->getUriTemplate() === $operationName) {
618✔
73
                        return $this->operationCache[$httpCacheKey] = $operation;
6✔
74
                    }
75
                }
76
            }
77

78
            foreach ($metadata->getGraphQlOperations() ?? [] as $name => $operation) {
48✔
79
                $isCollection = $operation instanceof CollectionOperationInterface;
38✔
80
                if ('' === $operationName && ($forceCollection ? $isCollection : !$isCollection) && false === $httpOperation) {
38✔
81
                    return $this->operationCache[$gqlCacheKey] = $operation;
4✔
82
                }
83

84
                if ($name === $operationName) {
38✔
85
                    return $this->operationCache[$httpCacheKey] = $operation;
26✔
86
                }
87
            }
88

89
            $it->next();
24✔
90
        }
91

92
        // Idea:
93
        // if ($metadata) {
94
        //     return (new class extends HttpOperation {})->withResource($metadata);
95
        // }
96

97
        $this->handleNotFound($operationName, $metadata);
22✔
98
    }
99

100
    /**
101
     * @throws OperationNotFoundException
102
     */
103
    private function handleNotFound(string $operationName, ?ApiResource $metadata): void
104
    {
105
        // Hide the FQDN in the exception message if possible
106
        $shortName = $metadata?->getShortName() ? $metadata->getShortName() : $this->resourceClass;
22✔
107
        if (!$metadata && false !== $pos = strrpos($shortName, '\\')) {
22✔
108
            $shortName = substr($shortName, $pos + 1);
4✔
109
        }
110

111
        throw new OperationNotFoundException(\sprintf('Operation "%s" not found for resource "%s".', $operationName, $shortName));
22✔
112
    }
113
}
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