Passed
Pull Request — master (#5)
by Joao
01:53
created

Scripts::extractArguments()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 19
rs 9.5555
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
     * @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...
25
     * @throws ConfigNotFoundException
26
     * @throws EnvironmentException
27
     * @throws KeyNotFoundException
28
     * @throws InvalidMigrationFile
29
     * @throws InvalidArgumentException
30
     */
31
    public static function migrate(Event $event)
32
    {
33
        $migrate = new Scripts();
34
        $migrate->runMigrate($event->getArguments());
35
    }
36
37
    /**
38
     * @throws ConfigNotFoundException
39
     * @throws EnvironmentException
40
     * @throws KeyNotFoundException
41
     * @throws InvalidArgumentException
42
     */
43
    public static function genRestDocs()
44
    {
45
        $build = new Scripts();
46
        $build->runGenRestDocs();
47
    }
48
49
    /**
50
     * @param $arguments
51
     * @throws ConfigNotFoundException
52
     * @throws EnvironmentException
53
     * @throws KeyNotFoundException
54
     * @throws InvalidMigrationFile
55
     * @throws InvalidArgumentException
56
     */
57
    public function runMigrate($arguments)
58
    {
59
        $dbConnection = Psr11::container()->get('DBDRIVER_CONNECTION');
60
61
        $migration = new Migration(new Uri($dbConnection), $this->workdir . "/db");
62
        $migration->registerDatabase("mysql", MySqlDatabase::class);
63
        $migration->addCallbackProgress(function ($cmd, $version) {
64
            echo "Doing $cmd, $version\n";
65
        });
66
67
        $argumentList = $this->extractArguments($arguments);
68
69
        $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...
70
            if (!$argumentList["--yes"]) {
71
                throw new Exception("Reset require the argument --yes");
72
            }
73
            $migration->prepareEnvironment();
74
            $migration->reset();
75
        };
76
77
78
        $exec["update"] = function () use ($migration, $argumentList) {
79
            $migration->update($argumentList["--up-to"], $argumentList["--force"]);
80
        };
81
82
        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...
83
            $exec[$argumentList['command']]();
84
        }
85
    }
86
87
    /**
88
     * @param $arguments
89
     * @param bool $hasCmd
90
     * @return array
91
     */
92
    protected function extractArguments($arguments, $hasCmd = true) {
93
        $ret = [
94
            '--up-to' => null,
95
            '--yes' => null,
96
            '--force' => false,
97
        ];
98
99
        $start = 0;
100
        if ($hasCmd) {
101
            $ret['command'] = isset($arguments[0]) ? $arguments[0] : null;
102
            $start = 1;
103
        }
104
105
        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...
106
            $args = explode("=", $arguments[$i]);
107
            $ret[$args[0]] = isset($args[1]) ? $args[1] : true;
108
        }
109
110
        return $ret;
111
    }
112
113
    /**
114
     * @throws ConfigNotFoundException
115
     * @throws EnvironmentException
116
     * @throws KeyNotFoundException
117
     * @throws InvalidArgumentException
118
     */
119
    public function runGenRestDocs()
120
    {
121
        $docPath = $this->workdir . '/web/docs/';
122
        chdir($this->workdir);
123
        $this->liveExecuteCommand(
124
            $this->fixDir("vendor/bin/swagger")
125
            . " --output \"$docPath\" "
126
            . "--exclude vendor "
127
            . "--exclude docker "
128
            . "--exclude fw "
129
            . "--processor OperationId"
130
        );
131
132
        $docs = file_get_contents("$docPath/swagger.json");
133
        $docs = str_replace('__HOSTNAME__', Psr11::container()->get('API_SERVER'), $docs);
134
        file_put_contents("$docPath/swagger.json", $docs);
135
    }
136
}
137