Passed
Push — master ( 913299...dd4b11 )
by Tom
04:05
created

StepScriptOption::run()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 19
c 1
b 0
f 1
nc 33
nop 0
dl 0
loc 25
ccs 20
cts 20
cp 1
crap 8
rs 8.4444
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use InvalidArgumentException;
8
use Ktomk\Pipelines\Cli\Args;
9
use Ktomk\Pipelines\Cli\Streams;
10
use Ktomk\Pipelines\File\File;
11
use Ktomk\Pipelines\File\ReferenceTypes;
12
use Ktomk\Pipelines\Runner\RunOpts;
13
use Ktomk\Pipelines\Runner\StepScriptWriter;
14
use RuntimeException;
15
16
/**
17
 * script specific options of the pipelines utility
18
 *
19
 * ** POC **
20
 *
21
 * --step-script[=(<id> | <step>[:<id>])]
22
 *
23
 * dump step-script to stdout. by default the first <step> script of the default pipeline <id>
24
 *
25
 * @package Ktomk\Pipelines\Utility
26
 */
27
class StepScriptOption
28
{
29
    /**
30
     * @var Streams
31
     */
32
    private $streams;
33
34
    /**
35
     * @var Args
36
     */
37
    private $args;
38
39
    /**
40
     * @var File
41
     */
42
    private $file;
43
    /**
44
     * @var RunOpts
45
     */
46
    private $runOpts;
47
48
    /**
49
     * bind options
50
     *
51
     * @param Args $args
52
     * @param Streams $streams
53
     * @param File $file
54
     *
55
     * @return StepScriptOption
56
     */
57 1
    public static function bind(Args $args, Streams $streams, File $file, RunOpts $runOpts)
58
    {
59 1
        return new self($args, $streams, $file, $runOpts);
60
    }
61
62
    /**
63
     * DockerOptions constructor.
64
     *
65
     * @param Args $args
66
     * @param Streams $streams
67
     * @param File $file
68
     * @param RunOpts $runOpts
69
     */
70 13
    public function __construct(Args $args, Streams $streams, File $file, RunOpts $runOpts)
71
    {
72 13
        $this->args = $args;
73 13
        $this->streams = $streams;
74 13
        $this->file = $file;
75 13
        $this->runOpts = $runOpts;
76 13
    }
77
78
    /**
79
     * Process --step-script option
80
     *
81
     * @throws InvalidArgumentException
82
     * @throws RuntimeException
83
     * @throws StatusException
84
     *
85
     * @return void
86
     */
87 11
    public function run()
88
    {
89 11
        $ref = $this->args->getOptionOptionalArgument('step-script', ReferenceTypes::SEG_DEFAULT);
90 11
        if (null !== $ref) {
91 10
            $step = 1;
92 10
            $id = $ref;
93 10
            $n = sscanf($ref, '%d:%s', $step, $id);
94 10
            $id = (1 === $n || '' === $id) ? ReferenceTypes::SEG_DEFAULT : $id;
95 10
            if (!ReferenceTypes::isValidId($id)) {
96 1
                StatusException::fatal(sprintf('invalid pipeline "%s"', $id));
97
            }
98 9
            $pipeline = $this->file->getById($id);
99 9
            if (null === $pipeline) {
100 1
                StatusException::fatal(sprintf('not a pipeline "%s"', $id));
101
            }
102 8
            $steps = $pipeline->getSteps();
103 8
            if (0 >= $step || $step > $steps->count()) {
104 2
                StatusException::fatal(sprintf('pipeline "%s" has no step #%d', $id, $step));
105
            }
106 6
            $script = StepScriptWriter::writeStepScript(
107 6
                $steps[$step - 1]->getScript(),
108 6
                $this->runOpts->getBoolOption('script.exit-early')
109
            );
110 6
            $this->streams->out($script);
111 6
            StatusException::ok();
112
        }
113 1
    }
114
}
115