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

AbstractTemplatableCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 11 1
A loadTemplateConfiguration() 0 16 4
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