Passed
Push — test ( bc3c71...6a6b84 )
by Tom
02:34
created

StepContainer::killAndRemove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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 Exec
36
     */
37
    private $exec;
38
39
    /**
40
     * @param string $name
41
     * @param Step $step
42
     * @param null|Exec $exec
43
     *
44
     * @return StepContainer
45
     */
46 1
    public static function create($name, Step $step, Exec $exec = null)
47
    {
48 1
        if (null === $exec) {
49 1
            $exec = new Exec();
50
        }
51
52 1
        return new self($name, $step, $exec);
53
    }
54
55
    /**
56
     * StepContainer constructor.
57
     *
58
     * @param string $name
59
     * @param Step $step
60
     * @param Exec $exec
61
     */
62 12
    public function __construct($name, Step $step, Exec $exec)
63
    {
64 12
        $this->name = $name;
65 12
        $this->step = $step;
66 12
        $this->exec = $exec;
67 12
    }
68
69
    /**
70
     * the display id
71
     *
72
     *   side-effect: if id is null, this signals a dry-run which is made
73
     * visible by the string "*dry-run*"
74
     *
75
     * @return string
76
     */
77 8
    public function getDisplayId()
78
    {
79 8
        return isset($this->id) ? $this->id : '*dry-run*';
80
    }
81
82
    /**
83
     * @return null|string ID of (once) running container or null if not yet running
84
     */
85 3
    public function getId()
86
    {
87 3
        return $this->id;
88
    }
89
90
    /**
91
     * @return null|string name of the container, NULL if no name generated yet
92
     */
93 5
    public function getName()
94
    {
95 5
        return $this->name;
96
    }
97
98
    /**
99
     * @param bool $keep a container on true, kill on false (if it exists)
100
     *
101
     * @return null|string
102
     */
103 7
    public function keepOrKill($keep)
104
    {
105 7
        $name = $this->name;
106 7
        if (null === $name) {
107 1
            throw new \BadMethodCallException('Container has no name yet');
108
        }
109
110 6
        $processManager = Docker::create($this->exec)->getProcessManager();
111
112 6
        if (false === $keep) {
113 2
            $processManager->zapContainersByName($name);
114
115 2
            return $this->id = null;
116
        }
117
118 5
        return $this->id = $processManager->findContainerIdByName($name);
119
    }
120
121
    /**
122
     * @param bool $kill
123
     * @param bool $remove
124
     *
125
     * @return void
126
     */
127 2
    public function killAndRemove($kill, $remove)
128
    {
129 2
        $id = $this->getDisplayId();
130
131 2
        StepContainers::execKillAndRemove($this->exec, $id, $kill, $remove);
132 2
    }
133
134
    /**
135
     * @param array $args
136
     *
137
     * @return array array(int $status, string $out, string $err)
138
     */
139 4
    public function run(array $args)
140
    {
141 4
        $execRun = StepContainers::execRun($this->exec, $args);
142 4
        $this->id = array_pop($execRun);
143
144 4
        return $execRun;
145
    }
146
}
147