Completed
Push — feature/add-cli-installers ( 55f17a...115549 )
by Steven
13:41
created

SetupCommand::copyConfigFiles()   B

Complexity

Conditions 4
Paths 18

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 23
rs 8.7972
cc 4
eloc 17
nc 18
nop 3
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
class SetupCommand extends Command
14
{
15
    protected $_basePath;
16
    protected $_projectPath;
17
    protected $_magesteadConfig;
18
19
    protected function configure()
20
    {
21
        $this->_basePath = dirname( __FILE__ ) . '/../../../';
22
        $this->_projectPath = getcwd();
23
24
        $this->setName("setup");
25
        $this->setDescription("Initialise Vagrant Project into current working directory");
26
    }
27
28
    /**
29
     * @param InputInterface $input
30
     * @param OutputInterface $output
31
     * @return \Magestead\Installers\Magento2Project|\Magestead\Installers\MagentoProject
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $helper = $this->getHelper('question');
36
        $options = new Options($helper, $input, $output);
37
38
        $this->setupProject($output, $options);
39
40
        $output->writeln('<info>Spinning up your custom box</info>');
41
        new ProcessCommand('vagrant up', $this->_projectPath, $output);
42
43
        return Project::create($options->getAllOptions(), $this->_magesteadConfig, $this->_projectPath, $output);
44
    }
45
46
    /**
47
     * @param $source
48
     * @param $target
49
     * @param OutputInterface $output
50
     */
51
    protected function copyConfigFiles($source, $target, OutputInterface $output)
52
    {
53
        try {
54
            $progress = new ProgressBar($output, 3720);
55
            $progress->start();
56
            foreach (
57
                $iterator = new \RecursiveIteratorIterator(
58
                    new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
59
                    \RecursiveIteratorIterator::SELF_FIRST) as $item
60
            ) {
61
                if ($item->isDir()) {
62
                    mkdir($target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
63
                } else {
64
                    copy($item, $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
65
                }
66
                $progress->advance();
67
            }
68
            $progress->finish();
69
            echo "\n";
70
        } catch (\Exception $e) {
71
            $output->writeln('<error>There was an error while setting up the project structure</error>');
72
        }
73
    }
74
75
    /**
76
     * @param array $options
77
     * @param OutputInterface $output
78
     */
79
    protected function configureProject(array $options, OutputInterface $output)
80
    {
81
        $this->_magesteadConfig = $this->getConfigFile($output);
82
83
        $this->_magesteadConfig['vagrantfile']['vm']['box'] = $options['box'];
84
        $this->_magesteadConfig['vagrantfile']['vm']['box_url'] = $options['box'];
85
        $this->_magesteadConfig['vagrantfile']['vm']['memory'] = $options['memory_limit'];
86
        $this->_magesteadConfig['vagrantfile']['vm']['network']['private_network'] = $options['ip_address'];
87
        $this->_magesteadConfig['magestead']['apps']['mba_12345']['type'] = ($options['app'] == 'magento 2') ? 'magento2' : 'magento';
88
89
        $this->saveConfigFile($this->_magesteadConfig, $output);
90
91
    }
92
93
    /**
94
     * @param OutputInterface $output
95
     * @return mixed
96
     */
97
    protected function getConfigFile(OutputInterface $output)
98
    {
99
        $yaml = new Parser();
100
        try {
101
            return $yaml->parse(file_get_contents($this->_projectPath . '/magestead.yaml'));
102
        } catch (ParseException $e) {
103
            $output->writeln('<error>Unable to parse the YAML string</error>');
104
            printf("Unable to parse the YAML string: %s", $e->getMessage());
105
        }
106
    }
107
108
    /**
109
     * @param array $config
110
     * @param OutputInterface $output
111
     */
112
    protected function saveConfigFile(array $config, OutputInterface $output)
113
    {
114
        $dumper = new Dumper();
115
        $yaml = $dumper->dump($config, 6);
116
117
        try {
118
            file_put_contents($this->_projectPath . '/magestead.yaml', $yaml);
119
        } catch (\Exception $e) {
120
            $output->writeln('<error>Unable to write to the YAML file</error>');
121
        }
122
    }
123
124
    /**
125
     * @param OutputInterface $output
126
     * @param $options
127
     */
128
    protected function setupProject(OutputInterface $output, $options)
129
    {
130
        $output->writeln('<info>Setting up project structure</info>');
131
        $provisionFolder = $this->_basePath . "provision";
132
        $this->copyConfigFiles($provisionFolder, $this->_projectPath, $output);
133
        $this->configureProject($options->getAllOptions(), $output);
134
    }
135
}
136