HostMountFactory::fromDocker()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 14
rs 10
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
}