Completed
Pull Request — master (#5)
by Joao
04:26 queued 02:49
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\DbMigration\Database\MySqlDatabase;
6
use ByJG\DbMigration\Migration;
7
use ByJG\Util\Uri;
8
use Composer\Script\Event;
9
10
class Scripts extends _Lib
11
{
12
    public function __construct()
13
    {
14
        parent::__construct();
15
    }
16
17
    /**
18
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
19
     * @throws \ByJG\Config\Exception\EnvironmentException
20
     * @throws \ByJG\Config\Exception\KeyNotFoundException
21
     * @throws \Psr\SimpleCache\InvalidArgumentException
22
     */
23
    public static function dockerBuild()
24
    {
25
        $build = new Scripts();
26
        $build->execDockerBuild();
27
    }
28
29
    /**
30
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
31
     * @throws \ByJG\Config\Exception\EnvironmentException
32
     * @throws \ByJG\Config\Exception\KeyNotFoundException
33
     * @throws \Psr\SimpleCache\InvalidArgumentException
34
     */
35
    public static function dockerRun()
36
    {
37
        $build = new Scripts();
38
        $build->execDockerRun();
39
    }
40
41
    /**
42
     * @param \Composer\Script\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...
43
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
44
     * @throws \ByJG\Config\Exception\EnvironmentException
45
     * @throws \ByJG\Config\Exception\KeyNotFoundException
46
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
47
     * @throws \Psr\SimpleCache\InvalidArgumentException
48
     */
49
    public static function migrate(Event $event)
50
    {
51
        $migrate = new Scripts();
52
        $migrate->runMigrate($event->getArguments());
53
    }
54
55
    /**
56
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
57
     * @throws \ByJG\Config\Exception\EnvironmentException
58
     * @throws \ByJG\Config\Exception\KeyNotFoundException
59
     * @throws \Psr\SimpleCache\InvalidArgumentException
60
     */
61
    public static function genRestDocs()
62
    {
63
        $build = new Scripts();
64
        $build->runGenRestDocs();
65
    }
66
67
68
    /**
69
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
70
     * @throws \ByJG\Config\Exception\EnvironmentException
71
     * @throws \ByJG\Config\Exception\KeyNotFoundException
72
     * @throws \Psr\SimpleCache\InvalidArgumentException
73
     */
74
    public function execDockerBuild()
75
    {
76
        $build = Psr11::container()->get('BUILDER_DOCKER_BUILD');
77
78
        if (empty($build)) {
79
            return;
80
        }
81
82
        $this->liveExecuteCommand($build);
83
    }
84
85
    /**
86
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
87
     * @throws \ByJG\Config\Exception\EnvironmentException
88
     * @throws \ByJG\Config\Exception\KeyNotFoundException
89
     * @throws \Psr\SimpleCache\InvalidArgumentException
90
     */
91
    public function execDockerRun()
92
    {
93
        $deployCommand = Psr11::container()->get('BUILDER_DOCKER_RUN');
94
95
        if (empty($deployCommand)) {
96
            return;
97
        }
98
99
        $this->liveExecuteCommand($deployCommand);
100
    }
101
102
103
    /**
104
     * @param $arguments
105
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
106
     * @throws \ByJG\Config\Exception\EnvironmentException
107
     * @throws \ByJG\Config\Exception\KeyNotFoundException
108
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
109
     * @throws \Psr\SimpleCache\InvalidArgumentException
110
     */
111
    public function runMigrate($arguments)
112
    {
113
        $dbConnection = Psr11::container()->get('DBDRIVER_CONNECTION');
114
115
        $migration = new Migration(new Uri($dbConnection), $this->workdir . "/db");
116
        $migration->registerDatabase("mysql", MySqlDatabase::class);
117
        $migration->addCallbackProgress(function ($cmd, $version) {
118
            echo "Doing $cmd, $version\n";
119
        });
120
121
        $argumentList = $this->extractArguments($arguments);
122
123
        $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...
124
            if (!$argumentList["--yes"]) {
125
                throw new \Exception("Reset require the argument --yes");
126
            }
127
            $migration->prepareEnvironment();
128
            $migration->reset();
129
        };
130
131
132
        $exec["update"] = function () use ($migration, $argumentList) {
133
            $migration->update($argumentList["--up-to"], $argumentList["--force"]);
134
        };
135
136
        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...
137
            $exec[$argumentList['command']]();
138
        }
139
    }
140
141
    /**
142
     * @param $arguments
143
     * @param bool $hasCmd
144
     * @return array
145
     */
146
    protected function extractArguments($arguments, $hasCmd = true) {
147
        $ret = [
148
            '--up-to' => null,
149
            '--yes' => null,
150
            '--force' => false,
151
        ];
152
153
        $start = 0;
154
        if ($hasCmd) {
155
            $ret['command'] = isset($arguments[0]) ? $arguments[0] : null;
156
            $start = 1;
157
        }
158
159
        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...
160
            $args = explode("=", $arguments[$i]);
161
            $ret[$args[0]] = isset($args[1]) ? $args[1] : true;
162
        }
163
164
        return $ret;
165
    }
166
167
    /**
168
     * @throws \ByJG\Config\Exception\ConfigNotFoundException
169
     * @throws \ByJG\Config\Exception\EnvironmentException
170
     * @throws \ByJG\Config\Exception\KeyNotFoundException
171
     * @throws \Psr\SimpleCache\InvalidArgumentException
172
     */
173
    public function runGenRestDocs()
174
    {
175
        $docPath = $this->workdir . '/web/docs/';
176
        chdir($this->workdir);
177
        $this->liveExecuteCommand(
178
            $this->fixDir("vendor/bin/swagger")
179
            . " --output \"$docPath\" "
180
            . "--exclude vendor "
181
            . "--exclude docker "
182
            . "--exclude fw "
183
            . "--processor OperationId"
184
        );
185
186
        $docs = file_get_contents("$docPath/swagger.json");
187
        $docs = str_replace('__HOSTNAME__', Psr11::container()->get('API_SERVER'), $docs);
188
        file_put_contents("$docPath/swagger.json", $docs);
189
    }
190
}
191