Completed
Pull Request — master (#608)
by Amrouche
03:41
created

EntrypointBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A getEntrypoint() 0 19 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Hal;
13
14
use ApiPlatform\Core\Api\IriConverterInterface;
15
use ApiPlatform\Core\Api\UrlGeneratorInterface;
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
use ApiPlatform\Core\Hypermedia\ContextBuilderInterface;
18
use ApiPlatform\Core\JsonLd\EntrypointBuilderInterface;
19
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
20
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
21
22
/**
23
 * {@inheritdoc}
24
 *
25
 * @author Kévin Dunglas <[email protected]>
26
 */
27
final class EntrypointBuilder implements EntrypointBuilderInterface
28
{
29
    private $resourceNameCollectionFactory;
30
    private $resourceMetadataFactory;
31
    private $iriConverter;
32
    private $urlGenerator;
33
    private $contextBuilder;
34
35 View Code Duplication
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator, ContextBuilderInterface $contextBuilder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
38
        $this->resourceMetadataFactory = $resourceMetadataFactory;
39
        $this->iriConverter = $iriConverter;
40
        $this->urlGenerator = $urlGenerator;
41
        $this->contextBuilder = $contextBuilder;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getEntrypoint(string $referenceType = UrlGeneratorInterface::ABS_PATH) : array
48
    {
49
        $entrypoint = $this->contextBuilder->getBaseContext($referenceType);
50
51
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
52
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
53
54
            if (empty($resourceMetadata->getCollectionOperations())) {
55
                continue;
56
            }
57
            try {
58
                $entrypoint['_links']['ap:'.$resourceMetadata->getShortName()] = ['href' => $this->iriConverter->getIriFromResourceClass($resourceClass)];
59
            } catch (InvalidArgumentException $ex) {
60
                // Ignore resources without GET operations
61
            }
62
        }
63
64
        return $entrypoint;
65
    }
66
}
67