AbstractTemplatableCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace Yoanm\ComposerConfigManager\Infrastructure\Command;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Yoanm\ComposerConfigManager\Application\Loader\ConfigurationFileLoaderInterface;
8
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile;
9
10
abstract class AbstractTemplatableCommand extends Command
11
{
12
    const OPTION_TEMPLATE = 'template';
13
14
    /** @var ConfigurationFileLoaderInterface */
15
    private $configurationFileLoader;
16
17
    /**
18
     * @param ConfigurationFileLoaderInterface $configurationFileLoader
19
     */
20
    public function __construct(ConfigurationFileLoaderInterface $configurationFileLoader)
21
    {
22
        parent::__construct();
23
        $this->configurationFileLoader = $configurationFileLoader;
24
    }
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->addOption(
32
                self::OPTION_TEMPLATE,
33
                null,
34
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
35
                'Path of the json template file. Will be used as default values.'
36
            )
37
        ;
38
    }
39
40
    /**
41
     * @return ConfigurationFileLoaderInterface
42
     */
43
    public function getConfigurationFileLoader()
44
    {
45
        return $this->configurationFileLoader;
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     *
51
     * @return ConfigurationFile[]
52
     */
53
    protected function loadTemplateConfigurationFileList(InputInterface $input)
54
    {
55
        $templatePathList = $input->getOption(self::OPTION_TEMPLATE);
56
        $templateConfigurationList = [];
57
        if (count($templatePathList)) {
58
            foreach ($templatePathList as $templatePath) {
59
                if (is_dir($templatePath)) {
60
                    $templateConfigurationList[] = $this->configurationFileLoader->fromPath($templatePath);
61
                } elseif (is_file($templatePath)) {
62
                    $templateConfigurationList[] = $this->configurationFileLoader->fromString(
63
                        file_get_contents($templatePath)
64
                    );
65
                } else {
66
                    throw new \UnexpectedValueException('Template path is nor a file or a path !');
67
                }
68
            }
69
        }
70
71
        return $templateConfigurationList;
72
    }
73
}
74