InitCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 46.43%

Importance

Changes 0
Metric Value
dl 0
loc 142
ccs 26
cts 56
cp 0.4643
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 9 1
B execute() 0 76 3
A createInitializer() 0 18 1
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console;
9
10
use Deployer\Initializer\Initializer;
11
use Deployer\Initializer\Template\CakeTemplate;
12
use Deployer\Initializer\Template\CodeIgniterTemplate;
13
use Deployer\Initializer\Template\CommonTemplate;
14
use Deployer\Initializer\Template\DrupalTemplate;
15
use Deployer\Initializer\Template\LaravelTemplate;
16
use Deployer\Initializer\Template\SymfonyTemplate;
17
use Deployer\Initializer\Template\Typo3Template;
18
use Deployer\Initializer\Template\Yii2AdvancedAppTemplate;
19
use Deployer\Initializer\Template\Yii2BasicAppTemplate;
20
use Deployer\Initializer\Template\YiiTemplate;
21
use Deployer\Initializer\Template\ZendTemplate;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Style\SymfonyStyle;
27
use Symfony\Component\Process\Exception\RuntimeException;
28
use Symfony\Component\Process\Process;
29
30
/**
31
 * The command for initialize Deployer system in your project
32
 *
33
 * @author Vitaliy Zhuk <zhuk2[email protected]>
34
 */
35
class InitCommand extends Command
36
{
37
    /**
38
     * @var Initializer
39
     */
40
    private $initializer;
41
42
    /**
43
     * @var array
44
     */
45
    private $availableTemplates;
46
47
    /**
48
     * Construct
49
     *
50
     * @param string $name
51
     */
52 17
    public function __construct($name = null)
53
    {
54 17
        $this->initializer = $this->createInitializer();
55 17
        $this->availableTemplates = $this->initializer->getTemplateNames();
56
57 17
        parent::__construct($name);
58 17
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 17
    protected function configure()
64
    {
65
        $this
66 17
            ->setName('init')
67 17
            ->setDescription('Initialize deployer in your project')
68 17
            ->addOption('template', 't', InputOption::VALUE_OPTIONAL, 'The template of you project. Available templates: ' . implode(', ', $this->availableTemplates))
69 17
            ->addOption('directory', null, InputOption::VALUE_OPTIONAL, 'The directory for create "deploy.php" file', getcwd())
70 17
            ->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'The file name. Default "deploy.php"', 'deploy.php');
71 17
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    protected function execute(InputInterface $input, OutputInterface $output)
77
    {
78
        $template = $input->getOption('template');
79
        $directory = $input->getOption('directory');
80
        $file = $input->getOption('filename');
81
        $params = [];
82
83
        if ($template === null) {
84
            $io = new SymfonyStyle($input, $output);
85
            $helper = $this->getHelper('question');
0 ignored issues
show
Unused Code introduced by Anton Medvedev
$helper is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
            $formatter = $this->getHelper('formatter');
87
88
            // Welcome message
89
            $output->writeln([
90
                '',
91
                $formatter->formatBlock('Welcome to the Deployer config generator', 'bg=blue;fg=white', true),
92
                '',
93
            ]);
94
95
            $io->text([
96
                'This utility will walk you through creating a deploy.php file.',
97
                'It only covers the most common items, and tries to guess sensible defaults.',
98
                '',
99
                'Press ^C at any time to quit.',
100
            ]);
101
102
            // Project type
103
            $template = $io->choice('Please select your project type', $this->availableTemplates, 'Common');
104
105
            // Repo
106
            $default = false;
107
            try {
108
                $default = (new Process('git remote get-url origin'))
109
                    ->mustRun()
110
                    ->getOutput();
111
                $default = trim($default);
112
            } catch (RuntimeException $e) {
113
                // pass
114
            }
115
            $params['repository'] = $io->ask('Repository', $default);
116
117
            // Privacy
118
            $io->text([
119
                'Contribute to the Deployer Development',
120
                '',
121
                'In order to help development and improve the features in Deployer,',
122
                'Deployer has a setting for usage data collection. This function',
123
                'collects anonymous usage data and sends it to Deployer. The data is',
124
                'used in Deployer development to get reliable statistics on which',
125
                'features are used (or not used). The information is not traceable',
126
                'to any individual or organization. Participation is voluntary,',
127
                'and you can change your mind at any time.',
128
                '',
129
                'Anonymous usage data contains Deployer version, php version, os type,',
130
                'name of the command being executed and whether it was successful or not,',
131
                'exception class name, count of hosts and anonymized project hash.',
132
                '',
133
                'If you would like to allow us to gather this information and help',
134
                'us develop a better tool, please add the code below.',
135
                '',
136
                "    <fg=white>set(<fg=cyan>'allow_anonymous_stats'</fg=cyan>, <fg=magenta;options=bold>true</fg=magenta;options=bold>);</fg=white>",
137
                '',
138
                'This function will not affect the performance of Deployer as',
139
                'the data is insignificant and transmitted in a separate process.',
140
            ]);
141
142
            $params['allow_anonymous_stats'] = $GLOBALS['allow_anonymous_stats'] = $io->confirm('Do you confirm?');
143
        }
144
145
        $filePath = $this->initializer->initialize($template, $directory, $file, $params);
146
147
        $output->writeln(sprintf(
148
            '<info>Successfully created:</info> <comment>%s</comment>',
149
            $filePath
150
        ));
151
    }
152
153
    /**
154
     * Create a initializer system
155
     *
156
     * @return Initializer
157
     */
158 17
    private function createInitializer()
159
    {
160 17
        $initializer = new Initializer();
161
162 17
        $initializer->addTemplate('Common', new CommonTemplate());
163 17
        $initializer->addTemplate('Laravel', new LaravelTemplate());
164 17
        $initializer->addTemplate('Symfony', new SymfonyTemplate());
165 17
        $initializer->addTemplate('Yii', new YiiTemplate());
166 17
        $initializer->addTemplate('Yii2 Basic App', new Yii2BasicAppTemplate());
167 17
        $initializer->addTemplate('Yii2 Advanced App', new Yii2AdvancedAppTemplate());
168 17
        $initializer->addTemplate('Zend Framework', new ZendTemplate());
169 17
        $initializer->addTemplate('CakePHP', new CakeTemplate());
170 17
        $initializer->addTemplate('CodeIgniter', new CodeIgniterTemplate());
171 17
        $initializer->addTemplate('Drupal', new DrupalTemplate());
172 17
        $initializer->addTemplate('TYPO3', new Typo3Template());
173
174 17
        return $initializer;
175
    }
176
}
177