Complex classes like Import often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Import, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Import |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Config class instance |
||
28 | * @var \gplcart\core\Config $config |
||
29 | */ |
||
30 | protected $config; |
||
31 | |||
32 | /** |
||
33 | * Translation UI model instance |
||
34 | * @var \gplcart\core\models\Translation $translation |
||
35 | */ |
||
36 | protected $translation; |
||
37 | |||
38 | /** |
||
39 | * User model instance |
||
40 | * @var \gplcart\core\models\User $user |
||
41 | */ |
||
42 | protected $user; |
||
43 | |||
44 | /** |
||
45 | * Validator model instance |
||
46 | * @var \gplcart\core\models\Validator $validator |
||
47 | */ |
||
48 | protected $validator; |
||
49 | |||
50 | /** |
||
51 | * File transfer model instance |
||
52 | * @var \gplcart\core\models\FileTransfer $file_transfer |
||
53 | */ |
||
54 | protected $file_transfer; |
||
55 | |||
56 | /** |
||
57 | * Product model instance |
||
58 | * @var \gplcart\core\models\Product $product |
||
59 | */ |
||
60 | protected $product; |
||
61 | |||
62 | /** |
||
63 | * CSV class instance |
||
64 | * @var \gplcart\modules\import\helpers\Csv $csv |
||
65 | */ |
||
66 | protected $csv; |
||
67 | |||
68 | /** |
||
69 | * An array of errors |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $errors = array(); |
||
73 | |||
74 | /** |
||
75 | * An array of parsed CSV rows |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $rows = array(); |
||
79 | |||
80 | /** |
||
81 | * An array of the current job |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $job = array(); |
||
85 | |||
86 | /** |
||
87 | * An array of the current line data |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $data = array(); |
||
91 | |||
92 | /** |
||
93 | * @param Config $config |
||
94 | * @param Product $product |
||
95 | * @param User $user |
||
96 | * @param FileTransfer $file_transfer |
||
97 | * @param Translation $translation |
||
98 | * @param Validator $validator |
||
99 | * @param Csv $csv |
||
100 | */ |
||
101 | public function __construct(Config $config, Product $product, User $user, |
||
113 | |||
114 | /** |
||
115 | * Processes one import iteration |
||
116 | * @param array $job |
||
117 | */ |
||
118 | public function process(array &$job) |
||
138 | |||
139 | /** |
||
140 | * Returns a total number of errors and logs them |
||
141 | * @return integer |
||
142 | */ |
||
143 | protected function countErrors() |
||
144 | { |
||
145 | $count = 0; |
||
146 | |||
147 | foreach ($this->errors as $line => $errors) { |
||
148 | $errors = array_filter($errors); |
||
149 | $count += count($errors); |
||
150 | $this->logErrors($line, $errors); |
||
151 | } |
||
152 | |||
153 | return $count; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Logs all errors happened on the line |
||
158 | * @param integer $line |
||
159 | * @param array $errors |
||
160 | * @return boolean |
||
161 | */ |
||
162 | protected function logErrors($line, array $errors) |
||
163 | { |
||
164 | $line_message = $this->translation->text('Line @num', array('@num' => $line)); |
||
165 | return gplcart_file_csv($this->job['log']['errors'], array($line_message, implode(PHP_EOL, $errors))); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Finishes the current iteration |
||
170 | */ |
||
171 | protected function finish() |
||
172 | { |
||
173 | if (empty($this->rows)) { |
||
174 | $this->job['status'] = false; |
||
175 | $this->job['done'] = $this->job['total']; |
||
176 | } else { |
||
177 | $offset = $this->csv->getOffset(); |
||
178 | $this->job['context']['offset'] = $offset; |
||
179 | $this->job['done'] = $offset ? $offset : $this->job['total']; |
||
180 | } |
||
181 | |||
182 | $this->job['errors'] += $this->countErrors(); |
||
183 | $vars = array('@num' => $this->job['context']['line']); |
||
184 | $this->job['message']['process'] = $this->translation->text('Last processed line: @num', $vars); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Adds/updates an array of products |
||
189 | */ |
||
190 | protected function import() |
||
198 | |||
199 | /** |
||
200 | * Prepare a data taken from a CSV row |
||
201 | * @param array $row |
||
202 | * @return boolean |
||
203 | */ |
||
204 | protected function prepare(array $row) |
||
228 | |||
229 | /** |
||
230 | * Validates a data to be imported |
||
231 | * @return boolean |
||
232 | */ |
||
233 | public function validate() |
||
260 | |||
261 | /** |
||
262 | * Sets a error |
||
263 | * @param string|array $error |
||
264 | */ |
||
265 | protected function setError($error) |
||
273 | |||
274 | /** |
||
275 | * Adds/updates a single product |
||
276 | */ |
||
277 | protected function set() |
||
291 | |||
292 | /** |
||
293 | * Returns an array of values from a string using a delimiter character |
||
294 | * @param string $string |
||
295 | * @return array |
||
296 | */ |
||
297 | protected function explodeValues($string) |
||
302 | |||
303 | /** |
||
304 | * Returns an array of image data |
||
305 | * @param string $string |
||
306 | * @return array |
||
307 | */ |
||
308 | protected function getImages($string) |
||
321 | |||
322 | /** |
||
323 | * Validates and returns a relative image path. |
||
324 | * If given an absolute URL, the file will be downloaded |
||
325 | * @param string $image |
||
326 | * @return boolean|string |
||
327 | */ |
||
328 | protected function getImagePath($image) |
||
353 | |||
354 | /** |
||
355 | * Downloads a remote image |
||
356 | * @param string $url |
||
357 | * @return boolean|string |
||
358 | */ |
||
359 | protected function downloadImage($url) |
||
373 | |||
374 | } |
||
375 |