Passed
Push — test ( d6edfc...f7394f )
by Tom
02:30
created

Repository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 27
dl 0
loc 113
ccs 34
cts 34
cp 1
rs 10
c 1
b 0
f 1
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A asPackageArray() 0 8 2
A resolve() 0 9 1
A containerProvisionDockerClientBinary() 0 5 1
A getLocalBinary() 0 3 1
A inject() 0 5 1
A injectPackage() 0 4 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner\Docker\Binary;
6
7
use Ktomk\Pipelines\Cli\Exec;
8
use Ktomk\Pipelines\Lib;
9
use Ktomk\Pipelines\Runner\Directories;
10
11
/**
12
 * Docker Binary Repository
13
 *
14
 * Docker provides a static binary which is useful to have in containers when
15
 * in the container itself docker should be available (e.g. services: - docker).
16
 *
17
 * It "comes" out of this binary repository. In the background there is a
18
 * BinaryUnPackager taking care of network interaction and file verification
19
 * as it is technically possible to have different kind of static docker
20
 * binaries also from different packages.
21
 *
22
 * Also there is a YAML package file reader which can read package information
23
 * out of such files.
24
 *
25
 * @package Ktomk\Pipelines\Runner\Docker
26
 */
27
class Repository implements PackageInterface
28
{
29
    /**
30
     * @var Exec
31
     */
32
    private $exec;
33
34
    /**
35
     * @var null|array package definition data
36
     */
37
    private $package;
38
39
    /**
40
     * @var null|UnPackager
41
     */
42
    private $unPackager;
43
44
    /**
45
     * Repository constructor.
46
     *
47
     * @param Exec $exec
48
     * @param Directories $directories (UnPackager dependency)
49
     * @param array $package [optional]
50
     * @param UnPackager $unPackager [optional]
51
     */
52 2
    public function __construct(Exec $exec, Directories $directories, array $package = null, UnPackager $unPackager = null)
53
    {
54 2
        $this->exec = $exec;
55 2
        $this->package = $package;
56 2
        if (null === $unPackager) {
57 1
            $unPackager = UnPackager::fromDirectories($this->exec, $directories);
58
        }
59 2
        $this->unPackager = $unPackager;
60 2
    }
61
62
    /**
63
     * provision docker client into a running container
64
     *
65
     * install a binary as /usr/bin/docker and make it executable.
66
     * show the version.
67
     *
68
     * @param string $containerId
69
     * @param string $path to static docker client binary
70
     */
71 1
    public function containerProvisionDockerClientBinary($containerId, $path)
72
    {
73 1
        $this->exec->pass(
74 1
            sprintf('2>&1 < %s docker', lib::quoteArg($path)),
75 1
            array('exec', '-i', $containerId, '/bin/sh', '-c', 'mkdir -p /usr/bin && cat - > /usr/bin/docker; chmod +x /usr/bin/docker; docker --version')
76
        );
77 1
    }
78
79
    /**
80
     * Resolve a binary package name in this repository
81
     *
82
     * @param string $packageName
83
     * @return Repository
84
     */
85 5
    public function resolve($packageName)
86
    {
87 5
        $packageDir = __DIR__ . '/../../../../lib/package';
88 5
        $reader = new PackageYamlFileReader(
89 5
            sprintf('%s/%s.yml', $packageDir, $packageName)
90
        );
91 5
        $this->package = $reader->asPackageArray();
92
93 4
        return $this;
94
    }
95
96
    /**
97
     * @param string $containerId
98
     */
99 1
    public function inject($containerId)
100
    {
101 1
        $package = $this->asPackageArray();
102 1
        $localBinary = $this->getLocalBinary($package);
103 1
        $this->containerProvisionDockerClientBinary($containerId, $localBinary);
104 1
    }
105
106
    /**
107
     * Inject binary docker client package by name into container
108
     *
109
     * @param string $name
110
     * @param string $containerId
111
     */
112 1
    public function injectPackage($name, $containerId)
113
    {
114 1
        $this->resolve($name);
115 1
        $this->inject($containerId);
116 1
    }
117
118
    /**
119
     * Get binary path from local store.
120
     *
121
     * @param array $package
122
     * @return string
123
     */
124 1
    public function getLocalBinary(array $package)
125
    {
126 1
        return $this->unPackager->getLocalBinary($package);
0 ignored issues
show
Bug introduced by
The method getLocalBinary() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        return $this->unPackager->/** @scrutinizer ignore-call */ getLocalBinary($package);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132 3
    public function asPackageArray()
133
    {
134 3
        $package = $this->package;
135 3
        if (null === $package) {
136 1
            $package = $this->resolve('docker-19.03.1-linux-static-x86_64')->package;
137
        }
138
139 3
        return $package;
140
    }
141
}
142