Passed
Push — dev5 ( 671e66...a3bb6d )
by Ron
06:57
created

updateRun::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Console\Commands;
4
5
// use Doctrine\Common\Cache\Version;
6
use Zip;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Storage;
9
use Illuminate\Support\Arr;
10
11
use PragmaRX\Version\Package\Version;
12
13
14
class updateRun extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'tb-update:run';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Update the Tech Bench to a newer version';
29
30
    /**
31
     * Create a new command instance.
32
     *
33
     * @return void
34
     */
35 2
    public function __construct()
36
    {
37 2
        parent::__construct();
38 2
    }
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return mixed
44
     */
45
    public function handle()
46
    {
47
        $this->line('');
48
        //  Select which update file to use
49
        $updateFile = $this->checkForUpdate();
50
        if($updateFile)
51
        {
52
            //  Open up the file and verify it is at least the same version as the current setup
53
            $valid = $this->openUpdate($updateFile);
0 ignored issues
show
Unused Code introduced by
The assignment to $valid is dead and can be removed.
Loading history...
54
55
            
56
57
58
59
60
61
62
63
64
65
66
67
        }
68
69
70
71
72
    }
73
74
    //  Check to see if any backups are in the backup folder
75
    private function checkForUpdate()
76
    {
77
        $updateList = [];
78
        $updates    = Storage::disk('staging')->files('updates');
79
80
        //  Cycle through each file in the update directory to see if they are update files
81
        foreach($updates as $update)
82
        {
83
            $baseName = explode('/', $update)[1];
84
85
            //  Verify the file is in the .zip format
86
            $fileParts = pathinfo($baseName);
87
            if($fileParts['extension'] == 'zip')
88
            {
89
                //  Verify this is actually an update file
90
                $zip = Zip::open(config('filesystems.disks.staging.root').
91
                    DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.$baseName);
92
                $files = $zip->listFiles();
93
                if(in_array($fileParts['filename'].'/config/version.yml', $files))
94
                {
95
                    $updateList[] = $baseName;
96
                }
97
            }
98
        }
99
100
        if (empty($updateList)) {
101
            $this->error('No updates have been loaded to the system');
102
            $this->error('Please upload update package to the Storage/Staging/Updates folder');
103
            return false;
104
        }
105
106
        //  Determine if there is more than one update that can be applied
107
        if(count($updateList) > 1)
108
        {
109
            $this->line('');
110
111
            $anticipate = [];
112
            foreach($updateList as $key => $up)
113
            {
114
                $opt = $key+1;
115
                $anticipate[$opt] = $up;
116
                $this->line('['.$opt.'] '.$up);
117
            }
118
            $updateFile = $this->choice('Please select which update you would like to load', $anticipate);
119
        }
120
        else
121
        {
122
            $updateFile = $updateList[0];
123
        }
124
125
        return $updateFile;
126
    }
127
128
    //  Unzip the update and verify it is a newer or the same version
129
    private function openUpdate($file)
130
    {
131
        $fileParts = pathinfo($file);
132
        $folder = $fileParts['filename'];
133
134
        $zip = Zip::open(config('filesystems.disks.staging.root') .
135
            DIRECTORY_SEPARATOR . 'updates' . DIRECTORY_SEPARATOR . $file);
136
137
        $zip->extract(config('filesystems.disks.staging.root') .
138
            DIRECTORY_SEPARATOR . 'updates' . DIRECTORY_SEPARATOR .'tmp');
139
140
        $verFile = fopen(config('filesystems.disks.staging.root') .
141
            DIRECTORY_SEPARATOR.'updates'.DIRECTORY_SEPARATOR.'tmp'.
142
            DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.'config'.
143
            DIRECTORY_SEPARATOR.'version.yml', 'r');
144
145
        $verData = [];
146
        $i = 0;
147
        while(!feof($verFile))
0 ignored issues
show
Bug introduced by
It seems like $verFile can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        while(!feof(/** @scrutinizer ignore-type */ $verFile))
Loading history...
148
        {
149
            $line = fgets($verFile);
0 ignored issues
show
Bug introduced by
It seems like $verFile can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            $line = fgets(/** @scrutinizer ignore-type */ $verFile);
Loading history...
150
            $data = explode(':', $line);
151
152
            if(($data[0] === '  major' || $data[0] === '  minor' || $data[0] === '  patch') && $i < 3)
153
            {
154
                $verData[trim($data[0])] = trim($data[1]);
155
                $i++;
156
            }
157
        }
158
159
        $curVersion = new \PragmaRX\Version\Package\Version();
160
161
        $valid = false;
162
        if($verData['major'] > $curVersion->major())
163
        {
164
            $valid = true;
165
        }
166
        else if($verData['minor'] > $curVersion->minor())
167
        {
168
            $valid = true;
169
        }
170
        else if($verData['patch'] >= $curVersion->patch())
171
        {
172
            $valid = true;
173
        }
174
175
        return $valid;
176
    }
177
}
178