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

api-platform / core / 10489772975

21 Aug 2024 12:22PM UTC coverage: 7.704%. Remained the same
10489772975

push

github

web-flow
doc: link monorepo to test laravel (#6530)

12476 of 161932 relevant lines covered (7.7%)

23.0 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);
2,243✔
36
    }
37

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

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

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

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

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

68
                    if ($name === $operationName) {
2,231✔
69
                        return $this->operationCache[$httpCacheKey] = $operation;
1,935✔
70
                    }
71

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

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

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

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

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

97
        $this->handleNotFound($operationName, $metadata);
38✔
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;
38✔
107
        if (!$metadata && false !== $pos = strrpos($shortName, '\\')) {
38✔
108
            $shortName = substr($shortName, $pos + 1);
13✔
109
        }
110

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