Passed
Push — master ( 1d6d9a...2a90e7 )
by Ron
02:47 queued 12s
created

updateRun::checkForUpdate()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 26
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 51
ccs 0
cts 26
cp 0
crap 56
rs 8.5706

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use Zip;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\Facades\Storage;
9
10
class updateRun extends Command
11
{
12
    protected $signature   = 'tb-update:run';
13
    protected $description = 'Update the Tech Bench to a newer version';
14
15
    public function handle()
16
    {
17
        $this->line('');
18
        //  Select which update file to use
19
        $updateFile = $this->checkForUpdate();
20
        if($updateFile)
21
        {
22
            //  Open up the file and verify it is at least the same version as the current setup
23
            $valid = $this->openUpdate($updateFile);
24
25
            if($valid)
26
            {
27
                $this->call('down');
28
                $this->copyFiles($updateFile);
29
30
                $this->info('Update Completed');
31
            }
32
            else
33
            {
34
                $this->error('The selected update is not a newer version');
35
                $this->error('Cannot downgrade Tech Bench in this manner');
36
            }
37
        }
38
    }
39
40
    //  Check to see if any backups are in the backup folder
41
    protected function checkForUpdate()
42
    {
43
        $updateList = [];
44
        $updates    = Storage::disk('staging')->files('updates');
45
46
        //  Cycle through each file in the update directory to see if they are update files
47
        foreach($updates as $update)
48
        {
49
            $baseName = explode('/', $update)[1];
50
51
            //  Verify the file is in the .zip format
52
            $fileParts = pathinfo($baseName);
53
            if($fileParts['extension'] == 'zip')
54
            {
55
                //  Verify this is actually an update file
56
                $zip = Zip::open(config('filesystems.disks.staging.root').
57
                    DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.$baseName);
58
                $files = $zip->listFiles();
59
                if(in_array($fileParts['filename'].'/config/version.yml', $files))
60
                {
61
                    $updateList[] = $baseName;
62
                }
63
            }
64
        }
65
66
        if(empty($updateList))
67
        {
68
            $this->error('No updates have been loaded to the system');
69
            $this->error('Please upload update package to the Storage/Staging/Updates folder');
70
            return false;
71
        }
72
73
        //  Determine if there is more than one update that can be applied
74
        if(count($updateList) > 1)
75
        {
76
            $this->line('');
77
78
            $anticipate = [];
79
            foreach($updateList as $key => $up) {
80
                $opt = $key + 1;
81
                $anticipate[$opt] = $up;
82
                $this->line('['.$opt.'] '.$up);
83
            }
84
            $updateFile = $this->choice('Please select which update you would like to load', $anticipate);
85
        }
86
        else
87
        {
88
            $updateFile = $updateList[0];
89
        }
90
91
        return $updateFile;
92
    }
93
94
    //  Unzip the update and verify it is a newer or the same version
95
    protected function openUpdate($file)
96
    {
97
        $fileParts = pathinfo($file);
98
        $folder = $fileParts['filename'];
99
100
        $zip = Zip::open(config('filesystems.disks.staging.root').
101
            DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.$file);
102
103
        $zip->extract(config('filesystems.disks.staging.root').
104
            DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.'tmp');
105
        $zip->close();
106
107
        $verFile = fopen(config('filesystems.disks.staging.root').
108
            DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.'tmp'.
109
            DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.'config'.
110
            DIRECTORY_SEPARATOR.'version.yml', 'r');
111
112
        $verData = [];
113
        $i = 0;
114
        while(!feof(
115
        /** @scrutinizer ignore-type */
116
        $verFile))
117
        {
118
            $line = fgets(
119
            /** @scrutinizer ignore-type */
120
            $verFile);
121
            $data = explode(':', $line);
122
123
            if(($data[0] === '  major' || $data[0] === '  minor' || $data[0] === '  patch') && $i < 3)
124
            {
125
                $verData[trim($data[0])] = trim($data[1]);
126
                $i++;
127
            }
128
        }
129
130
        $curVersion = new \PragmaRX\Version\Package\Version();
131
132
        $valid = false;
133
        if($verData['major'] > $curVersion->major())
134
        {
135
            $valid = true;
136
        }
137
        else if($verData['minor'] > $curVersion->minor())
138
        {
139
            $valid = true;
140
        }
141
        else if($verData['patch'] >= $curVersion->patch())
142
        {
143
            $valid = true;
144
        }
145
146
        return $valid;
147
    }
148
149
    //  copy the update files to the proper folders
150
    protected function copyFiles($updateFile)
151
    {
152
        $fileParts = pathinfo($updateFile);
153
        $folder = $fileParts['filename'];
154
155
        $updateFile = config('filesystems.disks.staging.root').
156
            DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.'tmp'.
157
            DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR;
158
159
        // Copy files
160
        File::copyDirectory($updateFile.'app',       base_path().DIRECTORY_SEPARATOR.'app');
161
        File::copyDirectory($updateFile.'bootstrap', base_path().DIRECTORY_SEPARATOR.'bootstrap');
162
        File::copyDirectory($updateFile.'config',    base_path().DIRECTORY_SEPARATOR.'config');
163
        File::copyDirectory($updateFile.'database',  base_path().DIRECTORY_SEPARATOR.'database');
164
        File::copyDirectory($updateFile.'public',    base_path().DIRECTORY_SEPARATOR.'public');
165
        File::copyDirectory($updateFile.'resources', base_path().DIRECTORY_SEPARATOR.'resources');
166
        File::copyDirectory($updateFile.'routes',    base_path().DIRECTORY_SEPARATOR.'routes');
167
        File::copy($updateFile.'composer.json',      base_path().DIRECTORY_SEPARATOR.'composer.json');
168
        File::copy($updateFile.'composer.lock',      base_path().DIRECTORY_SEPARATOR.'composer.lock');
169
        File::copy($updateFile.'package.json',       base_path().DIRECTORY_SEPARATOR.'package.json');
170
        File::copy($updateFile.'package-lock.json',  base_path().DIRECTORY_SEPARATOR.'package-lock.json');
171
172
        //  Run Composer Updates
173
        exec('cd '.base_path().' && composer install --no-dev --no-interaction --optimize-autoloader --no-ansi > /dev/null 2>&1');
174
        $this->call('ziggy:generate');
175
        //  Run NPM Updates
176
        exec('cd '.base_path().' && npm install --only=production > /dev/null 2>&1');
177
        exec('cd '.base_path().' && npm run production > /dev/null 2>&1');
178
179
        //  Update the database
180
        $this->call('migrate', ['--force' => 'default']);
181
182
        //  Update the cache
183
        $this->call('config:cache');
184
        $this->call('route:cache');
185
186
        //  Put the app back in working order
187
        $this->call('up');
188
189
        return true;
190
    }
191
}
192