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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    {
UNCOV
35
        parent::__construct($input);
×
36
    }
37

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

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

UNCOV
52
        $it = $this->getIterator();
×
UNCOV
53
        $metadata = null;
×
54

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

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

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

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

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

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

UNCOV
89
            $it->next();
×
90
        }
91

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

UNCOV
97
        $this->handleNotFound($operationName, $metadata);
×
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
UNCOV
106
        $shortName = $metadata?->getShortName() ? $metadata->getShortName() : $this->resourceClass;
×
UNCOV
107
        if (!$metadata && false !== $pos = strrpos($shortName, '\\')) {
×
UNCOV
108
            $shortName = substr($shortName, $pos + 1);
×
109
        }
110

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