Test Failed
Branch test (ad29d6)
by Tom
02:27
created

Repository::asPackageArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 8
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 array package definition data
36
     */
37
    private $package;
38
    /**
39
     * @var null|UnPackager
40
     */
41
    private $unPackager;
42
43
    /**
44
     * Repository constructor.
45
     *
46
     * @param Exec $exec
47
     * @param Directories $directories (UnPackager dependency)
48
     * @param array $package [optional]
49
     * @param UnPackager $unPackager [optional]
50
     */
51
    public function __construct(Exec $exec, Directories $directories, array $package = null, UnPackager $unPackager = null)
52
    {
53
        $this->exec = $exec;
54
        $this->package = $package;
55
        if (null === $unPackager) {
56
            $unPackager = UnPackager::fromDirectories($this->exec, $directories);
57
        }
58
        $this->unPackager = $unPackager;
59
    }
60
61
    /**
62
     * Resolve a binary package name in this repository
63
     *
64
     * @param string $packageName
65
     * @return Repository
66
     */
67
    public function resolve($packageName)
68
    {
69
        $packageDir = __DIR__ . '/../../../../lib/package';
70
        $reader = new PackageYamlFileReader(
71
            sprintf('%s/%s.yml', $packageDir, $packageName)
72
        );
73
        $this->package = $reader->asPackageArray();
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param $containerId
80
     */
81
    public function inject($containerId)
82
    {
83
        $package = $this->asPackageArray();
84
85
        $local = $this->getLocalBinary($package);
86
87
        $this->exec->pass(
88
            sprintf('2>&1 < %s docker', lib::quoteArg($local)),
89
            array('exec', '-i', $containerId, '/bin/sh', '-c', 'mkdir -p /usr/bin && cat - > /usr/bin/docker; chmod +x /usr/bin/docker; docker --version')
90
        );
91
    }
92
93
    /**
94
     * Get binary path from local store.
95
     *
96
     * @param array $package
97
     * @return string
98
     */
99
    public function getLocalBinary(array $package)
100
    {
101
        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

101
        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...
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function asPackageArray()
108
    {
109
        $package =  $this->package;
110
        if (null === $package) {
0 ignored issues
show
introduced by
The condition null === $package is always false.
Loading history...
111
            $package = $this->resolve('docker-19.03.1-linux-static-x86_64')->package;
112
        }
113
114
        return $package;
115
    }
116
}
117