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

api-platform / core / 4498909317

pending completion
4498909317

push

github

soyuka
Merge 3.1

364 of 364 new or added lines in 61 files covered. (100.0%)

10781 of 17989 relevant lines covered (59.93%)

11.36 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
    public static $skolemUriTemplate = '/.well-known/genid/{id}';
32

33
    private $linkFactory;
34
    private $decorated;
35

36
    public function __construct(LinkFactoryInterface $linkFactory, ?ResourceMetadataCollectionFactoryInterface $decorated = null)
37
    {
38
        $this->linkFactory = $linkFactory;
34✔
39
        $this->decorated = $decorated;
34✔
40
    }
41

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

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

57
        /** @var ApiResource $resource */
58
        foreach ($resourceMetadataCollection as $resource) {
3✔
59
            $operations = $resource->getOperations();
3✔
60

61
            foreach ($operations as $operation) {
3✔
62
                // An item operation has been found, nothing to do anymore in this factory
63
                if ((HttpOperation::METHOD_GET === $operation->getMethod() && !$operation instanceof CollectionOperationInterface) || ($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false)) {
3✔
64
                    return $resourceMetadataCollection;
2✔
65
                }
66
            }
67
        }
68

69
        // No item operation has been found on all resources for resource class: generate one on the last resource
70
        // Helpful to generate an IRI for a resource without declaring the Get operation
71
        /** @var HttpOperation $operation */
72
        $operation = (new NotExposed())->withClass($resource->getClass())->withShortName($resource->getShortName()); // @phpstan-ignore-line $resource is defined if count > 0
2✔
73

74
        if (!$this->linkFactory->createLinksFromIdentifiers($operation)) {
2✔
75
            $operation = $operation->withUriTemplate(self::$skolemUriTemplate);
2✔
76
        }
77

78
        $operations->add(sprintf('_api_%s_get', $operation->getShortName()), $operation)->sort(); // @phpstan-ignore-line $operation exists
2✔
79

80
        return $resourceMetadataCollection;
2✔
81
    }
82
}
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