Completed
Pull Request — master (#607)
by Kévin
03:14
created

EntrypointBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getEntrypoint() 0 23 4
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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
namespace ApiPlatform\Core\Hydra;
13
14
use ApiPlatform\Core\Api\IriConverterInterface;
15
use ApiPlatform\Core\Api\UrlGeneratorInterface;
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
use ApiPlatform\Core\JsonLd\EntrypointBuilderInterface;
18
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
19
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
20
21
/**
22
 * {@inheritdoc}
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 */
26
final class EntrypointBuilder implements EntrypointBuilderInterface
27
{
28
    private $resourceNameCollectionFactory;
29
    private $resourceMetadataFactory;
30
    private $iriConverter;
31
    private $urlGenerator;
32
33
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator)
34
    {
35
        $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
36
        $this->resourceMetadataFactory = $resourceMetadataFactory;
37
        $this->iriConverter = $iriConverter;
38
        $this->urlGenerator = $urlGenerator;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getEntrypoint(string $referenceType = UrlGeneratorInterface::ABS_PATH) : array
45
    {
46
        $entrypoint = [
47
            '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint'], $referenceType),
48
            '@id' => $this->urlGenerator->generate('api_hydra_entrypoint', [], $referenceType),
49
            '@type' => 'Entrypoint',
50
        ];
51
52
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
53
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
54
55
            if (empty($resourceMetadata->getCollectionOperations())) {
56
                continue;
57
            }
58
            try {
59
                $entrypoint[lcfirst($resourceMetadata->getShortName())] = $this->iriConverter->getIriFromResourceClass($resourceClass);
60
            } catch (InvalidArgumentException $ex) {
61
                // Ignore resources without GET operations
62
            }
63
        }
64
65
        return $entrypoint;
66
    }
67
}
68