HostMountFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDocker() 0 14 4
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Host;
4
5
use Assert\Assertion;
6
use Cocotte\Filesystem\Filesystem;
7
8
final class HostMountFactory
9
{
10
    /**
11
     * @var MountsFactory
12
     */
13
    private $mountsFactory;
14
15
    /**
16
     * @var Filesystem
17
     */
18
    private $filesystem;
19
20
    public function __construct(MountsFactory $mountsFactory, Filesystem $filesystem)
21
    {
22
        $this->mountsFactory = $mountsFactory;
23
        $this->filesystem = $filesystem;
24
    }
25
26
    /**
27
     * @return HostMount
28
     * @throws \Assert\AssertionFailedException
29
     * @throws HostException
30
     */
31
    public function fromDocker(): HostMount
32
    {
33
        foreach ($this->mountsFactory->fromDocker()->toArray() as $mount) {
34
            if ('bind' === $mount['Type'] && '/host' === $mount['Destination']) {
35
                Assertion::true($mount['RW'], "Volume /host must be writable");
36
                Assertion::true(
37
                    $this->filesystem->isAbsolutePath($mount['Source']),
38
                    "Host mount source '{$mount['Source']}' is not an absolute path"
39
                );
40
41
                return new HostMount($mount);
42
            }
43
        }
44
        throw HostException::noHostMount();
45
    }
46
}