Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

InstallCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 137
Duplicated Lines 5.84 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 8
loc 137
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 13 1
B execute() 8 57 8
B copyFolder() 0 21 5
A checkContinue() 0 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Console\Question\Question;
11
use Symfony\Component\Finder\Finder;
12
13
class InstallCommand extends Command {
14
15
    const DIRECTORY = 'dir';
16
17
    protected $fs;
18
19
    /**
20
     * InstallCommand constructor.
21
     *
22
     * @param string $name
23
     */
24
    public function __construct($name = null) {
25
        parent::__construct($name);
26
27
        $this->fs = new Filesystem();
28
    }
29
30
    protected function configure() {
31
        $this->setName('site:install')
32
            ->setDescription('Setup a new Stitcher installation')
33
            ->setHelp("This command will generate several files as a base installation. (Only if they don't exist yet.)
34
            
35
    - The src/ directory with some examples
36
    - The public/ directory
37
    - The dev/ directory
38
    - The stitcher console
39
    - A sample config.yml
40
    - Dev setup
41
");
42
    }
43
44
    /**
45
     * @param InputInterface  $input
46
     * @param OutputInterface $output
47
     *
48
     * @return int|null|void
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output) {
51
        $log = [];
52
53
        $srcDir = './src';
54
        $publicDir = './public';
55
        $devDir = './dev';
56
        $stitcherPath = './stitcher';
57
        $configPath = './config.yml';
58
59
        $installDir = __DIR__ . '/../../install';
60
        $installSrcDir = "{$installDir}/src";
61
        $installDevDir = "{$installDir}/dev";
62
        $installPublicDir = "{$installDir}/public";
63
        $installStitcherPath = "{$installDir}/stitcher";
64
        $installConfigPath = "{$installDir}/config.yml";
65
66
        if (!$this->fs->exists($configPath)) {
67
            $this->fs->copy($installConfigPath, $configPath);
68
69
            $log[] = "Copied a sample config.yml to {$configPath}";
70
        }
71
72
        if (!$this->fs->exists($stitcherPath)) {
73
            $this->fs->copy($installStitcherPath, $stitcherPath);
74
75
            $log[] = "Copied the Stitcher Console to {$stitcherPath}";
76
        }
77
78
        if (!$this->fs->exists($srcDir)) {
79
            $this->copyFolder($installSrcDir, $srcDir);
80
81
            $log[] = "Copied a sample src/ to {$srcDir}";
82
        }
83
84
        if (!$this->fs->exists($devDir)) {
85
            $this->copyFolder($installDevDir, $devDir);
86
87
            $log[] = "Copied a sample dev/ to {$devDir}";
88
        }
89
90
        if (!$this->fs->exists($publicDir)) {
91
            $this->copyFolder($installPublicDir, $publicDir);
92
93
            $log[] = "Copied the public/ dir to {$publicDir}";
94
        }
95
96 View Code Duplication
        if (count($log)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
            $output->writeln("Stitcher was successfully installed!\n");
98
            foreach ($log as $line) {
99
                $output->writeln("- {$line}");
100
            }
101
        } else {
102
            $output->writeln("Stitcher has already been installed.");
103
        }
104
105
        return;
106
    }
107
108
    protected function copyFolder($src, $dst) {
109
        $finder = new Finder();
110
        $srcFiles = $finder->files()->in($src)->ignoreDotFiles(false);
111
112
        if (!$this->fs->exists($dst)) {
113
            $this->fs->mkdir($dst);
114
        }
115
116
        foreach ($srcFiles as $srcFile) {
117
            if (!$this->fs->exists("{$dst}/{$srcFile->getRelativePath()}")) {
118
                $this->fs->mkdir("{$dst}/{$srcFile->getRelativePath()}");
119
            }
120
121
            $path = $srcFile->getRelativePathname();
122
            if (!$this->fs->exists("{$dst}/{$path}")) {
123
                $this->fs->touch("{$dst}/{$path}");
124
            }
125
126
            $this->fs->dumpFile("{$dst}/{$path}", $srcFile->getContents());
127
        }
128
    }
129
130
    protected function checkContinue(InputInterface $input, OutputInterface $output) {
131
        $srcDir = Config::get('directory.src');
132
133
        if ($this->fs->exists($srcDir)) {
134
            $questionHelper = $this->getHelper('question');
135
            $question = new Question('The src/ directory already exists, are you sure you want to continue? (Some files might be overwritten) [y/N] ', false);
136
137
            $overwrite = $questionHelper->ask($input, $output, $question);
138
139
            if (!$overwrite) {
140
                $output->writeln('Cancelling the install, run <fg=green>stitcher site:install</> again if you want to install the site anyways.');
141
142
                return false;
143
            }
144
        }
145
146
        return true;
147
    }
148
149
}
150