Passed
Push — master ( 00ac5f...f9f6f8 )
by Daniel
05:32
created

OpenApiFactory::removePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
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\OpenApi\Factory\OpenApiFactoryInterface;
17
use ApiPlatform\Core\OpenApi\OpenApi;
18
use PackageVersions\Versions;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
class OpenApiFactory implements OpenApiFactoryInterface
24
{
25
    private OpenApiFactoryInterface $decorated;
26
27
    public function __construct(OpenApiFactoryInterface $decorated)
28
    {
29
        $this->decorated = $decorated;
30
    }
31
32
    private function removePath(OpenApi $openApi, string $path): void
33
    {
34
        $pathItem = $openApi->getPaths()->getPath($path);
35
        if ($pathItem) {
36
            $openApi->getPaths()->addPath(
37
                $path,
38
                $pathItem->withGet(null)->withPut(null)->withPost(null)->withDelete(null)->withPatch(null)
39
            );
40
        }
41
    }
42
43
    public function __invoke(array $context = []): OpenApi
44
    {
45
        $openApi = $this->decorated->__invoke($context);
46
        $version = sprintf('%s (%s)', $openApi->getInfo()->getVersion(), Versions::getVersion('silverbackis/api-components-bundle'));
47
48
        $this->removePath($openApi, '/_/abstract_components/{id}');
49
        $this->removePath($openApi, '/component/forms/{id}');
50
51
        return $openApi->withInfo($openApi->getInfo()->withVersion($version));
52
    }
53
}
54