Completed
Pull Request — master (#1036)
by Hector
03:29
created

src/Hal/Serializer/EntrypointNormalizer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Hal\Serializer;
15
16
use ApiPlatform\Core\Api\Entrypoint;
17
use ApiPlatform\Core\Api\IriConverterInterface;
18
use ApiPlatform\Core\Api\UrlGeneratorInterface;
19
use ApiPlatform\Core\Exception\InvalidArgumentException;
20
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
23
/**
24
 * Normalizes the API entrypoint.
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
final class EntrypointNormalizer implements NormalizerInterface
29
{
30
    const FORMAT = 'jsonhal';
31
32
    private $resourceMetadataFactory;
33
    private $iriConverter;
34
    private $urlGenerator;
35
36
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator)
37
    {
38
        $this->resourceMetadataFactory = $resourceMetadataFactory;
39
        $this->iriConverter = $iriConverter;
40
        $this->urlGenerator = $urlGenerator;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize($object, $format = null, array $context = [])
47
    {
48
        $entrypoint = ['_links' => ['self' => ['href' => $this->urlGenerator->generate('api_entrypoint')]]];
49
50 View Code Duplication
        foreach ($object->getResourceNameCollection() as $resourceClass) {
0 ignored issues
show
This code seems to be duplicated across 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...
51
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
52
53
            if (empty($resourceMetadata->getCollectionOperations())) {
54
                continue;
55
            }
56
            try {
57
                $entrypoint['_links'][lcfirst($resourceMetadata->getShortName())]['href'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
58
            } catch (InvalidArgumentException $ex) {
59
                // Ignore resources without GET operations
60
            }
61
        }
62
63
        return $entrypoint;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function supportsNormalization($data, $format = null)
70
    {
71
        return self::FORMAT === $format && $data instanceof Entrypoint;
72
    }
73
}
74