Passed
Push — master ( 71d777...1cedb5 )
by Tom
04:21
created

DockerOptions::runClean()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\Cli\Args;
9
use Ktomk\Pipelines\Cli\DockerProcessManager;
10
use Ktomk\Pipelines\Cli\Exec;
11
use Ktomk\Pipelines\Cli\Streams;
12
use Ktomk\Pipelines\Lib;
13
use RuntimeException;
14
15
/**
16
 * docker specific options of the pipelines utility
17
 *
18
 * --docker-list
19
 * --docker-kill
20
 * --docker-clean
21
 * --docker-zap
22
 *
23
 * @package Ktomk\Pipelines\Utility
24
 */
25
class DockerOptions
26
{
27
    /**
28
     * @var Streams
29
     */
30
    private $streams;
31
32
    /**
33
     * @var Args
34
     */
35
    private $args;
36
37
    /**
38
     * @var Exec
39
     */
40
    private $exec;
41
42
    /**
43
     * @var string
44
     */
45
    private $prefix;
46
47
    /**
48
     * @var DockerProcessManager
49
     */
50
    private $ps;
51
52 1
    public static function bind(Args $args, Exec $exec, $prefix, Streams $streams)
53
    {
54 1
        return new self($args, $exec, $prefix, $streams, new DockerProcessManager($exec));
55
    }
56
57
    /**
58
     * DockerOptions constructor.
59
     *
60
     * @param Args $args
61
     * @param Exec $exec
62
     * @param string $prefix
63
     * @param Streams $streams
64
     * @param DockerProcessManager $ps
65
     */
66 5
    public function __construct(Args $args, Exec $exec, $prefix, Streams $streams, DockerProcessManager $ps)
67
    {
68 5
        $this->args = $args;
69 5
        $this->exec = $exec;
70 5
        $this->prefix = $prefix;
71 5
        $this->streams = $streams;
72 5
        $this->ps = $ps;
73 5
    }
74
75
    /**
76
     * Process docker related options
77
     *
78
     * --docker-list  - list containers
79
     * --docker-kill  - kill (running) containers
80
     * --docker-clean - remove (stopped) containers
81
     *
82
     * @throws InvalidArgumentException
83
     * @throws RuntimeException
84
     * @throws StatusException
85
     */
86 4
    public function run()
87
    {
88 4
        $this->parse($this->args, $this->prefix);
89 1
    }
90
91
    /**
92
     * @param Args $args
93
     * @param string $prefix
94
     * @throws StatusException
95
     */
96 4
    private function parse(Args $args, $prefix)
97
    {
98 4
        $count = 0;
99 4
        $status = 0;
100
101 4
        if ($args->hasOption('docker-list')) {
102 2
            $count++;
103 2
            $status = $this->runPs();
104
        }
105
106 4
        $hasKill = $args->hasOption('docker-kill');
107 4
        $hasClean = $args->hasOption('docker-clean');
108 4
        if ($args->hasOption('docker-zap')) {
109 1
            $hasKill = $hasClean = true;
110
        }
111
112 4
        $ids = $this->runGetIds($count, $hasClean || $hasKill, $prefix);
113
114 4
        $status = $this->runKill($status, $count, $hasKill, $prefix);
115
116 4
        $status = $this->runClean($status, $count, $hasClean, $ids);
117
118 4
        if ($count) {
119 3
            StatusException::status($status);
120
        }
121 1
    }
122
123 2
    private function info($message)
124
    {
125 2
        $this->streams->out(
126 2
            sprintf("%s\n", $message)
127
        );
128 2
    }
129
130
    /**
131
     * @return int
132
     */
133 2
    private function runPs()
134
    {
135 2
        $prefix = $this->prefix;
136
137 2
        return $this->exec->pass(
138 2
            'docker ps -a',
139
            array(
140 2
                '--filter',
141 2
                "name=^/${prefix}-"
142
            )
143
        );
144
    }
145
146
    /**
147
     * @param $flag
148
     * @param $count
149
     * @param $prefix
150
     * @return null|array
151
     */
152
    private function runGetIds(&$count, $flag, $prefix)
153
    {
154 4
        if (!$flag) {
155 1
            return null;
156
        }
157
158 3
        $count++;
159
160 3
        return $this->ps->findAllContainerIdsByNamePrefix($prefix . '-');
161
    }
162
163
    /**
164
     * @param int $status
165
     * @param int $count
166
     * @param bool $hasKill
167
     * @param string $prefix
168
     * @return int
169
     */
170
    private function runKill($status, &$count, $hasKill, $prefix)
171
    {
172 4
        if ($status || !$hasKill) {
173 1
            return $status;
174
        }
175
176 3
        if (!$status && $hasKill) {
177 3
            $count++;
178 3
            $running = $this->ps->findRunningContainerIdsByNamePrefix($prefix . '-');
179 3
            if ($running) {
180 1
                $status = $this->ps->kill($running);
181
            } else {
182 2
                $this->info('no containers to kill');
183
            }
184
        }
185
186 3
        return $status;
187
    }
188
189
    /**
190
     * @param int $status
191
     * @param int $count
192
     * @param bool $hasClean
193
     * @param array $ids
194
     * @return int
195
     */
196
    private function runClean($status, &$count, $hasClean, array $ids = null)
197
    {
198 4
        if ($status || !$hasClean) {
199 1
            return $status;
200
        }
201
202 3
        $count++;
203 3
        if ($ids) {
204 1
            $status = $this->ps->remove($ids);
205
        } else {
206 2
            $this->info('no containers to remove');
207
        }
208
209 3
        return $status;
210
    }
211
}
212