Passed
Push — master ( 79b4c4...b51eea )
by Daniel
05:43
created

OpenApiFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
nc 1
nop 2
dl 0
loc 4
c 2
b 0
f 0
cc 1
ccs 0
cts 3
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\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 removeResources(OpenApi $openApi, array $resourceClassNames): void
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
        $openApiPaths = $openApi->getPaths();
53
        $paths = $openApiPaths->getPaths();
54
        foreach ($paths as $path => $pathItem) {
55
            $operation = $pathItem->getGet() ?: $pathItem->getPost();
56
            if (!$operation) {
57
                continue;
58
            }
59
            $tags = $operation->getTags();
60
            if (!empty(array_intersect($tags, $shortNames))) {
61
                $openApiPaths->addPath($path, new PathItem());
62
            }
63
        }
64
    }
65
66
    public function __invoke(array $context = []): OpenApi
67
    {
68
        $openApi = $this->decorated->__invoke($context);
69
        $version = sprintf('%s (%s)', $openApi->getInfo()->getVersion(), Versions::getVersion('silverbackis/api-components-bundle'));
70
71
        $this->removeResources($openApi, [
72
            AbstractComponent::class,
73
            AbstractPageData::class,
74
            Forms::class,
75
            MediaObject::class,
76
        ]);
77
78
        return $openApi->withInfo($openApi->getInfo()->withVersion($version));
79
    }
80
}
81