Passed
Push — test ( 303a4d...d6edfc )
by Tom
02:34
created

Repository::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 9
ccs 6
cts 6
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
     * 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
     * Resolve a binary package name in this repository
64
     *
65
     * @param string $packageName
66
     * @return Repository
67
     */
68 4
    public function resolve($packageName)
69
    {
70 4
        $packageDir = __DIR__ . '/../../../../lib/package';
71 4
        $reader = new PackageYamlFileReader(
72 4
            sprintf('%s/%s.yml', $packageDir, $packageName)
73
        );
74 4
        $this->package = $reader->asPackageArray();
75
76 3
        return $this;
77
    }
78
79
    /**
80
     * @param $containerId
81
     */
82 1
    public function inject($containerId)
83
    {
84 1
        $package = $this->asPackageArray();
85
86 1
        $local = $this->getLocalBinary($package);
87
88 1
        $this->exec->pass(
89 1
            sprintf('2>&1 < %s docker', lib::quoteArg($local)),
90 1
            array('exec', '-i', $containerId, '/bin/sh', '-c', 'mkdir -p /usr/bin && cat - > /usr/bin/docker; chmod +x /usr/bin/docker; docker --version')
91
        );
92 1
    }
93
94
    /**
95
     * Get binary path from local store.
96
     *
97
     * @param array $package
98
     * @return string
99
     */
100 1
    public function getLocalBinary(array $package)
101
    {
102 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

102
        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...
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108 3
    public function asPackageArray()
109
    {
110 3
        $package = $this->package;
111 3
        if (null === $package) {
112 1
            $package = $this->resolve('docker-19.03.1-linux-static-x86_64')->package;
113
        }
114
115 3
        return $package;
116
    }
117
}
118