Passed
Push — test ( 5f8c8d...ebb7cd )
by Tom
02:58
created

ProcessManager::findContainerIdByName()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 4
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Cli\Docker;
6
7
use Ktomk\Pipelines\Cli\Exec;
8
use Ktomk\Pipelines\Lib;
9
10
/**
11
 * Process manager for docker container
12
 */
13
class ProcessManager
14
{
15
    /**
16
     * @var Exec
17
     */
18
    private $exec;
19
20 7
    public function __construct(Exec $exec)
21
    {
22 7
        $this->exec = $exec;
23 7
    }
24
25
    /**
26
     * @param string $name
27
     *
28
     * @return null|array|string[] 0 or more ids, null if subsystem
29
     *         unavailable, subsystem error or unsupported parameter
30
     */
31 1
    public function findAllContainerIdsByName($name)
32
    {
33 1
        $ids = null;
34
35 1
        $status = $this->exec->capture(
36 1
            'docker',
37
            array(
38 1
                'ps', '--no-trunc', '-qa', '--filter',
39 1
                "name=^/\\Q${name}\\E$",
40
            ),
41
            $result
42
        );
43
44
        $status || $ids = Lib::lines($result);
45
46
        return $ids;
47
    }
48
49
    /**
50
     * container ids by name prefix of running and stopped containers
51
     *
52
     * @param string $prefix
53
     *
54
     * @return null|array of ids, null if an internal error occurred
55
     */
56
    public function findAllContainerIdsByNamePrefix($prefix)
57
    {
58 2
        return $this->psPrefixImpl($prefix, true);
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return null|string
65
     */
66
    public function findContainerIdByName($name)
67
    {
68 4
        $ids = $this->findAllContainerIdsByName($name);
69 4
        if (null === $ids || !(is_array($ids) && 1 === count($ids))) {
70 3
            return null;
71
        }
72
73 1
        return $ids[0];
74
    }
75
76
    /**
77
     * container ids by name prefix of running containers
78
     *
79
     * @param string $prefix
80
     *
81
     * @return null|array of ids, null if an internal error occurred
82
     */
83
    public function findRunningContainerIdsByNamePrefix($prefix)
84
    {
85 1
        return $this->psPrefixImpl($prefix);
86
    }
87
88
    /**
89
     * @param string|string[] $idOrIds
90
     *
91
     * @return int
92
     */
93
    public function kill($idOrIds)
94
    {
95 1
        return $this->exec->capture('docker', Lib::merge('kill', func_get_args()));
96
    }
97
98
    /**
99
     * @param string|string[] $idOrIds
100
     *
101
     * @return int
102
     */
103
    public function remove($idOrIds)
104
    {
105 1
        return $this->exec->capture('docker', Lib::merge('rm', func_get_args()));
106
    }
107
108
    /**
109
     * @param string $name
110
     */
111
    public function zapContainersByName($name)
112
    {
113 2
        $ids = $this->findAllContainerIdsByName($name);
114 2
        if (!$ids) {
115 1
            return;
116
        }
117 1
        $this->zap($ids);
118 1
    }
119
120
    /**
121
     * @param string|string[] $idOrIds
122
     */
123
    public function zap($idOrIds)
124
    {
125 1
        $this->kill($idOrIds);
126 1
        $this->remove($idOrIds);
127 1
    }
128
129
    /**
130
     * @param string $prefix
131
     * @param bool $all
132
     *
133
     * @return null|array
134
     */
135
    private function psPrefixImpl($prefix, $all = false)
136
    {
137 3
        $ids = null;
138
139 3
        if (0 === preg_match('(^[a-zA-Z0-9][a-zA-Z0-9_.-]+$)', $prefix)) {
140 1
            throw new \InvalidArgumentException(
141 1
                sprintf('Invalid container name prefix "%s"', $prefix)
142
            );
143
        }
144
145 2
        $status = $this->exec->capture(
146 2
            'docker',
147
            array(
148 2
                'ps', '-q' . ($all ? 'a' : ''), '--no-trunc', '--filter',
149 2
                "name=^/${prefix}",
150
            ),
151 2
            $result
152
        );
153
154 2
        $status || $ids = Lib::lines($result);
155
156 2
        return $ids;
157
    }
158
}
159