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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

94.29
/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);
864✔
36
    }
37

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

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

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

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

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

67
                if ($name === $operationName) {
560✔
68
                    return $this->operationCache[$httpCacheKey] = $operation;
524✔
69
                }
70

71
                if ($operation->getUriTemplate() === $operationName) {
172✔
72
                    return $this->operationCache[$httpCacheKey] = $operation;
4✔
73
                }
74
            }
75

76
            foreach ($metadata->getGraphQlOperations() ?? [] as $name => $operation) {
52✔
77
                $isCollection = $operation instanceof CollectionOperationInterface;
40✔
78
                if ('' === $operationName && ($forceCollection ? $isCollection : !$isCollection) && false === $httpOperation) {
40✔
79
                    return $this->operationCache[$gqlCacheKey] = $operation;
×
80
                }
81

82
                if ($name === $operationName) {
40✔
83
                    return $this->operationCache[$httpCacheKey] = $operation;
40✔
84
                }
85
            }
86

87
            $it->next();
12✔
88
        }
89

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

95
        $this->handleNotFound($operationName, $metadata);
16✔
96
    }
97

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

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

© 2026 Coveralls, Inc