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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

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

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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);
1,332✔
36
    }
37

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

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

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

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

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

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

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

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

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

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

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

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

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