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

AbstractTemplatableCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
ccs 0
cts 30
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 11 1
A loadTemplateConfiguration() 0 15 4
1
<?php
2
namespace Yoanm\ComposerConfigManager\Infrastructure\Command;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputOption;
6
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
7
use Yoanm\ComposerConfigManager\Infrastructure\Loader\ConfigurationLoader;
8
9
abstract class AbstractTemplatableCommand extends Command
10
{
11
    const OPTION_TEMPLATE = 'template';
12
13
    /** @var ConfigurationLoader */
14
    private $configurationLoader;
15
16
    /**
17
     * @param ConfigurationLoader $configurationLoader
18
     */
19
    public function __construct(ConfigurationLoader $configurationLoader)
20
    {
21
        parent::__construct();
22
        $this->configurationLoader = $configurationLoader;
23
    }
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        $this
30
            ->addOption(
31
                self::OPTION_TEMPLATE,
32
                null,
33
                InputOption::VALUE_REQUIRED,
34
                'Path of the json template file. Will be used as default values.'
35
            )
36
        ;
37
    }
38
39
    /**
40
     * @param string $templatePath
41
     *
42
     * @return null|Configuration
43
     */
44
    protected function loadTemplateConfiguration($templatePath)
45
    {
46
        $templateConfiguration = null;
47
        if ($templatePath) {
48
            if (is_dir($templatePath)) {
49
                $templateConfiguration = $this->configurationLoader->fromPath($templatePath);
50
            } elseif (is_file($templatePath)) {
51
                $templateConfiguration = $this->configurationLoader->fromString(file_get_contents($templatePath));
52
            } else {
53
                throw new \UnexpectedValueException('Template path is nor a file or a path !');
54
            }
55
        }
56
57
        return $templateConfiguration;
58
    }
59
}
60