Completed
Push — master ( 6ac12a...b2cb7f )
by Alexey
05:07
created

Images::parse()   C

Complexity

Conditions 10
Paths 12

Size

Total Lines 23
Code Lines 16

Duplication

Lines 3
Ratio 13.04 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 23
rs 5.6534
cc 10
eloc 16
nc 12
nop 0

How to fix   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
    {
18
        $value = (string) $this->reader;
19
        $notEq = true;
20
        $dir = pathinfo($this->reader->source, PATHINFO_DIRNAME);
21
        foreach ($this->object->model->images as $image) {
22
            $file = $image->file;
23 View Code Duplication
            if ($file && $value && file_exists($dir . '/' . $value) && file_exists(\App::$primary->path . $file->path) && md5_file($dir . '/' . $value) == md5_file(\App::$primary->path . $file->path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
                $notEq = false;
25
            }
26
        }
27
        if ($notEq) {
28
            $file_id = \App::$primary->files->uploadFromUrl($dir . '/' . $value, ['accept_group' => 'image', 'upload_code' => 'MigrationUpload']);
29
            $image = new \Ecommerce\Item\Image([
30
                'item_id' => $this->object->model->pk(),
31
                'file_id' => $file_id
32
            ]);
33
            $image->save();
34
        }
35
        if ($image && !$this->object->model->image_file_id) {
36
            $this->object->model->image_file_id = $image->file_id;
37
        }
38
    }
39
40
}
41