FileOptions::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Utility\Show\FileShower;
12
13
class FileOptions implements Runnable
14
{
15
    /**
16
     * @var Args
17
     */
18
    private $args;
19
20
    /**
21
     * @var callable
22
     */
23
    private $output;
24
25
    /**
26
     * @var File
27
     */
28
    private $file;
29
30
    /**
31
     * bind options
32
     *
33
     * @param Args $args
34
     * @param Streams $streams
35
     * @param File $file
36
     *
37
     * @return FileOptions
38
     */
39 1
    public static function bind(Args $args, Streams $streams, File $file)
40
    {
41 1
        return new self($args, $streams, $file);
42
    }
43
44
    /**
45
     * FileOptions constructor.
46
     *
47
     * @param Args $args
48
     * @param callable $output
49
     * @param File $file
50
     */
51 7
    public function __construct(Args $args, $output, File $file)
52
    {
53 7
        $this->args = $args;
54 7
        $this->output = $output;
55 7
        $this->file = $file;
56
    }
57
58
    /**
59
     * run options
60
     *
61
     * @throws InvalidArgumentException
62
     * @throws StatusException
63
     *
64
     * @return $this
65
     */
66 4
    public function run()
67
    {
68 4
        $shower = $this->shower();
69
70 4
        $this->args->mapOption(
71
            array(
72 4
                'images' => 'showImages', /** @see FileShower::showImages() */
73
                'show' => 'showFile', /** @see FileShower::showFile() */
74
                'show-pipelines' => 'showPipelines', /** @see FileShower::showPipelines() */
75
                'show-services' => 'showServices', /** @see FileShower::showServices() */
76
                'list' => 'showPipelineIds', /** @see FileShower::showPipelineIds() */
77
            ),
78
            /**
79
             * @param string $option
80
             * @param string $method
81
             *
82
             * @throws StatusException
83
             */
84 4
            function ($option, $method) use ($shower) {
85 3
                throw new StatusException('', $shower->{$method}());
86
            }
87
        );
88
89 1
        return $this;
90
    }
91
92
    /**
93
     * @param File $pipelines
94
     *
95
     * @return int
96
     */
97 1
    public function showPipelines(File $pipelines)
98
    {
99 1
        return $this->shower($pipelines)->showPipelines();
100
    }
101
102
    /**
103
     * @param null|File $file [optional]
104
     *
105
     * @return FileShower
106
     */
107 5
    private function shower(File $file = null)
108
    {
109 5
        if (null === $file) {
110 4
            $file = $this->file;
111
        }
112
113 5
        return new FileShower($this->output, $file);
114
    }
115
}
116