Test Failed
Push — master ( 2a2e23...344e96 )
by Daniel
04:30
created

OpenApiFactory::removeResources()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 30
rs 9.6111
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\Exception\ResourceClassNotFoundException;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
19
use ApiPlatform\Core\OpenApi\Model\PathItem;
20
use ApiPlatform\Core\OpenApi\OpenApi;
21
use PackageVersions\Versions;
22
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
23
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
24
use Silverback\ApiComponentsBundle\Model\Uploadable\MediaObject;
25
use Symfony\Component\Form\Forms;
26
27
/**
28
 * @author Daniel West <[email protected]>
29
 */
30
class OpenApiFactory implements OpenApiFactoryInterface
31
{
32
    private OpenApiFactoryInterface $decorated;
33
    private ResourceMetadataFactoryInterface $resourceMetadataFactory;
34
35
    public function __construct(OpenApiFactoryInterface $decorated, ResourceMetadataFactoryInterface $resourceMetadataFactory)
36
    {
37
        $this->decorated = $decorated;
38
        $this->resourceMetadataFactory = $resourceMetadataFactory;
39
    }
40
41
    private function getResourceClassShortNames(array $resourceClassNames): array
42
    {
43
        $shortNames = [];
44
        foreach ($resourceClassNames as $resourceClassName) {
45
            try {
46
                $metadata = $this->resourceMetadataFactory->create($resourceClassName);
47
                $shortNames[] = $metadata->getShortName();
48
            } catch (ResourceClassNotFoundException $exception) {
49
                // the component may not be enabled
50
            }
51
        }
52
53
        return $shortNames;
54
    }
55
56
    private function removeResources(OpenApi $openApi, array $resourceClassNames): void
57
    {
58
        $shortNames = $this->getResourceClassShortNames($resourceClassNames);
59
        $openApiPaths = $openApi->getPaths();
60
        $paths = $openApiPaths->getPaths();
61
        foreach ($paths as $path => $pathItem) {
62
            $operation = $pathItem->getGet() ?: $pathItem->getPost();
63
            if (!$operation) {
64
                continue;
65
            }
66
            $tags = $operation->getTags();
67
            if (!empty(array_intersect($tags, $shortNames))) {
68
                $openApiPaths->addPath($path, new PathItem());
69
            }
70
        }
71
    }
72
73
    public static function getExtendedVersion(string $version): string
74
    {
75
        return sprintf('%s (%s)', $version, Versions::getVersion('silverbackis/api-components-bundle'));
76
    }
77
78
    public function __invoke(array $context = []): OpenApi
79
    {
80
        $openApi = $this->decorated->__invoke($context);
81
        $version = self::getExtendedVersion($openApi->getInfo()->getVersion());
82
83
        $this->removeResources($openApi, [
84
            AbstractComponent::class,
85
            AbstractPageData::class,
86
            Forms::class,
87
            MediaObject::class,
88
        ]);
89
90
        return $openApi->withInfo($openApi->getInfo()->withVersion($version));
91
    }
92
}
93