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 ( e11e84...669d0f )
by Sam
02:18
created

BuildController::createBuildStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SamIT\Yii2\PhpFpm\controllers;
5
6
7
use Docker\API\Model\BuildInfo;
8
use Docker\API\Model\PushImageInfo;
9
use Docker\Context\Context;
10
use Docker\Context\ContextBuilder;
11
use Docker\Context\ContextInterface;
12
use Docker\Docker;
13
use Docker\Stream\BuildStream;
14
use Psr\Http\Message\ResponseInterface;
15
use SamIT\Yii2\PhpFpm\Module;
16
use yii\base\InvalidConfigException;
17
use yii\console\Controller;
18
use yii\helpers\Console;
19
use yii\web\Response;
20
use function Clue\StreamFilter\fun;
21
22
/**
23
 * Class BuildController
24
 * @package SamIT\Yii2\PhpFpm\controllers
25
 * @property Module $module
26
 */
27
class BuildController extends Controller
28
{
29
    public $defaultAction = 'build';
30
31
    /**
32
     * @var string The name of the created image
33
     * If not explicitly set will take its default from module config.
34
     */
35
    public $image;
36
37
    /**
38
     * @var string The tag of the created image
39
     * If not explicitly set will take its default from module config.
40
     */
41
    public $tag;
42
43
    /**
44
     * @var bool whether to push the image after a successful build.
45
     * If not explicitly set will take its default from module config.
46
     */
47
    public $push;
48
49
    /**
50
     * @var Docker
51
     */
52
    protected $docker;
53
54
    /**
55
     * @var string the user to authenticate against the repository
56
     */
57
    public $user;
58
59
    /**
60
     * @var string the password to authenticate against the repository
61
     */
62
    public $password;
63 3
64
    public function init(): void
65 3
    {
66 3
        parent::init();
67 3
        $this->docker = Docker::create();
68 3
        $this->push = $this->module->push;
69 3
        $this->image = $this->module->image;
70 3
        $this->tag = $this->module->tag;
71
    }
72 2
73
    public function actionBuild(): void
74
    {
75
76 2
77 2
        $params = [];
78 2
        if (isset($this->image)) {
79 2
            $name = "{$this->image}:{$this->tag}";
80
            $params['t'] = $name;
81 2
        }
82
        $buildStream = $this->createBuildStream($params);
83 2
84 2
        $buildStream->onFrame(function(BuildInfo $buildInfo): void {
85 2
            echo $buildInfo->getStream();
86
        });
87 2
88 2
        $buildStream->wait();
89 2
        echo "Wait finished\n";
90
        $buildStream->wait();
91 2
92 2
        if ($this->push) {
93 1
            if (!isset($name, $this->user, $this->password)) {
94
                throw new InvalidConfigException("When using the push option, you must configure or provide user, password and image");
95
            }
96 1
            $params = [
97 1
                'X-Registry-Auth' => \base64_encode(\GuzzleHttp\json_encode([
98 1
                    'username' => $this->user,
99
                    'password' => $this->password
100
                ]))
101 1
            ];
102
            $pushResult = $this->docker->imagePush($name, $params ?? [], Docker::FETCH_OBJECT);
103 1
104
            if ($pushResult instanceof ResponseInterface) {
105
                throw new \Exception($pushResult->getReasonPhrase() . ':' . $pushResult->getBody()->getContents(), $pushResult->getStatusCode());
106
            }
107 1
            /** @var PushImageInfo $pushInfo */
108
            $pushInfo = \array_pop($pushResult);
109 1
110 1
            if (!empty($pushInfo->getError())) {
111
                throw new \Exception($pushInfo->getError());
112
            }
113
114
        }
115
    }
116 3
117
    public function actionTestClient(): void
118
    {
119 3
        $this->stdout("It seems the console client works!\n", Console::FG_GREEN);
120
    }
121
122 3
    public function createBuildStream(array $params = []): BuildStream
123 3
    {
124
125
        $context = $this->module->createBuildContext();
126 1
127
        /** @var BuildStream $buildStream */
128
        $buildStream = $this->docker->imageBuild($context->toStream(), $params, Docker::FETCH_STREAM);
129 1
        return $buildStream;
130
    }
131 1
132 1
    public function options($actionID)
133 1
    {
134 1
135 1
        $result = parent::options($actionID);
136 1
        switch ($actionID) {
137 1
            case 'build':
138
                $result[] = 'push';
139
                $result[] = 'image';
140 1
                $result[] = 'tag';
141
                $result[] = 'user';
142
                $result[] = 'password';
143
                break;
144
145
        }
146
        return $result;
147
    }
148
149
    public function optionAliases()
150
    {
151
        $result = parent::optionAliases();
152
        $result['p'] = 'push';
153
        $result['t'] = 'tag';
154
        $result['i'] = 'image';
155
        $result['u'] = 'user';
156
        $result['P'] = 'password';
157
        return $result;
158
    }
159
160
161
}