GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7dd1a3...36003d )
by Sam
03:06
created

BuildController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SamIT\Yii2\PhpFpm\controllers;
5
6
7
use Docker\API\Model\AuthConfig;
8
use Docker\API\Model\BuildInfo;
9
use Docker\API\Model\PushImageInfo;
10
use Docker\Docker;
11
use Docker\Stream\BuildStream;
12
use Docker\Stream\PushStream;
13
use Psr\Http\Message\ResponseInterface;
14
use SamIT\Yii2\PhpFpm\Module;
15
use yii\base\InvalidConfigException;
16
use yii\console\Controller;
17
use yii\helpers\Console;
18
19
/**
20
 * Class BuildController
21
 * @package SamIT\Yii2\PhpFpm\controllers
22
 * @property Module $module
23
 */
24
class BuildController extends Controller
25
{
26
    public $defaultAction = 'build';
27
28
    /**
29
     * @var string The name of the created image
30
     * If not explicitly set will take its default from module config.
31
     */
32
    public $image;
33
34
    /**
35
     * @var string The tag of the created image
36
     * If not explicitly set will take its default from module config.
37
     */
38
    public $tag;
39
40
    /**
41
     * @var bool whether to push the image after a successful build.
42
     * If not explicitly set will take its default from module config.
43
     */
44
    public $push;
45
46
    /**
47
     * @var Docker
48
     */
49
    protected $docker;
50
51
    /**
52
     * @var string the user to authenticate against the repository
53
     */
54
    public $user;
55
56
    /**
57
     * @var string the password to authenticate against the repository
58
     */
59
    public $password;
60
61 3
    public function init(): void
62
    {
63 3
        parent::init();
64 3
        $this->docker = Docker::create();
65 3
        $this->push = $this->module->push;
66 3
        $this->image = $this->module->image;
67 3
        $this->tag = $this->module->tag;
68 3
    }
69
70 2
    public function actionBuild(): void
71
    {
72 2
        if ($this->push && !isset($this->image, $this->user, $this->password)) {
73 1
            throw new InvalidConfigException("When using the push option, you must configure or provide user, password and image");
74
        }
75
76 1
        $params = [];
77
78
79 1
        if (isset($this->image)) {
80 1
            $params['t'] = "{$this->image}:{$this->tag}";
81
        }
82 1
        $buildStream = $this->createBuildStream($params);
83 1
        $this->color = true;
84 1
        $buildStream->onFrame(\Closure::fromCallable([$this, 'logBuildInfo']));
85 1
        $buildStream->wait();
86 1
        $this->stdout("Wait finished\n");
87 1
        $buildStream->wait();
88
89 1
        if ($this->push) {
90 1
            $this->pushImage();
91
        }
92
    }
93
94 1
    private function logBuildInfo(BuildInfo $buildInfo): void
95
    {
96 1
        $this->stdout($buildInfo->getStream(), Console::FG_CYAN);
97 1
        $this->stdout($buildInfo->getProgress(), Console::FG_YELLOW);
98 1
        $this->stdout($buildInfo->getStatus(), Console::FG_RED);
99 1
        if (!empty($buildInfo->getProgressDetail())) {
100
            $this->stdout($buildInfo->getProgressDetail()->getMessage(), Console::FG_YELLOW);
101
        }
102 1
        if (!empty($buildInfo->getErrorDetail())) {
103
            $this->stdout($buildInfo->getErrorDetail()->getCode() . ':' . $buildInfo->getErrorDetail()->getMessage(), Console::FG_YELLOW);
104
        }
105 1
        if (!is_null($buildInfo->getError())) {
106
            throw new \Exception($buildInfo->getError() . ':' . $buildInfo->getErrorDetail()->getMessage());
107
        }
108 1
    }
109
110
    /**
111
     * Push a docker container image
112
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
113
     * @throws \Exception
114
     */
115 1
    private function pushImage(): void
116
    {
117 1
        if (!isset($this->image)) {
118
            throw new \InvalidArgumentException("Image name must be configured when pushing images");
119
        }
120 1
        $name = "{$this->image}:{$this->tag}";
121
122 1
        $authConfig = new AuthConfig();
123 1
        $authConfig->setUsername($this->user);
124 1
        $authConfig->setPassword($this->password);
125
        $params = [
126 1
            'X-Registry-Auth' => $authConfig
127
        ];
128
        /** @var PushStream $pushStream */
129 1
        $pushStream = $this->docker->imagePush($name, [], $params ?? [],  Docker::FETCH_OBJECT);
130
131 1
        if ($pushStream instanceof ResponseInterface) {
132
            throw new \Exception($pushStream->getReasonPhrase() . ':' . $pushStream->getBody()->getContents(), $pushStream->getStatusCode());
133
        }
134
135
        $pushStream->onFrame(function(PushImageInfo $pushImageInfo): void {
136 1
            if (!empty($pushImageInfo->getError())) {
137 1
                throw new \Exception($pushImageInfo->getError());
138
            }
139 1
            $this->stdout($pushImageInfo->getProgress(), Console::FG_YELLOW);
140 1
            $this->stdout($pushImageInfo->getStatus(), Console::FG_RED);
141 1
        });
142 1
        $pushStream->wait();
143
    }
144
145
    public function actionTestClient(): void
146
    {
147
        $this->stdout("It seems the console client works!\n", Console::FG_GREEN);
148
    }
149
150 2
    public function createBuildStream(array $params = []): BuildStream
151
    {
152
153 2
        $context = $this->module->createBuildContext();
154 2
        return $this->docker->imageBuild($context->toStream(), $params, [], Docker::FETCH_OBJECT);
155
    }
156
157 1
    public function options($actionID)
158
    {
159
160 1
        $result = parent::options($actionID);
161
        switch ($actionID) {
162 1
            case 'build':
163 1
                $result[] = 'push';
164 1
                $result[] = 'image';
165 1
                $result[] = 'tag';
166 1
                $result[] = 'user';
167 1
                $result[] = 'password';
168 1
                break;
169
170
        }
171 1
        return $result;
172
    }
173
174
    public function optionAliases()
175
    {
176
        $result = parent::optionAliases();
177
        $result['p'] = 'push';
178
        $result['t'] = 'tag';
179
        $result['i'] = 'image';
180
        $result['u'] = 'user';
181
        $result['P'] = 'password';
182
        return $result;
183
    }
184
185 1 View Code Duplication
    public function stdout($string)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187 1
        if ($this->isColorEnabled()) {
188 1
            $args = \func_get_args();
189 1
            \array_shift($args);
190 1
            $string = Console::ansiFormat($string, $args);
191
        }
192
193 1
        echo $string;
194 1
        return \strlen($string);
195
    }
196
197
198
}