Completed
Push — master ( e93ba9...f665a7 )
by Yo
02:45
created

getConfigurationLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\ConfigurationLoaderInterface;
8
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
9
10
abstract class AbstractTemplatableCommand extends Command
11
{
12
    const OPTION_TEMPLATE = 'template';
13
14
    /** @var ConfigurationLoaderInterface */
15
    private $configurationLoader;
16
17
    /**
18
     * @param ConfigurationLoaderInterface $configurationLoader
19
     */
20
    public function __construct(ConfigurationLoaderInterface $configurationLoader)
21
    {
22
        parent::__construct();
23
        $this->configurationLoader = $configurationLoader;
24
    }
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->addOption(
32
                self::OPTION_TEMPLATE,
33
                null,
34
                InputOption::VALUE_REQUIRED,
35
                'Path of the json template file. Will be used as default values.'
36
            )
37
        ;
38
    }
39
40
    /**
41
     * @return ConfigurationLoaderInterface
42
     */
43
    public function getConfigurationLoader()
44
    {
45
        return $this->configurationLoader;
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     *
51
     * @return null|Configuration
52
     */
53
    protected function loadTemplateConfiguration(InputInterface $input)
54
    {
55
        $templatePath = $input->getOption(self::OPTION_TEMPLATE);
56
        $templateConfiguration = null;
57
        if ($templatePath) {
58
            if (is_dir($templatePath)) {
59
                $templateConfiguration = $this->configurationLoader->fromPath($templatePath);
60
            } elseif (is_file($templatePath)) {
61
                $templateConfiguration = $this->configurationLoader->fromString(file_get_contents($templatePath));
62
            } else {
63
                throw new \UnexpectedValueException('Template path is nor a file or a path !');
64
            }
65
        }
66
67
        return $templateConfiguration;
68
    }
69
}
70