SwaggerDumpCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 129
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A __construct() 0 6 1
B execute() 0 39 1
A getParameters() 0 14 2
B getResources() 0 24 3
A getRouteMethods() 0 4 2
1
<?php
2
3
namespace BrainExe\Core\Console;
4
5
use BrainExe\Core\Application\SerializedRouteCollection;
6
use BrainExe\Core\Traits\ConfigTrait;
7
use Generator;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use BrainExe\Core\Annotations\Command as CommandAnnotation;
12
use Symfony\Component\Routing\CompiledRoute;
13
use Symfony\Component\Routing\Route;
14
use Symfony\Component\Yaml\Dumper;
15
16
/**
17
 * @CommandAnnotation
18
 * @codeCoverageIgnore
19
 */
20
class SwaggerDumpCommand extends Command
21
{
22
23
    use ConfigTrait;
24
25
    /**
26
     * @var SerializedRouteCollection
27
     */
28
    private $routes;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this->setName('swagger:dump')
36
            ->setDescription('Dump swagger config');
37
    }
38
39
    /**
40
     * @param SerializedRouteCollection $rebuild
41
     */
42
    public function __construct(SerializedRouteCollection $rebuild)
43
    {
44
        $this->routes = $rebuild;
45
46
        parent::__construct();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $applicationName = $this->getParameter('application.name');
55
56
        $routes = $this->routes->all();
57
        $resources = $this->getResources($routes);
58
59
        $url = parse_url($this->getParameter('application.url'));
60
61
        $formatted = [
62
            'swagger' => '2.0',
63
            'info'    => [
64
                'title'       => $applicationName,
65
                'description' => sprintf('%s API', $applicationName),
66
                'version'     =>  '1.0.0'
67
            ],
68
            'consumes' => ['application/json'],
69
            'produces' => ['application/json', 'text/html'],
70
            'host'     => $url['host'],
71
            'schemes'  => [$url['scheme']],
72
            'securityDefinitions' => [
73
                'token' => [
74
                    'type'        => 'apiKey',
75
                    'description' => 'Given API Token',
76
                    'name'        => 'Token',
77
                    'in'          => 'Header'
78
                ]
79
            ],
80
            'security' => [
81
                'token' => [
82
                    'all'
83
                ]
84
            ],
85
            'paths' => $resources,
86
        ];
87
88
        $dumper = new Dumper();
89
        $output->writeln($dumper->dump($formatted, 4));
90
    }
91
92
    /**
93
     * @param Route $route
94
     * @return Generator
95
     */
96
    private function getParameters(Route $route) : Generator
97
    {
98
        /** @var CompiledRoute $compiled */
99
        $compiled = $route->compile();
100
101
        foreach ($compiled->getVariables() as $name) {
102
            yield [
103
                'name'     => $name,
104
                'type'     => 'string',
105
                'required' => true,
106
                'in'       => 'path'
107
            ];
108
        }
109
    }
110
111
    /**
112
     * @param array $routes
113
     * @return array
114
     */
115
    protected function getResources(array $routes) : array
116
    {
117
        $resources = [];
118
        foreach ($routes as $name => $route) {
119
            $parameters = iterator_to_array($this->getParameters($route));
120
121
            $data = [
122
                'summary' => $name,
123
                'responses' => [
124
                    200 => [
125
                        'description' => 'OK'
126
                    ]
127
                ]
128
            ];
129
130
            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...
131
                $data['parameters'] = $parameters;
132
            }
133
134
            $resources[$route->getPath()][$this->getRouteMethods($route)] = $data;
135
        }
136
137
        return $resources;
138
    }
139
140
    /**
141
     * @param Route $route
142
     * @return string
143
     */
144
    protected function getRouteMethods(Route $route) : string
145
    {
146
        return strtolower(implode(',', $route->getMethods()) ?: 'get');
147
    }
148
}
149