ConfigReaderCommand::checkFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/interop-config for the canonical source repository
6
 * @copyright Copyright (c) 2017-2020 Sandro Keil
7
 * @license   http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types=1);
11
12
namespace Interop\Config\Tool;
13
14
use Interop\Config\Exception\InvalidArgumentException;
15
16
/**
17
 * Command to dump a configuration from a factory class
18
 *
19
 * @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
20
 */
21
class ConfigReaderCommand extends AbstractCommand
22
{
23
    const COMMAND_CLI_NAME = 'display-config';
24
25
    // @codingStandardsIgnoreStart
26
    const HELP_TEMPLATE = <<< EOH
27
<info>Usage:</info>
28
  %s  [options] [<configFile>] [<className>]
29
  
30
<info>Options:</info>
31
  <value>-h, --help, help</value>       Display this help message
32
33
<info>Arguments:</info>
34
  <value>configFile</value>             Path to a config file for which to display options. It must return an array / ArrayObject.
35
  <value>className</value>              Name of the class to reflect and for which to display options.
36
37
Reads the provided configuration file and displays options for the provided class name.
38
EOH;
39
    // @codingStandardsIgnoreEnd
40
41
    /**
42
     * @var ConfigReader
43
     */
44
    private $configReader;
45
46 10
    public function __construct(ConsoleHelper $helper = null, ConfigReader $configReader = null)
47
    {
48 10
        parent::__construct($helper);
49 10
        $this->configReader = $configReader ?: new ConfigReader($this->helper);
50 10
    }
51
52
    /**
53
     * @param array $args Argument list, minus script name
54
     * @return int Exit status
55
     */
56 10
    public function __invoke(array $args): int
57
    {
58 10
        $arguments = $this->parseArgs($args);
59
60 10
        switch ($arguments->command) {
61 10
            case self::COMMAND_HELP:
62 2
                $this->help();
63 2
                return 0;
64 8
            case self::COMMAND_ERROR:
65 4
                $this->helper->writeErrorLine($arguments->message);
66 4
                $this->help();
67 4
                return 1;
68 4
            case self::COMMAND_DUMP:
69
                // fall-through
70
            default:
71 4
                break;
72
        }
73
74 4
        $config = $this->configReader->readConfig($arguments->config, $arguments->class);
75
76 4
        $this->helper->write($this->configReader->dumpConfigFile($config) . PHP_EOL);
77
78 4
        return 0;
79
    }
80
81 1
    protected function checkFile(string $configFile): ?\stdClass
82
    {
83 1
        if (!is_readable(dirname($configFile))) {
84 1
            return $this->createErrorArgument(sprintf(
85 1
                'Cannot read configuration at path "%s".',
86
                $configFile
87
            ));
88
        }
89
        return null;
90
    }
91
}
92