GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CopyToRemoteCommand   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 138
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 14 3
A getOptions() 0 19 1
A handleOptions() 0 9 3
A upload() 0 38 4
A getDiskName() 0 5 1
A ignoredFile() 0 8 2
1
<?php
2
3
namespace Elimuswift\DbExporter\Commands;
4
5
use Config;
6
use Storage;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class CopyToRemoteCommand extends GeneratorCommand
10
{
11
    protected $name = 'db-exporter:backup';
12
13
    protected $description = 'Command to copy the migrations and/or the seeds to a remote host.';
14
15
    protected $ignoredFiles = [
16
                                '..',
17
                                '.',
18
                                '.gitkeep',
19
                                ];
20
21
    protected $uploadedFiles;
22
23
    protected static $filesCount;
24
25
    public function __construct()
26
    {
27
        parent::__construct();
28
    }
29
30
    public function handle()
31
    {
32
        $this->handleOptions();
33
        foreach ($this->uploadedFiles as $type => $files) {
34
            $this->line("\n");
35
            $this->info(ucfirst($type));
36
            foreach ($files as $file) {
37
                $this->sectionMessage($type, $file.' uploaded.');
38
            }
39
        }
40
41
        $disk = $this->getDiskName();
42
        $this->blockMessage('Success!', "Everything uploaded to $disk filesystem!");
43
    }
44
45
    //end fire()
46
47
    protected function getOptions()
48
    {
49
        return [
50
                [
51
                    'migrations',
52
                    'm',
53
                    InputOption::VALUE_NONE,
54
                    'Upload the migrations to a storage.',
55
                    null,
56
                ],
57
                [
58
                    'seeds',
59
                    's',
60
                    InputOption::VALUE_NONE,
61
                    'Upload the seeds to the remote host.',
62
                    null,
63
                ],
64
                ];
65
    }
66
67
    //end getOptions()
68
69
    protected function handleOptions()
70
    {
71
        $options = $this->option();
72
        foreach ($options as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $options of type array|string|boolean|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
73
            if ($value) {
74
                $this->upload($key);
75
            }
76
        }
77
    }
78
79
    protected function upload($what)
80
    {
81
        $localPath = Config::get('db-exporter.export_path.'.$what);
82
        $dir = scandir($localPath);
83
        $remotePath = Config::get('db-exporter.remote.'.$what);
84
        $this->line("\n");
85
        $this->info(ucfirst($what));
86
        // Reset file coounter
87
        static::$filesCount = 0;
88
        // Prepare the progress bar
89
        array_walk($dir, function ($file) {
90
            if ($this->ignoredFile($file)) {
91
                return;
92
            }
93
            ++static::$filesCount;
94
        });
95
        $progress = $this->output->createProgressBar(static::$filesCount);
96
        foreach ($dir as $file) {
97
            if ($this->ignoredFile($file)) {
98
                $this->comment("---> ignoring $file ");
99
                continue;
100
            }
101
102
            // Capture the uploaded files for displaying later
103
            $this->uploadedFiles[$what][] = $remotePath.$file;
104
105
            // Copy the files
106
            Storage::disk($this->getDiskName())->put(
107
                $remotePath.$file,
108
                $localPath.'/'.$file
109
            );
110
            $progress->advance();
111
        }
112
113
        $progress->finish();
114
115
        return true;
116
    }
117
118
    //end upload()
119
120
    /**
121
     * @return string|null
122
     */
123
    protected function getDiskName()
124
    {
125
        // For now static from he config file.
126
        return Config::get('db-exporter.backup.disk');
127
    }
128
129
    /**
130
     * Determine if a file should be ignored.
131
     *
132
     * @param string $file filename
133
     *
134
     * @return bool
135
     **/
136
    protected function ignoredFile($file)
137
    {
138
        if (in_array($file, $this->ignoredFiles)) {
139
            return true;
140
        }
141
142
        return false;
143
    }
144
145
    //end getDiskName()
146
}//end class
147