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

Migrations::findObject()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 2
dl 10
loc 10
rs 9.4285
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