Passed
Push — test ( b9d7c5...32b011 )
by Tom
02:29
created

Repository::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
     * Static factory method.
46
     *
47
     * @param Exec $exec
48
     * @param Directories $directories (UnPackager dependency)
49
     * @return Repository
50
     */
51 1
    public static function create(Exec $exec, Directories $directories)
52
    {
53 1
        $unPackager = UnPackager::fromDirectories($exec, $directories);
54
55 1
        return new self($exec, array(), $unPackager);
56
    }
57
58
    /**
59
     * Repository constructor.
60
     *
61
     * @param Exec $exec
62
     * @param Directories $directories (UnPackager dependency) [on removal]
63
     * @param array $package [optional - on removal]
64
     * @param UnPackager $unPackager
65
     */
66 2
    public function __construct(Exec $exec, array $package, UnPackager $unPackager)
67
    {
68 2
        $this->exec = $exec;
69 2
        $this->package = $package;
70 2
        $this->unPackager = $unPackager;
71 2
    }
72
73
    /**
74
     * provision docker client into a running container
75
     *
76
     * install a binary as /usr/bin/docker and make it executable.
77
     * show the version.
78
     *
79
     * @param string $containerId
80
     * @param string $path to static docker client binary
81
     */
82 1
    public function containerProvisionDockerClientBinary($containerId, $path)
83
    {
84 1
        $this->exec->pass(
85 1
            sprintf('2>&1 < %s docker', lib::quoteArg($path)),
86 1
            array('exec', '-i', $containerId, '/bin/sh', '-c', 'mkdir -p /usr/bin && cat - > /usr/bin/docker; chmod +x /usr/bin/docker; docker --version')
87
        );
88 1
    }
89
90
    /**
91
     * Resolve a binary package name in this repository
92
     *
93
     * @param string $packageName
94
     * @return Repository
95
     */
96 5
    public function resolve($packageName)
97
    {
98 5
        $packageDir = __DIR__ . '/../../../../lib/package';
99 5
        $reader = new PackageYamlFileReader(
100 5
            sprintf('%s/%s.yml', $packageDir, $packageName)
101
        );
102 5
        $this->package = $reader->asPackageArray();
103
104 4
        return $this;
105
    }
106
107
    /**
108
     * @param string $containerId
109
     */
110 1
    public function inject($containerId)
111
    {
112 1
        $package = $this->asPackageArray();
113 1
        $localBinary = $this->getLocalBinary($package);
114 1
        $this->containerProvisionDockerClientBinary($containerId, $localBinary);
115 1
    }
116
117
    /**
118
     * Inject binary docker client package by name into container
119
     *
120
     * @param string $name
121
     * @param string $containerId
122
     */
123 1
    public function injectPackage($name, $containerId)
124
    {
125 1
        $this->resolve($name);
126 1
        $this->inject($containerId);
127 1
    }
128
129
    /**
130
     * Get binary path from local store.
131
     *
132
     * @param array $package
133
     * @return string
134
     */
135 1
    public function getLocalBinary(array $package)
136
    {
137 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

137
        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...
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143 3
    public function asPackageArray()
144
    {
145 3
        $package = $this->package;
146 3
        if (!$package) {
147 1
            $package = $this->resolve('docker-19.03.1-linux-static-x86_64')->package;
148
        }
149
150 3
        return $package;
151
    }
152
}
153