Completed
Pull Request — master (#200)
by Erin
01:51
created

ConfigurationReader::readPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Peridot\Console;
3
4
use Peridot\Configuration;
5
use Symfony\Component\Console\Input\InputInterface;
6
7
/**
8
 * The ConfigurationReader is responsible for building a Configuration
9
 * object from an InputInterface.
10
 *
11
 * @package Peridot\Console
12
 */
13
class ConfigurationReader
14
{
15
    /**
16
     * @var \Symfony\Component\Console\Input\InputInterface
17
     */
18
    protected $input;
19
20
    /**
21
     * @param InputInterface $input
22
     */
23
    public function __construct(InputInterface $input)
24
    {
25
        $this->input = $input;
26
    }
27
28
    /**
29
     * Read configuration information from input
30
     *
31
     * @return Configuration
32
     */
33
    public function read()
34
    {
35
        $configuration = new Configuration();
36
        $configuration->setPaths($this->readPaths());
37
38
        $options = [
39
            'focus' => [$configuration, 'setFocusPattern'],
40
            'skip' => [$configuration, 'setSkipPattern'],
41
            'grep' => [$configuration, 'setGrep'],
42
            'no-colors' => [$configuration, 'disableColors'],
43
            'force-colors' => [$configuration, 'enableColorsExplicit'],
44
            'bail' => [$configuration, 'stopOnFailure'],
45
            'configuration' => [$configuration, 'setConfigurationFile']
46
        ];
47
48
        foreach ($options as $option => $callable) {
49
            $this->callForOption($option, $callable);
50
        }
51
52
        return $configuration;
53
    }
54
55
    /**
56
     * Static access to reader
57
     *
58
     * @param  InputInterface $input
59
     * @return Configuration
60
     */
61
    public static function readInput(InputInterface $input)
62
    {
63
        $reader = new static($input);
64
65
        return $reader->read();
66
    }
67
68
    /**
69
     * Execute a callback if the input object has a value for the
70
     * given option name.
71
     *
72
     * @param string $optionName
73
     * @param callable $callable
74
     */
75
    protected function callForOption($optionName, callable $callable)
76
    {
77
        $value = $this->input->getOption($optionName);
78
        if ($value) {
79
            call_user_func_array($callable, [$value]);
80
        }
81
    }
82
83
    private function readPaths()
84
    {
85
        $paths = $this->input->getArgument('path');
86
87
        if (null === $this->input->getFirstArgument()) {
88
            $paths = array_filter($paths, 'file_exists');
89
        }
90
91
        return $paths;
92
    }
93
}
94