Completed
Push — epic/2.0.0 ( 97ef1b...098c19 )
by Steven
9s
created

SetupCommand::configureProject()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 3
Metric Value
c 6
b 0
f 3
dl 0
loc 24
rs 8.9713
cc 2
eloc 17
nc 2
nop 2
1
<?php namespace Magestead\Command;
2
3
use Magestead\Helper\Options;
4
use Magestead\Installers\Project;
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Symfony\Component\Yaml\Parser;
7
use Symfony\Component\Yaml\Dumper;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Yaml\Exception\ParseException;
12
13
/**
14
 * Class SetupCommand
15
 * @package Magestead\Command
16
 */
17
class SetupCommand extends Command
18
{
19
    protected $_basePath;
20
    protected $_projectPath;
21
    protected $_msConfig;
22
23
    protected function configure()
24
    {
25
        $this->_basePath    = dirname( __FILE__ ) . '/../../../';
26
        $this->_projectPath = getcwd();
27
28
        $this->setName("setup");
29
        $this->setDescription("Initialise Magestead project into current working directory");
30
    }
31
32
    /**
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @return \Magestead\Installers\Magento2Project|\Magestead\Installers\MagentoProject
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $helper  = $this->getHelper('question');
40
        $options = new Options($helper, $input, $output);
41
42
        $this->setupProject($output, $options);
43
44
        $output->writeln('<info>Spinning up your custom box</info>');
45
        new ProcessCommand('vagrant up', $this->_projectPath, $output);
46
47
        return Project::create($options->getAllOptions(), $this->_msConfig, $this->_projectPath, $output);
48
    }
49
50
    /**
51
     * @param $source
52
     * @param $target
53
     * @param OutputInterface $output
54
     */
55
    protected function copyConfigFiles($source, $target, OutputInterface $output)
56
    {
57
        try {
58
            $progress = new ProgressBar($output, 3720);
59
            $progress->start();
60
            foreach (
61
                $iterator = new \RecursiveIteratorIterator(
62
                    new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
63
                    \RecursiveIteratorIterator::SELF_FIRST) as $item
64
            ) {
65
                if ($item->isDir()) {
66
                    mkdir($target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
67
                } else {
68
                    copy($item, $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
69
                }
70
                $progress->advance();
71
            }
72
            $progress->finish();
73
            echo "\n";
74
        } catch (\Exception $e) {
75
            $output->writeln('<error>There was an error while setting up the project structure</error>');
76
        }
77
    }
78
79
    /**
80
     * @param array $options
81
     * @param OutputInterface $output
82
     */
83
    protected function configureProject(array $options, OutputInterface $output)
84
    {
85
        $msConfig = $this->getConfigFile($output);
86
87
        $app = ($options['app'] == 'magento2') ? 'magento2' : 'magento';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

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

will produce issues in the first and second line, while this second example

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

will produce no issues.

Loading history...
88
        $hostname = 'magestead-' . $options['base_url'];
89
90
        $msConfig['vagrantfile']['vm']['box']                           = $options['box'];
91
        $msConfig['vagrantfile']['vm']['box_url']                       = $options['box'];
92
        $msConfig['vagrantfile']['vm']['hostname']                      = $hostname;
93
        $msConfig['vagrantfile']['vm']['memory']                        = $options['memory_limit'];
94
        $msConfig['vagrantfile']['vm']['network']['private_network']    = $options['ip_address'];
95
        $msConfig['magestead']['apps']['mba_12345']['type']             = $app;
96
        $msConfig['magestead']['apps']['mba_12345']['locale']           = $options['locale'];
97
        $msConfig['magestead']['apps']['mba_12345']['default_currency'] = $options['default_currency'];
98
        $msConfig['magestead']['apps']['mba_12345']['base_url']         = $options['base_url'];
99
        $msConfig['magestead']['os']                                    = $options['os'];
100
        $msConfig['magestead']['server']                                = $options['server'];
101
102
        $this->_msConfig = $msConfig;
103
104
        $this->saveConfigFile($msConfig, $output);
105
106
    }
107
108
    /**
109
     * @param OutputInterface $output
110
     * @return mixed
111
     */
112
    protected function getConfigFile(OutputInterface $output)
113
    {
114
        $yaml = new Parser();
115
        try {
116
            return $yaml->parse(file_get_contents($this->_projectPath . '/magestead.yaml'));
117
        } catch (ParseException $e) {
118
            $output->writeln('<error>Unable to parse the YAML string</error>');
119
            printf("Unable to parse the YAML string: %s", $e->getMessage());
120
        }
121
    }
122
123
    /**
124
     * @param array $config
125
     * @param OutputInterface $output
126
     */
127 View Code Duplication
    protected function saveConfigFile(array $config, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $dumper = new Dumper();
130
        $yaml   = $dumper->dump($config, 6);
131
132
        try {
133
            file_put_contents($this->_projectPath . '/magestead.yaml', $yaml);
134
        } catch (\Exception $e) {
135
            $output->writeln('<error>Unable to write to the YAML file</error>');
136
        }
137
    }
138
139
    /**
140
     * @param OutputInterface $output
141
     * @param $options
142
     */
143
    protected function setupProject(OutputInterface $output, $options)
144
    {
145
        $output->writeln('<info>Setting up project structure</info>');
146
        $provisionFolder = $this->_basePath . "provision";
147
        $this->copyConfigFiles($provisionFolder, $this->_projectPath, $output);
148
        $this->configureProject($options->getAllOptions(), $output);
149
    }
150
}
151