Completed
Pull Request — master (#5)
by Joao
06:58
created

Scripts::genRestDocs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Builder;
4
5
use ByJG\Config\Exception\ConfigNotFoundException;
6
use ByJG\Config\Exception\EnvironmentException;
7
use ByJG\Config\Exception\KeyNotFoundException;
8
use ByJG\DbMigration\Database\MySqlDatabase;
9
use ByJG\DbMigration\Exception\InvalidMigrationFile;
10
use ByJG\DbMigration\Migration;
11
use ByJG\Util\Uri;
12
use Composer\Script\Event;
13
use Exception;
14
use Psr\SimpleCache\InvalidArgumentException;
15
16
class Scripts extends _Lib
17
{
18
    public function __construct()
19
    {
20
        parent::__construct();
21
    }
22
23
    /**
24
     * @throws ConfigNotFoundException
25
     * @throws EnvironmentException
26
     * @throws KeyNotFoundException
27
     * @throws InvalidArgumentException
28
     */
29
    public static function dockerBuild()
30
    {
31
        $build = new Scripts();
32
        $build->execDockerBuild();
33
    }
34
35
    /**
36
     * @throws ConfigNotFoundException
37
     * @throws EnvironmentException
38
     * @throws KeyNotFoundException
39
     * @throws InvalidArgumentException
40
     */
41
    public static function dockerRun()
42
    {
43
        $build = new Scripts();
44
        $build->execDockerRun();
45
    }
46
47
    /**
48
     * @param Event $event
0 ignored issues
show
Bug introduced by
The type Composer\Script\Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
     * @throws ConfigNotFoundException
50
     * @throws EnvironmentException
51
     * @throws KeyNotFoundException
52
     * @throws InvalidMigrationFile
53
     * @throws InvalidArgumentException
54
     */
55
    public static function migrate(Event $event)
56
    {
57
        $migrate = new Scripts();
58
        $migrate->runMigrate($event->getArguments());
59
    }
60
61
    /**
62
     * @throws ConfigNotFoundException
63
     * @throws EnvironmentException
64
     * @throws KeyNotFoundException
65
     * @throws InvalidArgumentException
66
     */
67
    public static function genRestDocs()
68
    {
69
        $build = new Scripts();
70
        $build->runGenRestDocs();
71
    }
72
73
74
    /**
75
     * @throws ConfigNotFoundException
76
     * @throws EnvironmentException
77
     * @throws KeyNotFoundException
78
     * @throws InvalidArgumentException
79
     */
80
    public function execDockerBuild()
81
    {
82
        $build = Psr11::container()->get('BUILDER_DOCKER_BUILD');
83
84
        if (empty($build)) {
85
            return;
86
        }
87
88
        $this->liveExecuteCommand($build);
89
    }
90
91
    /**
92
     * @throws ConfigNotFoundException
93
     * @throws EnvironmentException
94
     * @throws KeyNotFoundException
95
     * @throws InvalidArgumentException
96
     */
97
    public function execDockerRun()
98
    {
99
        $deployCommand = Psr11::container()->get('BUILDER_DOCKER_RUN');
100
101
        if (empty($deployCommand)) {
102
            return;
103
        }
104
105
        $this->liveExecuteCommand($deployCommand);
106
    }
107
108
109
    /**
110
     * @param $arguments
111
     * @throws ConfigNotFoundException
112
     * @throws EnvironmentException
113
     * @throws KeyNotFoundException
114
     * @throws InvalidMigrationFile
115
     * @throws InvalidArgumentException
116
     */
117
    public function runMigrate($arguments)
118
    {
119
        $dbConnection = Psr11::container()->get('DBDRIVER_CONNECTION');
120
121
        $migration = new Migration(new Uri($dbConnection), $this->workdir . "/db");
122
        $migration->registerDatabase("mysql", MySqlDatabase::class);
123
        $migration->addCallbackProgress(function ($cmd, $version) {
124
            echo "Doing $cmd, $version\n";
125
        });
126
127
        $argumentList = $this->extractArguments($arguments);
128
129
        $exec['reset'] = function () use ($migration, $argumentList) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
$exec was never initialized. Although not strictly required by PHP, it is generally a good practice to add $exec = array(); before regardless.
Loading history...
130
            if (!$argumentList["--yes"]) {
131
                throw new Exception("Reset require the argument --yes");
132
            }
133
            $migration->prepareEnvironment();
134
            $migration->reset();
135
        };
136
137
138
        $exec["update"] = function () use ($migration, $argumentList) {
139
            $migration->update($argumentList["--up-to"], $argumentList["--force"]);
140
        };
141
142
        if (isset($exec[$argumentList['command']])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $exec seems to never exist and therefore isset should always be false.
Loading history...
143
            $exec[$argumentList['command']]();
144
        }
145
    }
146
147
    /**
148
     * @param $arguments
149
     * @param bool $hasCmd
150
     * @return array
151
     */
152
    protected function extractArguments($arguments, $hasCmd = true) {
153
        $ret = [
154
            '--up-to' => null,
155
            '--yes' => null,
156
            '--force' => false,
157
        ];
158
159
        $start = 0;
160
        if ($hasCmd) {
161
            $ret['command'] = isset($arguments[0]) ? $arguments[0] : null;
162
            $start = 1;
163
        }
164
165
        for ($i=$start; $i < count($arguments); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
166
            $args = explode("=", $arguments[$i]);
167
            $ret[$args[0]] = isset($args[1]) ? $args[1] : true;
168
        }
169
170
        return $ret;
171
    }
172
173
    /**
174
     * @throws ConfigNotFoundException
175
     * @throws EnvironmentException
176
     * @throws KeyNotFoundException
177
     * @throws InvalidArgumentException
178
     */
179
    public function runGenRestDocs()
180
    {
181
        $docPath = $this->workdir . '/web/docs/';
182
        chdir($this->workdir);
183
        $this->liveExecuteCommand(
184
            $this->fixDir("vendor/bin/swagger")
185
            . " --output \"$docPath\" "
186
            . "--exclude vendor "
187
            . "--exclude docker "
188
            . "--exclude fw "
189
            . "--processor OperationId"
190
        );
191
192
        $docs = file_get_contents("$docPath/swagger.json");
193
        $docs = str_replace('__HOSTNAME__', Psr11::container()->get('API_SERVER'), $docs);
194
        file_put_contents("$docPath/swagger.json", $docs);
195
    }
196
}
197