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

api-platform / core / 10508505187

22 Aug 2024 12:55PM UTC coverage: 7.704%. Remained the same
10508505187

push

github

soyuka
Merge 3.4

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

8840 existing lines in 284 files now uncovered.

12477 of 161949 relevant lines covered (7.7%)

22.99 hits per line

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

100.0
/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 ApiPlatform\Metadata\NotExposed} operation with {@see ApiPlatform\Symfony\Action\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
    {
UNCOV
40
        $this->linkFactory = $linkFactory;
2,248✔
UNCOV
41
        $this->decorated = $decorated;
2,248✔
42
    }
43

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

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

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

63
            /** @var HttpOperation $operation */
UNCOV
64
            foreach ($operations as $operation) {
34✔
65
                // An item operation has been found, nothing to do anymore in this factory
UNCOV
66
                if (('GET' === $operation->getMethod() && !$operation instanceof CollectionOperationInterface) || ($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false)) {
34✔
UNCOV
67
                    return $resourceMetadataCollection;
31✔
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 */
UNCOV
75
        [$key, $operation] = $this->getOperationWithDefaults(resource: $resource, operation: new NotExposed(), generated: true, ignoredOptions: ['uriTemplate', 'uriVariables']); // @phpstan-ignore-line $resource is defined if count > 0
8✔
76

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

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

UNCOV
83
        return $resourceMetadataCollection;
8✔
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

© 2025 Coveralls, Inc