Completed
Push — master ( 77675a...fe304c )
by Yo
02:42
created

loadTemplateConfigurationFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.2
cc 4
eloc 11
nc 4
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\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,
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 null|ConfigurationFile
53
     */
54
    protected function loadTemplateConfigurationFile(InputInterface $input)
55
    {
56
        $templatePath = $input->getOption(self::OPTION_TEMPLATE);
57
        $templateConfiguration = null;
58
        if ($templatePath) {
59
            if (is_dir($templatePath)) {
60
                $templateConfiguration = $this->configurationFileLoader->fromPath($templatePath);
61
            } elseif (is_file($templatePath)) {
62
                $templateConfiguration = $this->configurationFileLoader->fromString(file_get_contents($templatePath));
63
            } else {
64
                throw new \UnexpectedValueException('Template path is nor a file or a path !');
65
            }
66
        }
67
68
        return $templateConfiguration;
69
    }
70
}
71