Completed
Push — master ( 021a0b...ed1d1c )
by Alexey
07:23 queued 02:39
created

Images::parse()   D

Complexity

Conditions 19
Paths 301

Size

Total Lines 60
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 0
Metric Value
cc 19
eloc 44
c 5
b 3
f 0
nc 301
nop 0
dl 0
loc 60
rs 4.6281

How to fix   Long Method    Complexity   

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
/**
4
 * Item images parser
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2016 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Exchange1c\Parser\Item;
13
14
class Images extends \Migrations\Parser {
15
16
  public function parse() {
17
    if (is_null($this->data)) {
18
      return;
19
    }
20
    $value = $this->data;
21
    if (!is_array($value)) {
22
      $value = [$value];
23
    }
24
    $ids = [];
25
    $dir = pathinfo($this->object->walker->migtarionLog->source, PATHINFO_DIRNAME);
26
    $this->model->image_file_id = 0;
27
    foreach ($value as $key => $imagePath) {
28
      if (!$imagePath || !file_exists($dir . '/' . $imagePath)) {
29
        continue;
30
      }
31
      $notEq = true;
32
      $md5Cur = md5_file($dir . '/' . $imagePath);
33
      foreach ($this->model->images as $imageId => $image) {
34
        if (!$image->file) {
35
          $image->delete();
36
          continue;
37
        }
38
        $file = $image->file;
39
        $md5File = '';
40
        if ($file->md5) {
41
          $md5File = $file->md5;
42
        } elseif (file_exists($file->getRealPath())) {
43
          $md5File = $file->md5 = md5_file($file->getRealPath());
44
          $file->save();
45
        }
46
47
        if ($file && file_exists($file->getRealPath()) && $md5Cur == $md5File) {
48
          $notEq = false;
49
          $ids[] = $imageId;
50
          break;
51
        }
52
      }
53
      if ($notEq) {
54
        $file_id = \App::$primary->files->uploadFromUrl($dir . '/' . $imagePath, ['accept_group' => 'image', 'upload_code' => 'MigrationUpload']);
55
        if ($file_id) {
56
          $image = new \Ecommerce\Item\Image([
57
              'item_id' => $this->model->pk(),
58
              'file_id' => $file_id
59
          ]);
60
          $image->save();
61
          $ids[] = $image->id;
62
        }
63
      } else {
64
        $image->weight = $key;
65
      }
66
      if ($image && !$this->model->image_file_id) {
67
        $this->model->image_file_id = $image->file_id;
68
      }
69
    }
70
    foreach ($this->model->images as $imageId => $image) {
71
      if (!in_array($imageId, $ids)) {
72
        $image->delete();
73
      }
74
    }
75
  }
76
77
}
78