Completed
Push — master ( fe304c...6b259c )
by Yo
02:47
created

AbstractTemplatableCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 11 1
A getConfigurationFileLoader() 0 4 1
B loadTemplateConfigurationFileList() 0 20 5
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\Configuration;
9
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile;
10
11
abstract class AbstractTemplatableCommand extends Command
12
{
13
    const OPTION_TEMPLATE = 'template';
14
15
    /** @var ConfigurationFileLoaderInterface */
16
    private $configurationFileLoader;
17
18
    /**
19
     * @param ConfigurationFileLoaderInterface $configurationFileLoader
20
     */
21
    public function __construct(ConfigurationFileLoaderInterface $configurationFileLoader)
22
    {
23
        parent::__construct();
24
        $this->configurationFileLoader = $configurationFileLoader;
25
    }
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function configure()
30
    {
31
        $this
32
            ->addOption(
33
                self::OPTION_TEMPLATE,
34
                null,
35
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
36
                'Path of the json template file. Will be used as default values.'
37
            )
38
        ;
39
    }
40
41
    /**
42
     * @return ConfigurationFileLoaderInterface
43
     */
44
    public function getConfigurationFileLoader()
45
    {
46
        return $this->configurationFileLoader;
47
    }
48
49
    /**
50
     * @param InputInterface $input
51
     *
52
     * @return ConfigurationFile[]
53
     */
54
    protected function loadTemplateConfigurationFileList(InputInterface $input)
55
    {
56
        $templatePathList = $input->getOption(self::OPTION_TEMPLATE);
57
        $templateConfigurationList = [];
58
        if (count($templatePathList)) {
59
            foreach ($templatePathList as $templatePath) {
60
                if (is_dir($templatePath)) {
61
                    $templateConfigurationList[] = $this->configurationFileLoader->fromPath($templatePath);
62
                } elseif (is_file($templatePath)) {
63
                    $templateConfigurationList[] = $this->configurationFileLoader->fromString(
64
                        file_get_contents($templatePath)
65
                    );
66
                } else {
67
                    throw new \UnexpectedValueException('Template path is nor a file or a path !');
68
                }
69
            }
70
        }
71
72
        return $templateConfigurationList;
73
    }
74
}
75