Completed
Push — master ( ba9072...49bd4f )
by Matze
06:56
created

SwaggerDumpCommand::getParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace BrainExe\Core\Console;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Core\Application\SerializedRouteCollection;
7
8
use BrainExe\Core\Traits\ConfigTrait;
9
use Generator;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
use BrainExe\Core\Annotations\Command as CommandAnnotation;
15
use Symfony\Component\Routing\CompiledRoute;
16
use Symfony\Component\Routing\Route;
17
use Symfony\Component\Yaml\Dumper;
18
19
/**
20
 * @CommandAnnotation
21
 * @codeCoverageIgnore
22
 */
23
class SwaggerDumpCommand extends Command
24
{
25
26
    use ConfigTrait;
27
28
    /**
29
     * @var SerializedRouteCollection
30
     */
31
    private $routes;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function configure()
37
    {
38
        $this->setName('swagger:dump')
39
            ->setDescription('Dump swagger config');
40
    }
41
42
    /**
43
     * @Inject("@Core.RouteCollection")
44
     * @param SerializedRouteCollection $rebuild
45
     */
46
    public function __construct(SerializedRouteCollection $rebuild)
47
    {
48
        $this->routes = $rebuild;
49
50
        parent::__construct();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $applicationName = $this->getParameter('application.name');
59
60
        $routes = $this->routes->all();
61
62
        $dumper = new Dumper();
63
64
        $resources = [];
65
        foreach ($routes as $name => $route) {
66
            $parameters = iterator_to_array($this->getParameters($route));
67
68
            $data = [
69
                'summary'    => $name,
70
                'responses'  => [
71
                    200 => [
72
                        'description' => 'OK'
73
                    ]
74
                ]
75
            ];
76
77
            if ($parameters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parameters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
                $data['parameters'] = $parameters;
79
            }
80
81
            $resources[$route->getPath()][strtolower(implode(',', $route->getMethods()) ?: 'get')] = $data;
82
        }
83
84
        $url = parse_url($this->getParameter('application.url'));
85
86
        $formatted = [
87
            'swagger' => '2.0',
88
            'info'    => [
89
                'title'       => $applicationName,
90
                'description' => sprintf('%s API', $applicationName),
91
                'version'     =>  '1.0.0'
92
            ],
93
            'consumes' => ['application/json'],
94
            'produces' => ['application/json', 'text/html'],
95
            'host'     => $url['host'],
96
            'schemes'  => [$url['scheme']],
97
            'securityDefinitions' => [
98
                'token' => [
99
                    'type'        => 'apiKey',
100
                    'description' => 'Given API Token',
101
                    'name'        => 'Token',
102
                    'in'          => 'Header'
103
                ]
104
            ],
105
            'security' => [
106
                'token' => [
107
                    'all'
108
                ]
109
            ],
110
            'paths' => $resources,
111
        ];
112
113
        echo $dumper->dump($formatted, 4);
114
        echo PHP_EOL;
115
    }
116
117
    /**
118
     * @param Route $route
119
     * @return Generator
120
     */
121
    private function getParameters(Route $route)
122
    {
123
        /** @var CompiledRoute $compiled */
124
        $compiled = $route->compile();
125
126
        foreach ($compiled->getVariables() as $name) {
127
            yield [
128
                'name'     => $name,
129
                'type'     => 'string',
130
                'required' => true,
131
                'in'       => 'path'
132
            ];
133
        }
134
    }
135
}
136