ConfiguredContainerIds::findAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Dock\Docker\Compose;
4
5
use Dock\IO\ProcessRunner;
6
7
class ConfiguredContainerIds implements \Dock\Docker\Containers\ConfiguredContainerIds
8
{
9
    /**
10
     * @var ProcessRunner
11
     */
12
    private $processRunner;
13
14
    public function __construct(ProcessRunner $processRunner)
15
    {
16
        $this->processRunner = $processRunner;
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function findAll()
23
    {
24
        $rawOutput = $this->processRunner->run('docker-compose ps -q')->getOutput();
25
26
        return $this->parseOutput($rawOutput);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function findByName($name)
33
    {
34
        $rawOutput = $this->processRunner->run('docker-compose ps -q '.$name)->getOutput();
35
        $ids = $this->parseOutput($rawOutput);
36
37
        if (count($ids) == 0) {
38
            throw new \RuntimeException(sprintf(
39
                'No container named "%s" found',
40
                $name
41
            ));
42
        }
43
44
        return current($ids);
45
    }
46
47
    /**
48
     * @param string $output
49
     *
50
     * @return array
51
     */
52
    private function parseOutput($output)
53
    {
54
        $lines = explode("\n", $output);
55
        $containerIds = [];
56
57
        foreach ($lines as $line) {
58
            $line = trim($line);
59
            if (!empty($line)) {
60
                $containerIds[] = $line;
61
            }
62
        }
63
64
        return $containerIds;
65
    }
66
}
67