Completed
Push — master ( 9930d3...e136e6 )
by Alexey
05:19
created

Migrations   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 31.25 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 20
loc 64
rs 10
wmc 11
lcom 2
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
B startMigration() 0 25 2
A findObject() 10 10 3
A findParse() 10 10 3
A getMigrationObject() 0 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Data Migrations class
5
 *
6
 * Migration from file, to file, from web, to web
7
 *
8
 * @author Alexey Krupskiy <[email protected]>
9
 * @link http://inji.ru/
10
 * @copyright 2015 Alexey Krupskiy
11
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
12
 */
13
class Migrations extends \Module
14
{
15
    public $ids = [];
16
    public $migrationObjects = [];
17
18
    public function startMigration($migrationId, $mapId, $filePath)
19
    {
20
        $log = new \Migrations\Log();
21
        $log->migration_id = $migrationId;
22
        $log->migration_map_id = $mapId;
23
        $log->source = $filePath;
24
        $log->save();
25
26
        $reader = new Migrations\Reader\Xml();
27
        if (!$reader->loadData($filePath)) {
28
            $event = new Migrations\Log\Event();
29
            $event->log_id = $log->id;
30
            $event->type = 'load_data_error';
31
            $event->save();
32
            return false;
33
        }
34
        $walker = new \Migrations\Walker();
35
        $walker->migration = Migrations\Migration::get($migrationId);
36
        $walker->map = Migrations\Migration\Map::get($mapId);
37
        $walker->data = $reader->getArray();
38
        $walker->migtarionLog = $log;
39
        $walker->walk();
40
        $log->result = 'success';
41
        $log->save();
42
    }
43
44 View Code Duplication
    function findObject($parseId, $type)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
    {
46
        if (empty($this->ids['parseIds'][$type])) {
47
            $this->ids['parseIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'parse_id']);
48
            ksort($this->ids['parseIds'][$type]);
49
        }
50
        if (!empty($this->ids['parseIds'][$type][$parseId])) {
51
            return $this->ids['parseIds'][$type][$parseId];
52
        }
53
    }
54
55 View Code Duplication
    function findParse($objectId, $type)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
        if (empty($this->ids['objectIds'][$type])) {
58
            $this->ids['objectIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'object_id']);
59
            ksort($this->ids['objectIds'][$type]);
60
        }
61
        if (!empty($this->ids['objectIds'][$type][$objectId])) {
62
            return $this->ids['objectIds'][$type][$objectId];
63
        }
64
    }
65
66
    function getMigrationObject($objectId)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
    {
68
        if (empty($this->migrationObjects)) {
69
            $this->migrationObjects = \Migrations\Migration\Object::getList();
70
        }
71
        if (!empty($this->migrationObjects[$objectId])) {
72
            return $this->migrationObjects[$objectId];
73
        }
74
    }
75
76
}
77