Completed
Push — master ( 38f1ea...fd6c7c )
by Matze
04:05
created

SwaggerDumpCommand::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 39
rs 8.8571
cc 1
eloc 27
nc 1
nop 2
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
        $resources = $this->getResources($routes);
62
63
        $url = parse_url($this->getParameter('application.url'));
64
65
        $formatted = [
66
            'swagger' => '2.0',
67
            'info'    => [
68
                'title'       => $applicationName,
69
                'description' => sprintf('%s API', $applicationName),
70
                'version'     =>  '1.0.0'
71
            ],
72
            'consumes' => ['application/json'],
73
            'produces' => ['application/json', 'text/html'],
74
            'host'     => $url['host'],
75
            'schemes'  => [$url['scheme']],
76
            'securityDefinitions' => [
77
                'token' => [
78
                    'type'        => 'apiKey',
79
                    'description' => 'Given API Token',
80
                    'name'        => 'Token',
81
                    'in'          => 'Header'
82
                ]
83
            ],
84
            'security' => [
85
                'token' => [
86
                    'all'
87
                ]
88
            ],
89
            'paths' => $resources,
90
        ];
91
92
        $dumper = new Dumper();
93
        $output->writeln($dumper->dump($formatted, 4));
94
    }
95
96
    /**
97
     * @param Route $route
98
     * @return Generator
99
     */
100
    private function getParameters(Route $route) : Generator
101
    {
102
        /** @var CompiledRoute $compiled */
103
        $compiled = $route->compile();
104
105
        foreach ($compiled->getVariables() as $name) {
106
            yield [
107
                'name'     => $name,
108
                'type'     => 'string',
109
                'required' => true,
110
                'in'       => 'path'
111
            ];
112
        }
113
    }
114
115
    /**
116
     * @param array $routes
117
     * @return array
118
     */
119
    protected function getResources(array $routes) : array
120
    {
121
        $resources = [];
122
        foreach ($routes as $name => $route) {
123
            $parameters = iterator_to_array($this->getParameters($route));
124
125
            $data = [
126
                'summary' => $name,
127
                'responses' => [
128
                    200 => [
129
                        'description' => 'OK'
130
                    ]
131
                ]
132
            ];
133
134
            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...
135
                $data['parameters'] = $parameters;
136
            }
137
138
            $resources[$route->getPath()][$this->getRouteMethods($route)] = $data;
139
        }
140
141
        return $resources;
142
    }
143
144
    /**
145
     * @param Route $route
146
     * @return string
147
     */
148
    protected function getRouteMethods(Route $route) : string
149
    {
150
        return strtolower(implode(',', $route->getMethods()) ?: 'get');
151
    }
152
}
153