ConfigurationCommand::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace YahooJapan\ConfigCacheBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * ConfigurationCommand is a command to generate a code sample of Configuration class.
21
 */
22
class ConfigurationCommand extends ContainerAwareCommand
23
{
24
    protected $bundle;
25
    protected $loader;
26
    protected $fileName;
27
    protected $bundlePath;
28
    protected $resourceDir = '/Resources/config';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 8
    protected function configure()
34
    {
35 8
        $this
36 8
            ->setName('generate:configuration')
37 8
            ->setDescription('Generates a Configuration sample')
38 8
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'A Bundle name of a config file')
39 8
            ->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'A config file name')
40 8
            ->addOption(
41 8
                'configuration', 'c', InputOption::VALUE_OPTIONAL, 'A Configuration class name', 'Configuration'
42 8
            )
43
            ;
44 8
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 5
    protected function initialize(InputInterface $input, OutputInterface $output)
50
    {
51 5
        $this->bundle   = $this->getContainer()->get('kernel')->getBundle($input->getOption('bundle'));
52 4
        $this->loader   = $this->getContainer()->get('yahoo_japan_config_cache.delegating_loader');
53 4
        $this->fileName = $this->bundle->getPath().$this->resourceDir.'/'.$input->getOption('file');
54 4
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 4
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        // load file
62 4
        $config = $this->loader->load($this->fileName);
63
64
        // root key check
65 4
        $rootKey = $this->bundle->getContainerExtension()->getAlias();
66 4
        if (!array_key_exists($rootKey, $config)) {
67 1
            $message = "The config file[{$this->fileName}] root key must be a bundle alias[{$rootKey}]";
68 1
            throw new \RuntimeException($message);
69
        }
70
71
        // generate Configuration
72 3
        $code = $this->createGenerator($config[$rootKey], $input->getOption('configuration'))->generate();
73
74
        // write file
75 3
        $path = $this->createPath($input->getOption('configuration'));
76 3
        file_put_contents($path, $code);
77 3
        $output->writeln("Generated file {$path}");
78 3
    }
79
80
    /**
81
     * Creates a Configuration file path.
82
     *
83
     * @return string
84
     */
85 3
    protected function createPath($className)
86
    {
87 3
        return $this->getBundlePath()."/DependencyInjection/{$className}.php";
88
    }
89
90
    /**
91
     * Gets a Bundle file path.
92
     *
93
     * @return string
94
     */
95 3
    protected function getBundlePath()
96
    {
97 3
        return $this->bundlePath ?: $this->bundle->getPath();
98
    }
99
100
    /**
101
     * Creates a ConfigurationGenerator.
102
     *
103
     * @param array  $config
104
     * @param string $className
105
     *
106
     * @return ConfigurationGenerator
107
     */
108 3
    protected function createGenerator(array $config, $className)
109
    {
110 3
        return new ConfigurationGenerator($config, $this->bundle, $className);
111
    }
112
}
113