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

OpenApiDecorator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 15.79%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 17
c 3
b 1
f 0
dl 0
loc 37
ccs 3
cts 19
cp 0.1579
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 3 1
A normalize() 0 23 4
A __construct() 0 3 1
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