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

MountsFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDocker() 0 16 4
A __construct() 0 3 1
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
}