Issues (1519)

system/modules/Migrations/Migrations.php (9 issues)

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 $usedIds = [];
19
    public $migrationObjects = [];
20
21
    public function startMigration($migrationId, $mapId, $filePath) {
22
        $log = new \Migrations\Log();
23
        $log->migration_id = $migrationId;
0 ignored issues
show
Bug Best Practice introduced by
The property migration_id does not exist on Migrations\Log. Since you implemented __set, consider adding a @property annotation.
Loading history...
24
        $log->migration_map_id = $mapId;
0 ignored issues
show
Bug Best Practice introduced by
The property migration_map_id does not exist on Migrations\Log. Since you implemented __set, consider adding a @property annotation.
Loading history...
25
        $log->source = $filePath;
0 ignored issues
show
Bug Best Practice introduced by
The property source does not exist on Migrations\Log. Since you implemented __set, consider adding a @property annotation.
Loading history...
26
        $log->save();
27
28
        $reader = new Migrations\Reader\Xml();
29
        $logKey = App::$cur->log->start('load xml');
30
        if (!$reader->loadData($filePath)) {
31
            $event = new Migrations\Log\Event();
32
            $event->log_id = $log->id;
0 ignored issues
show
Bug Best Practice introduced by
The property log_id does not exist on Migrations\Log\Event. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property id does not exist on Migrations\Log. Since you implemented __get, consider adding a @property annotation.
Loading history...
33
            $event->type = 'load_data_error';
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on Migrations\Log\Event. Since you implemented __set, consider adding a @property annotation.
Loading history...
34
            $event->save();
35
            return false;
36
        }
37
        App::$cur->log->end($logKey);
38
        $walker = new \Migrations\Walker();
39
        $walker->migration = \Migrations\Migration::get($migrationId);
0 ignored issues
show
Documentation Bug introduced by
It seems like Migrations\Migration::get($migrationId) of type false is incompatible with the declared type Migrations\Migration of property $migration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
        $walker->map = \Migrations\Migration\Map::get($mapId);
41
        $logKey = App::$cur->log->start('parse xml');
42
        $walker->data = $reader->getArray();
43
        App::$cur->log->end($logKey);
44
        $walker->migtarionLog = $log;
45
        $walker->walk();
46
        $log->result = 'success';
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist on Migrations\Log. Since you implemented __set, consider adding a @property annotation.
Loading history...
47
        $log->save();
48
        $this->saveLastAccess();
49
    }
50
51
    public function loadParseIds($type) {
52
        $this->ids['parseIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'parse_id', 'array' => true]);
53
    }
54
55
    public function loadObjectIds($type) {
56
        $this->ids['objectIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'object_id', 'array' => true]);
57
    }
58
59
    public function findObject($parseId, $type) {
60
        if (empty($this->ids['parseIds'][$type])) {
61
            $this->loadParseIds($type);
62
            ksort($this->ids['parseIds'][$type]);
63
        }
64
        if (!empty($this->ids['parseIds'][$type][$parseId])) {
65
            $this->usedIds['parseIds'][$type][$parseId] = new \Migrations\Id($this->ids['parseIds'][$type][$parseId]);
66
            $this->usedIds['parseIds'][$type][$parseId]->last_access = Date('Y-m-d H:i:s');
67
            return $this->usedIds['parseIds'][$type][$parseId];
68
        }
69
    }
70
71
    public function findParse($objectId, $type) {
72
        if (empty($this->ids['objectIds'][$type])) {
73
            $this->loadObjectIds($type);
74
            ksort($this->ids['objectIds'][$type]);
75
        }
76
        if (!empty($this->ids['objectIds'][$type][$objectId])) {
77
            $this->usedIds['objectIds'][$type][$objectId] = new \Migrations\Id($this->ids['objectIds'][$type][$objectId]);
78
            $this->usedIds['objectIds'][$type][$objectId]->last_access = Date('Y-m-d H:i:s');
79
            return $this->usedIds['objectIds'][$type][$objectId];
80
        }
81
    }
82
83
    /**
84
     * @param \Migrations\Migration $migration
85
     * @param int|string $objectId
86
     * @param null|string $col
87
     * @return null|\Migrations\Migration\Object
88
     */
89
    public function getMigrationObject($migration, $objectId, $col = null) {
90
        if ($col === null && isset($migration->objects[$objectId])) {
91
            return $migration->objects[$objectId];
92
        }
93
        if ($col !== null && isset($migration->objects(['key' => $col])[$objectId])) {
94
            return $migration->objects(['key' => $col])[$objectId];
95
        }
96
        return null;
97
    }
98
99
    public function saveLastAccess() {
100
        foreach ($this->usedIds as $group => $types) {
101
            foreach ($types as $type => $ids) {
102
                foreach ($ids as $key => $id) {
103
                    if ($id->_changedParams) {
104
                        $id->save();
105
                    }
106
                }
107
            }
108
        }
109
    }
110
111
}
112