Passed
Push — test ( ccd17d...b5a64e )
by Tom
02:39
created

RunnerOptions::parseDockerClient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.8666
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use Ktomk\Pipelines\Cli\Args;
8
use Ktomk\Pipelines\Cli\ArgsException;
9
use Ktomk\Pipelines\Cli\Exec;
10
use Ktomk\Pipelines\Cli\Streams;
11
use Ktomk\Pipelines\Lib;
12
use Ktomk\Pipelines\Runner\Directories;
13
use Ktomk\Pipelines\Runner\Docker\Binary\Repository;
14
use Ktomk\Pipelines\Runner\RunOpts;
15
use Prophecy\Exception\Prediction\AggregateException;
16
17
/**
18
 * aggregated args parser for RunOpts / runner options
19
 *
20
 * @package Ktomk\Pipelines\Utility\Args
21
 */
22
class RunnerOptions
23
{
24
    /**
25
     * @var Args
26
     */
27
    private $args;
28
    /**
29
     * @var Streams
30
     */
31
    private $streams;
32
33
    /**
34
     * @param Args $args
35
     * @param Streams $streams
36
     *
37
     * @return RunnerOptions
38
     */
39 4
    public static function bind(Args $args, Streams $streams)
40
    {
41 4
        return new self($args, $streams);
42
    }
43
44
    /**
45
     * the repository used for listing and validation
46
     *
47
     * @return Repository
48
     */
49 3
    public static function createRepository()
50
    {
51 3
        return Repository::create(
52 3
            new Exec(),
53 3
            new Directories(Lib::env($_SERVER), 'fake')
54
        );
55
    }
56
57
    /**
58
     * list all statically available docker client package names
59
     * that ship w/ pipelines
60
     *
61
     * @param Streams $streams
62
     */
63 1
    public static function listPackages(Streams $streams)
64
    {
65 1
        $list = self::createRepository()->listPackages();
66
67 1
        $streams->out(implode("\n", $list));
68 1
        $streams->out("\n");
69 1
    }
70
71
    /**
72
     * @param Args $args
73
     * @param Streams $streams
74
     */
75 4
    public function __construct(Args $args, Streams $streams)
76
    {
77 4
        $this->args = $args;
78 4
        $this->streams = $streams;
79 4
    }
80
81
    /**
82
     * @throws ArgsException
83
     * @throws StatusException
84
     * @return RunOpts
85
     */
86 4
    public function run()
87
    {
88 4
        $runOpts = RunOpts::create();
89 4
        $this->parse($this->args, $runOpts);
90
91 1
        return $runOpts;
92
    }
93
94
    /**
95
     * Parse keep arguments
96
     *
97
     * @param Args $args
98
     * @param RunOpts $runOpts
99
     *
100
     * @throws ArgsException
101
     * @throws StatusException
102
     */
103 4
    public function parse(Args $args, RunOpts $runOpts)
104
    {
105 4
        $runOpts->setPrefix($this->parsePrefix($args));
106
107 3
        $runOpts->setBinaryPackage($this->parseDockerClient($args));
108
109 2
        $this->parseDockerClientListPackages($args);
110 1
    }
111
112
    /**
113
     * @param Args $args
114
     * @throws ArgsException
115
     * @return string
116
     */
117 3
    private function parseDockerClient(Args $args)
118
    {
119 3
        $default = Repository::PKG_INTEGRATE;
120 3
        $binaryClient = $args->getOptionArgument('docker-client', $default);
121 3
        if ($binaryClient !== $default) {
122 2
            $repository = self::createRepository();
123
124
            try {
125 2
                $repository->resolve($binaryClient);
126 1
            } catch (\InvalidArgumentException $ex) {
127 1
                $message = "--docker-client needs a valid package name, file or docker client binary path; non-matching '${binaryClient}' given";
128 1
                $message .= "\n  docker client binary packages shipping w/ pipelines:";
129 1
                $message .= "\n    - " . implode("\n    - ", $repository->listPackages());
130 1
                ArgsException::__($message);
131
            }
132
        }
133
134
        return $binaryClient;
135
    }
136
137
    /**
138
     * @param Args $args
139
     *
140
     * @throws StatusException
141
     */
142
    private function parseDockerClientListPackages(Args $args)
143
    {
144 2
        if (!$args->hasOption('docker-client-pkgs')) {
145 1
            return;
146
        }
147
148 1
        self::listPackages($this->streams);
149
150 1
        throw new StatusException('', 0);
151
    }
152
153
    /**
154
     * @param Args $args
155
     * @throws ArgsException
156
     * @return string
157
     */
158
    private function parsePrefix(Args $args)
159
    {
160 4
        $prefix = $args->getOptionArgument('prefix', App::UTILITY_NAME);
161 4
        if (!preg_match('~^[a-z]{3,}$~', $prefix)) {
162 1
            ArgsException::__(sprintf("invalid prefix: '%s'", $prefix));
163
        }
164
165 3
        return $prefix;
166
    }
167
}
168