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

loadTemplateConfiguration()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 20
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