Completed
Pull Request — master (#13)
by Yo
02:36
created

AbstractTemplatableCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Domain\Model\Configuration;
8
use Yoanm\ComposerConfigManager\Infrastructure\Loader\ConfigurationLoader;
9
10
abstract class AbstractTemplatableCommand extends Command
11
{
12
    const OPTION_TEMPLATE = 'template';
13
14
    /** @var ConfigurationLoader */
15
    private $configurationLoader;
16
17
    /**
18
     * @param ConfigurationLoader $configurationLoader
19
     */
20
    public function __construct(ConfigurationLoader $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
     * @param InputInterface $input
42
     *
43
     * @return null|Configuration
44
     */
45
    protected function loadTemplateConfiguration(InputInterface $input)
46
    {
47
        $templatePath = $input->getOption(self::OPTION_TEMPLATE);
48
        $templateConfiguration = null;
49
        if ($templatePath) {
50
            if (is_dir($templatePath)) {
51
                $templateConfiguration = $this->configurationLoader->fromPath($templatePath);
52
            } elseif (is_file($templatePath)) {
53
                $templateConfiguration = $this->configurationLoader->fromString(file_get_contents($templatePath));
54
            } else {
55
                throw new \UnexpectedValueException('Template path is nor a file or a path !');
56
            }
57
        }
58
59
        return $templateConfiguration;
60
    }
61
}
62