Passed
Push — master ( 63969d...b1d9d8 )
by Daniel
17:15
created

OpenApiDecorator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 16
dl 0
loc 35
ccs 3
cts 18
cp 0.1666
rs 10
c 1
b 1
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 3 1
A normalize() 0 21 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 Silverback\ApiComponentsBundle\Entity\Component\Form;
19
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
20
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class OpenApiDecorator implements NormalizerInterface
27
{
28
    private NormalizerInterface $decorated;
29
30 7
    public function __construct(NormalizerInterface $decorated)
31
    {
32 7
        $this->decorated = $decorated;
33 7
    }
34
35
    public function normalize($object, string $format = null, array $context = [])
36
    {
37
        if (!$object instanceof Documentation) {
38
            throw new InvalidArgumentException(sprintf('%s only supports %s', self::class, Documentation::class));
39
        }
40
41
        // We should prevent normalization for the FormInterface (Symfony) class. get the `Class elf does not exist` error
42
        // This currently removed the Form component from the docs... Not ideal!
43
        $resourceNameCollection = $object->getResourceNameCollection();
44
        $classes = [];
45
        $unsupported = [Form::class, AbstractComponent::class];
46
        foreach ($resourceNameCollection->getIterator() as $className) {
47
            if (\in_array($className, $unsupported, true)) {
48
                continue;
49
            }
50
            $classes[] = $className;
51
        }
52
        $newResourceNameCollection = new ResourceNameCollection($classes);
53
        $newDocumentation = new Documentation($newResourceNameCollection, $object->getTitle(), $object->getDescription(), $object->getVersion(), $object->getMimeTypes());
54
55
        return $this->decorated->normalize($newDocumentation, $format, $context);
56
    }
57
58
    public function supportsNormalization($data, string $format = null): bool
59
    {
60
        return $this->decorated->supportsNormalization($data, $format);
61
    }
62
}
63