CopyToRemoteCommand   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 119
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 15 4
A __construct() 0 10 1
A getArguments() 0 6 1
A getOptions() 0 7 1
A getRemoteName() 0 9 2
B handleOptions() 0 19 6
B upload() 0 31 3
1
<?php namespace Nwidart\DbExporter\Commands;
2
3
use Nwidart\DbExporter\Server;
4
use Symfony\Component\Console\Input\InputOption;
5
use Symfony\Component\Console\Input\InputArgument;
6
use SSH;
7
use Config;
8
9
class CopyToRemoteCommand extends GeneratorCommand
10
{
11
12
    protected $name = 'dbe:remote';
13
    protected $description = 'Command to copy the migrations and/or the seeds to a remote host.';
14
15
    protected $ignoredFiles =  array('..', '.', '.gitkeep');
16
17
    protected $migrationsPath;
18
    protected $seedsPath;
19
    protected $uploadedFiles;
20
    protected $commandOptions;
21
22
    protected $server;
23
24
    public function __construct(Server $server)
25
    {
26
        parent::__construct();
27
28
        // Set the paths
29
        $this->migrationsPath = app_path() . "/database/migrations";
30
        $this->seedsPath = app_path() . "/database/seeds";
31
32
        $this->server = $server;
33
    }
34
35
    public function fire()
36
    {
37
        $succes = $this->handleOptions();
38
        if ($succes) {
39
            // Inform what files have been uploaded
40
            foreach ($this->uploadedFiles as $type => $files) {
41
                $this->info(ucfirst($type));
42
                foreach ($files as $file) {
43
                    $this->sectionMessage($type, $file .' uploaded.');
44
                }
45
            }
46
47
            $this->blockMessage('Success!', 'Everything uploaded!');
48
        }
49
    }
50
51
    protected function getArguments()
52
    {
53
        return array(
54
            array('remote', InputArgument::REQUIRED, 'The remote name.')
55
        );
56
    }
57
58
    protected function getOptions()
59
    {
60
        return array(
61
            array('migrations', 'mig', InputOption::VALUE_NONE, 'Upload the migrations to the remote host.', null),
62
            array('seeds', 'seed', InputOption::VALUE_NONE, 'Upload the seeds to the remote host.', null)
63
        );
64
    }
65
66
    private function getRemoteName()
67
    {
68
        // Use default production key
69
        if (!$this->argument('remote')) {
70
            return 'production';
71
        } else {
72
            return $this->argument('remote');
73
        }
74
    }
75
76
    private function handleOptions()
77
    {
78
        $options = $this->option();
79
        switch ($options) {
80
            case (($options['seeds'] === true) && ($options['migrations'] === true)):
81
                if (!$this->upload('migrations')) return false;
82
                return $this->upload('seeds');
83
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
84
            case $options['migrations'] === true:
85
                // // $this->server->upload('migrations');
86
                $this->commandOptions = 'migrations';
87
                return $this->upload('migrations');
88
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
89
            case $options['seeds'] === true:
90
                $this->commandOptions = 'seeds';
91
                return $this->upload('seeds');
92
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
93
        }
94
    }
95
96
    private function upload($what)
97
    {
98
        $localPath = "{$what}Path";
99
100
        $dir = scandir($this->$localPath);
101
        $remotePath = config('db-exporter.remote.'.$what);
102
103
        // Prepare the progress bar
104
        $progress = $this->getHelperSet()->get('progress');
105
        $filesCount = count($dir) - count($this->ignoredFiles);
106
        $progress->start($this->output, $filesCount);
107
        $this->info(ucfirst($what));
108
        foreach($dir as $file) {
109
            if (in_array($file, $this->ignoredFiles)) {
110
                continue;
111
            }
112
113
            // Capture the uploaded files for displaying later
114
            $this->uploadedFiles[$what][] = $remotePath . $file;
115
116
            // Copy the files
117
            SSH::into($this->getRemoteName())->put(
118
                $this->$localPath .'/' . $file,
119
                $remotePath . $file
120
            );
121
            $progress->advance();
122
        }
123
        $progress->finish();
124
125
        return true;
126
    }
127
}