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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

94.12
/src/Metadata/Resource/Factory/NotExposedOperationResourceMetadataCollectionFactory.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\Factory;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\CollectionOperationInterface;
18
use ApiPlatform\Metadata\Get;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\NotExposed;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22

23
/**
24
 * Adds a {@see NotExposed} operation with {@see NotFoundAction} on a resource which only has a GetCollection.
25
 * This operation helps to generate resource IRI for items.
26
 *
27
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
28
 */
29
final class NotExposedOperationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
30
{
31
    use OperationDefaultsTrait;
32

33
    public static $skolemUriTemplate = '/.well-known/genid/{id}';
34

35
    private $linkFactory;
36
    private $decorated;
37

38
    public function __construct(LinkFactoryInterface $linkFactory, ResourceMetadataCollectionFactoryInterface $decorated = null)
39
    {
40
        $this->linkFactory = $linkFactory;
160✔
41
        $this->decorated = $decorated;
160✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function create(string $resourceClass): ResourceMetadataCollection
48
    {
49
        $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
28✔
50
        if ($this->decorated) {
28✔
51
            $resourceMetadataCollection = $this->decorated->create($resourceClass);
28✔
52
        }
53

54
        // Do not add a NotExposed operation on a resourceClass with no resource
55
        if (!$resourceMetadataCollection->count()) {
28✔
56
            return $resourceMetadataCollection;
×
57
        }
58

59
        /** @var ApiResource $resource */
60
        foreach ($resourceMetadataCollection as $resource) {
28✔
61
            $operations = $resource->getOperations();
28✔
62

63
            /** @var \ApiPlatform\Metadata\HttpOperation $operation */
64
            foreach ($operations as $operation) {
28✔
65
                // An item operation has been found, nothing to do anymore in this factory
66
                if (('GET' === $operation->getMethod() && !$operation instanceof CollectionOperationInterface) || ($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false)) {
28✔
67
                    return $resourceMetadataCollection;
24✔
68
                }
69
            }
70
        }
71

72
        // No item operation has been found on all resources for resource class: generate one on the last resource
73
        // Helpful to generate an IRI for a resource without declaring the Get operation
74
        /** @var HttpOperation $operation */
75
        [$key, $operation] = $this->getOperationWithDefaults(resource: $resource, operation: new NotExposed(), generated: true, ignoredOptions: ['uriTemplate', 'uriVariables']); // @phpstan-ignore-line $resource is defined if count > 0
12✔
76

77
        if (!$this->linkFactory->createLinksFromIdentifiers($operation)) {
12✔
78
            $operation = $operation->withUriTemplate(self::$skolemUriTemplate);
12✔
79
        }
80

81
        $operations->add($key, $operation)->sort(); // @phpstan-ignore-line $operation exists
12✔
82

83
        return $resourceMetadataCollection;
12✔
84
    }
85
}
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