Passed
Push — master ( bf5242...2353ff )
by Phillip
01:25
created

GenerateDeployCommand::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Deployee\Plugins\GenerateDeploy\Commands;
4
5
use Deployee\Components\Config\ConfigInterface;
6
use Deployee\Components\Environment\Environment;
7
use Deployee\Plugins\Deploy\Definitions\Deploy\AbstractDeployDefinition;
8
use Nette\PhpGenerator\ClassType;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class GenerateDeployCommand extends Command
15
{
16
    /**
17
     * @var ConfigInterface
18
     */
19
    private $config;
20
21
    /**
22
     * @var Environment
23
     */
24
    private $env;
25
26
    /**
27
     * @param Environment $env
28
     */
29
    public function setEnv(Environment $env)
30
    {
31
        $this->env = $env;
32
    }
33
34
    /**
35
     * @param ConfigInterface $config
36
     */
37
    public function setConfig(ConfigInterface $config)
38
    {
39
        $this->config = $config;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function configure()
46
    {
47
        parent::configure();
48
        $this
49
            ->setName('deploy:generate')
50
            ->addArgument("name", InputArgument::OPTIONAL, '', uniqid('', false)
51
        );
52
    }
53
54
    /**
55
     * @param InputInterface $input
56
     * @param OutputInterface $output
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $class = sprintf('Deploy_%d_%s', time(), $input->getArgument('name'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('name') can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $class = sprintf('Deploy_%d_%s', time(), /** @scrutinizer ignore-type */ $input->getArgument('name'));
Loading history...
61
        $class = str_replace(['-', ' '], '_', $class);
62
        $filePath = $this->config->get('deploy_definition_path') . "/{$class}.php";
63
        $filePath = strpos($filePath, '/') !== 0 && strpos($filePath, ':') !== 1
64
            ? $this->env->getWorkDir() . DIRECTORY_SEPARATOR . $filePath
65
            : $filePath;
66
67
        $fileContents = $this->generateFileContents($class);
68
        if(!file_put_contents($filePath, $fileContents)){
69
            throw new \RuntimeException('File could not be generated!');
70
        }
71
72
        $output->writeln(sprintf('Generated file %s', $filePath));
73
    }
74
75
    /**
76
     * @param string $class
77
     * @return string
78
     */
79
    private function generateFileContents(string $class): string
80
    {
81
        $generator = new ClassType($class);
82
        $generator->addExtend(AbstractDeployDefinition::class);
83
        if(class_exists('Deployee\Plugins\IdeSupport\IdeSupportPlugin')){
84
            $generator->setComment('@mixin \deployee_ide_helper');
85
        }
86
        $method = $generator->addMethod('define');
87
        $method->setComment('@return void');
88
        $method->setBody('// Start writing awesome deployments');
89
90
        return <<<EOL
91
<?php
92
93
{$generator}
94
EOL;
95
    }
96
}