Download::install()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 0
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);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->download() on line 93 can also be of type boolean; however, gplcart\modules\installe...dels\Install::fromZip() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
100
101
        if ($result !== true) {
102
            $this->setError($result);
0 ignored issues
show
Documentation introduced by
$result is of type null|boolean, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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