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

api-platform / core / 10943429050

19 Sep 2024 02:48PM UTC coverage: 7.647% (-0.03%) from 7.675%
10943429050

push

github

web-flow
feat: api-platform/json-hal component (#6621)

* feat: add hal support for laravel

* feat: quick review

* fix: typo & cs-fixer

* fix: typo in composer.json

* fix: cs-fixer & phpstan

* fix: forgot about hal item normalizer, therefore there's no more createbook nor updatebook test as Hal is a readonly format

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

9082 existing lines in 291 files now uncovered.

12629 of 165144 relevant lines covered (7.65%)

22.89 hits per line

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

74.36
/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\Metadata\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\Operation;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23

24
trait LinksHandlerTrait
25
{
26
    private ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory;
27

28
    /**
29
     * @param array{linkClass?: string, linkProperty?: string}&array<string, mixed> $context
30
     *
31
     * @return \ApiPlatform\Metadata\Link[]
32
     */
33
    private function getLinks(string $resourceClass, Operation $operation, array $context): array
34
    {
UNCOV
35
        $links = $this->getOperationLinks($operation);
776✔
36

UNCOV
37
        if (!($linkClass = $context['linkClass'] ?? false)) {
776✔
UNCOV
38
            return $links;
748✔
39
        }
40

UNCOV
41
        $newLink = null;
61✔
UNCOV
42
        $linkProperty = $context['linkProperty'] ?? null;
61✔
43

UNCOV
44
        foreach ($links as $link) {
61✔
UNCOV
45
            if ($linkClass === $link->getFromClass() && $linkProperty === $link->getFromProperty()) {
55✔
UNCOV
46
                $newLink = $link;
1✔
UNCOV
47
                break;
1✔
48
            }
49
        }
50

UNCOV
51
        if ($newLink) {
61✔
UNCOV
52
            return [$newLink];
1✔
53
        }
54

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

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

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

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

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

UNCOV
89
        return [$newLink];
60✔
90
    }
91

92
    /**
93
     * @param array<int|string,mixed> $identifiers
94
     */
95
    private function getIdentifierValue(array &$identifiers, ?string $name = null): mixed
96
    {
UNCOV
97
        if (null !== $name && isset($identifiers[$name])) {
776✔
UNCOV
98
            $value = $identifiers[$name];
7✔
UNCOV
99
            unset($identifiers[$name]);
7✔
100

UNCOV
101
            return $value;
7✔
102
        }
103

UNCOV
104
        return array_shift($identifiers);
769✔
105
    }
106

107
    /**
108
     * @return \ApiPlatform\Metadata\Link[]|array
109
     */
110
    private function getOperationLinks(?Operation $operation = null): array
111
    {
UNCOV
112
        if ($operation instanceof GraphQlOperation) {
776✔
UNCOV
113
            return $operation->getLinks() ?? [];
61✔
114
        }
115

UNCOV
116
        if ($operation instanceof HttpOperation) {
748✔
UNCOV
117
            return $operation->getUriVariables() ?? [];
748✔
118
        }
119

120
        return [];
×
121
    }
122
}
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