Passed
Push — test ( a68431...c65f5a )
by Tom
02:31
created

StepContainer::shutdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner;
6
7
use Ktomk\Pipelines\Cli\Docker;
8
use Ktomk\Pipelines\Cli\Exec;
9
use Ktomk\Pipelines\File\Pipeline\Step;
10
use Ktomk\Pipelines\Runner\Containers\NameBuilder;
11
12
/**
13
 * Class StepContainer
14
 *
15
 * @package Ktomk\Pipelines\Runner
16
 */
17
class StepContainer
18
{
19
    /**
20
     * @var null|string id of the (running) container
21
     */
22
    private $id;
23
24
    /**
25
     * @var null|string name of the container
26
     */
27
    private $name;
28
29
    /**
30
     * @var Step
31
     */
32
    private $step;
33
34
    /**
35
     * @var Runner
36
     */
37
    private $runner;
38
39
    /**
40
     * StepContainer constructor.
41
     *
42
     * @param string $name
43
     * @param Step $step
44
     * @param Exec $exec
45
     */
46 12
    public function __construct($name, Step $step, Runner $exec)
47
    {
48 12
        $this->name = $name;
49 12
        $this->step = $step;
50 12
        $this->runner = $exec;
51 12
    }
52
53
    /**
54
     * the display id
55
     *
56
     *   side-effect: if id is null, this signals a dry-run which is made
57
     * visible by the string "*dry-run*"
58
     *
59
     * @return string
60
     */
61 8
    public function getDisplayId()
62
    {
63 8
        return isset($this->id) ? $this->id : '*dry-run*';
64
    }
65
66
    /**
67
     * @return null|string ID of (once) running container or null if not yet running
68
     */
69 3
    public function getId()
70
    {
71 3
        return $this->id;
72
    }
73
74
    /**
75
     * @return null|string name of the container, NULL if no name generated yet
76
     */
77 5
    public function getName()
78
    {
79 5
        return $this->name;
80
    }
81
82
    /**
83
     * @param null|bool $keep a container on true, kill on false (if it exists)
84
     *
85
     * @return null|string
86
     */
87 7
    public function keepOrKill($keep = null)
88
    {
89 7
        $keep = null === $keep ? $this->runner->getFlags()->reuseContainer() : $keep;
90
91 7
        $name = $this->name;
92 7
        if (null === $name) {
93 1
            throw new \BadMethodCallException('Container has no name yet');
94
        }
95
96 6
        $processManager = Docker::create($this->runner->getExec())->getProcessManager();
97
98 6
        if (false === $keep) {
99 2
            $processManager->zapContainersByName($name);
100
101 2
            return $this->id = null;
102
        }
103
104 5
        return $this->id = $processManager->findContainerIdByName($name);
105
    }
106
107
    /**
108
     * @param bool $kill
109
     * @param bool $remove
110
     *
111
     * @return void
112
     */
113 2
    public function killAndRemove($kill, $remove)
114
    {
115 2
        $id = $this->getDisplayId();
116
117 2
        Containers::execKillAndRemove($this->runner->getExec(), $id, $kill, $remove);
118 2
    }
119
120
    /**
121
     * @param array $args
122
     *
123
     * @return array array(int $status, string $out, string $err)
124
     */
125 4
    public function run(array $args)
126
    {
127 4
        $execRun = Containers::execRun($this->runner->getExec(), $args);
128 4
        $this->id = array_pop($execRun);
129
130 4
        return $execRun;
131
    }
132
133
    /**
134
     * @param int $status
135
     *
136
     * @return void
137
     */
138 4
    public function shutdown($status)
139
    {
140 4
        $id = $this->getDisplayId();
141
142 4
        $message = sprintf(
143 4
            'keeping container id %s',
144 4
            (string)substr($id, 0, 12)
145
        );
146
147 4
        Containers::execShutdownContainer(
148 4
            $this->runner->getExec(),
149 4
            $this->runner->getStreams(),
150 4
            $id,
151 4
            $status,
152 4
            $this->runner->getFlags(),
153 4
            $message
154
        );
155 4
    }
156
}
157