Completed
Pull Request — master (#93)
by
unknown
03:16
created

GenerateSecuredSkeleton   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
B execute() 0 30 2
B generateSecuredVariables() 0 29 2
1
<?php
2
3
namespace Karma\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Karma\Configuration;
9
use Karma\Command;
10
use Karma\Configuration\ValueFilterIterator;
11
use Karma\Hydrator;
12
13
class GenerateSecuredSkeleton extends Command
14
{
15
    const
16
        ENV_PROD= 'prod';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
17
18
    protected function configure()
19
    {
20
        parent::configure();
21
22
        $this
23
            ->setName('generate:secure:skeleton')
24
            ->setDescription('Generate the secured.conf file')
25
26
            ->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Target environment', self::ENV_PROD)
27
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existing file if present')
28
            ->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Target path for generated secured.conf file')
29
        ;
30
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        parent::execute($input, $output);
35
36
        $environment = $input->getOption('env');
37
        $outputPath = $input->getOption('output');
38
        $overwrite = $input->getOption('force');
39
40
        $this->output->writeln(sprintf(
41
            "<info>Generate secured variables for <comment>%s</comment> environment</info>\n",
42
            $environment
43
        ));
44
45
        $reader = $this->app['configuration'];
46
        $reader->setDefaultEnvironment($input->getOption('env'));
47
        
48
        $securedVariables = $this->generateSecuredVariables($reader);
49
50
        if(!empty($outputPath))
51
        {
52
            $this->app['configuration.fileSystem']->write($outputPath, $securedVariables, $overwrite);
53
        }
54
        
55
        $this->output->writeln('');
56
        $this->output->writeln('***');
57
        
58
        $this->output->writeln($securedVariables);
59
        
60
        $this->output->writeln('***');
61
    }
62
63
    private function generateSecuredVariables(Configuration $reader)
64
    {
65
        $values = new ValueFilterIterator(
66
            Configuration::NOT_FOUND,
67
            new \ArrayIterator($reader->getAllValuesForEnvironment())
68
        );
69
70
        $values->ksort();
0 ignored issues
show
Bug introduced by
The method ksort() does not seem to exist on object<Karma\Configuration\ValueFilterIterator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72
        $securedConf = "[variables]";
73
        
74
        foreach($values as $variable => $value)
75
        {
76
            $variablePattern = <<<PATTERN
77
78
%s:
79
    %s = %s
80
PATTERN;
81
82
            $securedConf .= sprintf(
83
                $variablePattern,
84
                $variable,
85
                $reader->getDefaultEnvironment(),
86
                Hydrator::TODO_VALUE
87
            );
88
        }
89
        
90
        return $securedConf;
91
    }
92
}
93