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

ContextBuilder::getResourceContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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\UrlGeneratorInterface;
15
use ApiPlatform\Core\Hypermedia\ContextBuilderInterface;
16
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
17
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
18
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
19
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
20
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
21
22
/**
23
 * {@inheritdoc}
24
 *
25
 * @author Kévin Dunglas <[email protected]>
26
 */
27
final class ContextBuilder implements ContextBuilderInterface
28
{
29
    private $resourceNameCollectionFactory;
30
    private $resourceMetadataFactory;
31
    private $propertyNameCollectionFactory;
32
    private $propertyMetadataFactory;
33
    private $urlGenerator;
34
    private $docUri;
35
36
    /**
37
     * @var NameConverterInterface
38
     */
39
    private $nameConverter;
40
41 View Code Duplication
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, UrlGeneratorInterface $urlGenerator, string $docUri = '', NameConverterInterface $nameConverter = null)
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...
42
    {
43
        $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
44
        $this->resourceMetadataFactory = $resourceMetadataFactory;
45
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
46
        $this->propertyMetadataFactory = $propertyMetadataFactory;
47
        $this->urlGenerator = $urlGenerator;
48
        $this->nameConverter = $nameConverter;
49
        $this->docUri = $docUri;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL, string $linkUrl = '/') : array
56
    {
57
        return [
58
            '_links' => ['self' => ['href' => $referenceType ? $this->urlGenerator->generate('api_hal_entrypoint') : $linkUrl],
59
                         'curies' => [
60
                             ['name' => 'ap',
61
                              'href' => $this->urlGenerator->generate('api_hal_entrypoint').$this->docUri.'#section-{rel}',
62
                              'templated' => true,
63
                             ],
64
                         ],
65
            ],
66
        ];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH) : array
73
    {
74
        return [];
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : array
81
    {
82
        return [];
83
    }
84
85
    public function getResourceContextUri(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : string
86
    {
87
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
88
89
        return $this->urlGenerator->generate('api_jsonhal_context', ['shortName' => $resourceMetadata->getShortName()]);
90
    }
91
}
92