Passed
Push — master ( 934fcb...23ca99 )
by Daniel
11:14
created

OpenApiDecorator::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 2
b 1
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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 Silverback\ApiComponentsBundle\OpenApi;
15
16
use ApiPlatform\Core\Documentation\Documentation;
17
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
18
use PackageVersions\Versions;
19
use Silverback\ApiComponentsBundle\Entity\Component\Form;
20
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
21
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
22
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class OpenApiDecorator implements NormalizerInterface
28
{
29
    private NormalizerInterface $decorated;
30
31 7
    public function __construct(NormalizerInterface $decorated)
32
    {
33 7
        $this->decorated = $decorated;
34 7
    }
35
36
    public function normalize($object, $format = null, array $context = [])
37
    {
38
        if (!$object instanceof Documentation) {
39
            throw new InvalidArgumentException(sprintf('%s only supports %s', self::class, Documentation::class));
40
        }
41
42
        // We should prevent normalization for the FormInterface (Symfony) class. get the `Class elf does not exist` error
43
        // This currently removed the Form component from the docs... Not ideal!
44
        // See: https://github.com/api-platform/core/issues/3344
45
        $resourceNameCollection = $object->getResourceNameCollection();
46
        $classes = [];
47
        $unsupported = [Form::class, AbstractComponent::class];
48
        foreach ($resourceNameCollection->getIterator() as $className) {
49
            if (\in_array($className, $unsupported, true)) {
50
                continue;
51
            }
52
            $classes[] = $className;
53
        }
54
        $newResourceNameCollection = new ResourceNameCollection($classes);
55
        $version = sprintf('%s (%s)', $object->getVersion(), Versions::getVersion('silverbackis/api-components-bundle'));
56
        $newDocumentation = new Documentation($newResourceNameCollection, $object->getTitle(), $object->getDescription(), $version, $object->getMimeTypes());
57
58
        return $this->decorated->normalize($newDocumentation, $format, $context);
59
    }
60
61
    public function supportsNormalization($data, $format = null): bool
62
    {
63
        return $this->decorated->supportsNormalization($data, $format);
64
    }
65
}
66