Passed
Push — master ( 308d02...325a65 )
by Christian
02:19
created

MountsFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Host;
4
5
class MountsFactory
6
{
7
    /**
8
     * @var array
9
     */
10
    private $mounts;
11
12
    /**
13
     * @var InspectMountsProcess
14
     */
15
    private $process;
16
17
    public function __construct(InspectMountsProcess $process)
18
    {
19
        $this->process = $process;
20
    }
21
22
    /**
23
     * @return Mounts
24
     * @throws HostException
25
     */
26
    public function fromDocker(): Mounts
27
    {
28
        if (null === $this->mounts) {
29
            $this->process->run();
30
            if (!$this->process->isSuccessful()) {
31
                $error = $this->process->getErrorOutput();
32
                if (false !== strpos($error, 'var/run/docker.sock')) {
33
                    throw HostException::noSocketMount($this->process->getErrorOutput());
34
                } else {
35
                    throw new HostException($error);
36
                }
37
            }
38
            $this->mounts = json_decode($this->process->getOutput(), true);
39
        }
40
41
        return new Mounts($this->mounts);
42
    }
43
44
}