|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Installer |
|
5
|
|
|
* @author Iurii Makukh |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\installer\handlers; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\models\FileTransfer; |
|
13
|
|
|
use gplcart\modules\installer\models\Install; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handler for Installer module |
|
17
|
|
|
*/ |
|
18
|
|
|
class Download |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Language model instance |
|
23
|
|
|
* @var \gplcart\core\models\Language $language |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $language; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Installer model class instance |
|
29
|
|
|
* @var \gplcart\modules\installer\models\Install $install |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $install; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* File transfer model instance |
|
35
|
|
|
* @var \gplcart\core\models\FileTransfer $file_transfer |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $file_transfer; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* An array of errors |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $errors = array(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* An array of the current job |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $job = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The current processing URL |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $data_url; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Download constructor. |
|
59
|
|
|
* @param FileTransfer $file_transfer |
|
60
|
|
|
* @param Install $install |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(FileTransfer $file_transfer, Install $install) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->install = $install; |
|
65
|
|
|
$this->file_transfer = $file_transfer; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Processes one job iteration |
|
70
|
|
|
* @param array $job |
|
71
|
|
|
*/ |
|
72
|
|
|
public function process(array &$job) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->job = &$job; |
|
75
|
|
|
$this->errors = array(); |
|
76
|
|
|
|
|
77
|
|
|
if (empty($this->job['data']['sources'][$this->job['done']])) { |
|
78
|
|
|
$this->job['status'] = false; |
|
79
|
|
|
$this->job['done'] = $this->job['total']; |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->install(); |
|
82
|
|
|
$this->job['done']++; |
|
83
|
|
|
$this->job['errors'] += $this->countErrors(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Install a module |
|
89
|
|
|
* @return boolean |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function install() |
|
92
|
|
|
{ |
|
93
|
|
|
$file = $this->download(); |
|
94
|
|
|
|
|
95
|
|
|
if (empty($file)) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$result = $this->install->fromZip($file); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
if ($result !== true) { |
|
102
|
|
|
$this->setError($result); |
|
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->install->isUpdate()) { |
|
107
|
|
|
$this->job['updated']++; |
|
108
|
|
|
} else { |
|
109
|
|
|
$this->job['inserted']++; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Download a module file from a remote source |
|
117
|
|
|
* @return boolean|string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function download() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->data_url = $this->job['data']['sources'][$this->job['done']]; |
|
122
|
|
|
|
|
123
|
|
|
$filename = md5($this->data_url); |
|
124
|
|
|
$destination = gplcart_file_private_module('installer', "$filename.zip", true); |
|
125
|
|
|
$result = $this->file_transfer->download($this->data_url, 'zip', $destination); |
|
126
|
|
|
|
|
127
|
|
|
if ($result === true) { |
|
128
|
|
|
return $this->file_transfer->getTransferred(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->setError($result); |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns a total number of errors and logs them |
|
137
|
|
|
* @return integer |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function countErrors() |
|
140
|
|
|
{ |
|
141
|
|
|
$count = 0; |
|
142
|
|
|
foreach ($this->errors as $url => $errors) { |
|
143
|
|
|
$errors = array_filter($errors); |
|
144
|
|
|
$count += count($errors); |
|
145
|
|
|
$this->logErrors($url, $errors); |
|
146
|
|
|
} |
|
147
|
|
|
return $count; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Logs all errors happened for the URL |
|
152
|
|
|
* @param integer $url |
|
153
|
|
|
* @param array $errors |
|
154
|
|
|
* @return boolean |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function logErrors($url, array $errors) |
|
157
|
|
|
{ |
|
158
|
|
|
$data = array($url, implode(PHP_EOL, $errors)); |
|
159
|
|
|
return gplcart_file_csv($this->job['log']['errors'], $data); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Sets a error |
|
164
|
|
|
* @param string|array $error |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function setError($error) |
|
167
|
|
|
{ |
|
168
|
|
|
settype($error, 'array'); |
|
169
|
|
|
$existing = empty($this->errors[$this->data_url]) ? array() : $this->errors[$this->data_url]; |
|
170
|
|
|
$this->errors[$this->data_url] = gplcart_array_merge($existing, $error); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.