Test Failed
Push — master ( 6548d1...8173f8 )
by Alexey
05:06
created

Migrations   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 83
Duplicated Lines 24.1 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
dl 20
loc 83
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
B startMigration() 0 25 2
A loadParseIds() 0 3 1
A loadObjectIds() 0 3 1
A findObject() 10 10 3
A findParse() 10 10 3
A getMigrationObject() 0 8 3
B saveLastAccess() 0 11 5

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
14
//define('mdebug',true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
15
class Migrations extends \Module {
16
17
    public $ids = [];
18
    public $migrationObjects = [];
19
20
    public function startMigration($migrationId, $mapId, $filePath) {
21
        $log = new \Migrations\Log();
22
        $log->migration_id = $migrationId;
23
        $log->migration_map_id = $mapId;
24
        $log->source = $filePath;
25
        $log->save();
26
27
        $reader = new Migrations\Reader\Xml();
28
        if (!$reader->loadData($filePath)) {
29
            $event = new Migrations\Log\Event();
30
            $event->log_id = $log->id;
31
            $event->type = 'load_data_error';
32
            $event->save();
33
            return false;
34
        }
35
        $walker = new \Migrations\Walker();
36
        $walker->migration = Migrations\Migration::get($migrationId);
37
        $walker->map = Migrations\Migration\Map::get($mapId);
38
        $walker->data = $reader->getArray();
39
        $walker->migtarionLog = $log;
40
        $walker->walk();
41
        $log->result = 'success';
42
        $log->save();
43
        $this->saveLastAccess();
44
    }
45
46
    public function loadParseIds($type) {
47
        $this->ids['parseIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'parse_id']);
48
    }
49
50
    public function loadObjectIds($type) {
51
        $this->ids['objectIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'object_id']);
52
    }
53
54 View Code Duplication
    public function findObject($parseId, $type) {
55
        if (empty($this->ids['parseIds'][$type])) {
56
            $this->loadParseIds($type);
57
            ksort($this->ids['parseIds'][$type]);
58
        }
59
        if (!empty($this->ids['parseIds'][$type][$parseId])) {
60
            $this->ids['parseIds'][$type][$parseId]->last_access = Date('Y-m-d H:i:s');
61
            return $this->ids['parseIds'][$type][$parseId];
62
        }
63
    }
64
65 View Code Duplication
    public function findParse($objectId, $type) {
66
        if (empty($this->ids['objectIds'][$type])) {
67
            $this->loadObjectIds($type);
68
            ksort($this->ids['objectIds'][$type]);
69
        }
70
        if (!empty($this->ids['objectIds'][$type][$objectId])) {
71
            $this->ids['objectIds'][$type][$objectId]->last_access = Date('Y-m-d H:i:s');
72
            return $this->ids['objectIds'][$type][$objectId];
73
        }
74
    }
75
76
    public function getMigrationObject($objectId) {
77
        if (empty($this->migrationObjects)) {
78
            $this->migrationObjects = \Migrations\Migration\Object::getList();
79
        }
80
        if (!empty($this->migrationObjects[$objectId])) {
81
            return $this->migrationObjects[$objectId];
82
        }
83
    }
84
85
    public function saveLastAccess() {
86
        foreach ($this->ids as $group => $types) {
87
            foreach ($types as $type => $ids) {
88
                foreach ($ids as $key => $id) {
89
                    if ($id->_changedParams) {
90
                        $id->save();
91
                    }
92
                }
93
            }
94
        }
95
    }
96
97
}
98