Test Failed
Push — test ( 0c4d14...2be48d )
by Tom
02:17
created

Repository::asPackageArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

104
        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...
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function asPackageArray()
111
    {
112
        $package =  $this->package;
113
        if (null === $package) {
0 ignored issues
show
introduced by
The condition null === $package is always false.
Loading history...
114
            $package = array(
115
                'name' => 'docker-19.03.1-linux-static-x86_64',
116
                'uri' => 'https://download.docker.com/linux/static/stable/x86_64/docker-19.03.1.tgz',
117
                'basename' => 'docker-19.03.1',
118
                'sha256' => '6e7d8e24ee46b13d7547d751696d01607d19c8224c1b2c867acc8c779e77734b',
119
                'binary' => 'docker/docker',
120
                'binary_sha256' => '9b6191f64cb89e706dc29390408260d643e88f7f853d8878b2fb0360186b2ac3',
121
            );
122
        }
123
124
        return $package;
125
    }
126
}
127