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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

0 of 1 new or added line in 1 file covered. (0.0%)

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

74.47
/src/Doctrine/Common/State/LinksHandlerTrait.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\Doctrine\Common\State;
15

16
use ApiPlatform\Exception\OperationNotFoundException;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
19
use ApiPlatform\Metadata\GraphQl\Query;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Link;
22
use ApiPlatform\Metadata\Operation;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24
use Psr\Container\ContainerInterface;
25

26
trait LinksHandlerTrait
27
{
28
    private ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory;
29
    private ?ContainerInterface $handleLinksLocator;
30

31
    /**
32
     * @return Link[]
33
     */
34
    private function getLinks(string $resourceClass, Operation $operation, array $context): array
35
    {
36
        $links = $this->getOperationLinks($operation);
828✔
37

38
        if (!($linkClass = $context['linkClass'] ?? false)) {
828✔
39
            return $links;
801✔
40
        }
41

42
        $newLink = null;
54✔
43
        $linkProperty = $context['linkProperty'] ?? null;
54✔
44

45
        foreach ($links as $link) {
54✔
46
            if ($linkClass === $link->getFromClass() && $linkProperty === $link->getFromProperty()) {
48✔
47
                $newLink = $link;
3✔
48
                break;
3✔
49
            }
50
        }
51

52
        if ($newLink) {
54✔
53
            return [$newLink];
3✔
54
        }
55

56
        if (!$this->resourceMetadataCollectionFactory) {
51✔
57
            return [];
×
58
        }
59

60
        // Using GraphQL, it's possible that we won't find a GraphQL Operation of the same type (e.g. it is disabled).
61
        try {
62
            $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($linkClass);
51✔
63
            $linkedOperation = $resourceMetadataCollection->getOperation($operation->getName());
51✔
64
        } catch (OperationNotFoundException $e) {
×
65
            if (!$operation instanceof GraphQlOperation) {
×
66
                throw $e;
×
67
            }
68

69
            // Instead, we'll look for the first Query available.
70
            foreach ($resourceMetadataCollection as $resourceMetadata) {
×
71
                foreach ($resourceMetadata->getGraphQlOperations() as $op) {
×
72
                    if ($op instanceof Query) {
×
73
                        $linkedOperation = $op;
×
74
                    }
75
                }
76
            }
77
        }
78

79
        foreach ($this->getOperationLinks($linkedOperation ?? null) as $link) {
51✔
80
            if ($resourceClass === $link->getToClass() && $linkProperty === $link->getFromProperty()) {
51✔
81
                $newLink = $link;
51✔
82
                break;
51✔
83
            }
84
        }
85

86
        if (!$newLink) {
51✔
87
            throw new RuntimeException(sprintf('The class "%s" cannot be retrieved from "%s".', $resourceClass, $linkClass));
×
88
        }
89

90
        return [$newLink];
51✔
91
    }
92

93
    private function getIdentifierValue(array &$identifiers, string $name = null): mixed
94
    {
95
        if (isset($identifiers[$name])) {
828✔
96
            $value = $identifiers[$name];
21✔
97
            unset($identifiers[$name]);
21✔
98

99
            return $value;
21✔
100
        }
101

102
        return array_shift($identifiers);
807✔
103
    }
104

105
    private function getOperationLinks(Operation $operation = null): array
106
    {
107
        if ($operation instanceof GraphQlOperation) {
828✔
108
            return $operation->getLinks() ?? [];
54✔
109
        }
110

111
        if ($operation instanceof HttpOperation) {
801✔
112
            return $operation->getUriVariables() ?? [];
801✔
113
        }
114

115
        return [];
×
116
    }
117

118
    private function getLinksHandler(Operation $operation): ?callable
119
    {
120
        if (!($options = $operation->getStateOptions()) || !method_exists($options, 'getHandleLinks') || null === $options->getHandleLinks()) {
1,587✔
121
            return null;
1,581✔
122
        }
123

124
        $handleLinks = $options->getHandleLinks(); // @phpstan-ignore-line method_exists called above
6✔
125
        if (\is_callable($handleLinks)) {
6✔
UNCOV
126
            return $handleLinks;
×
127
        }
128

129
        if ($this->handleLinksLocator && \is_string($handleLinks) && $this->handleLinksLocator->has($handleLinks)) {
6✔
130
            return [$this->handleLinksLocator->get($handleLinks), 'handleLinks'];
6✔
131
        }
132

133
        throw new RuntimeException(sprintf('Could not find handleLinks service "%s"', $handleLinks));
×
134
    }
135
}
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