GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Parser::loadMetaData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace SimaLand\API\Parser;
4
5
use SimaLand\API\AbstractList;
6
use SimaLand\API\Object;
7
8
/**
9
 * Загрузка и сохранение всех записей сущностей.
10
 *
11
 * ```php
12
 *
13
 * $client = new \SimaLand\API\Rest\Client([
14
 *     'login' => 'login',
15
 *     'password' => 'password'
16
 * ]);
17
 * $itemList = new \SimaLand\API\Entities\ItemList($client);
18
 * $itemStorage = new Json(['filename' => 'path/to/item.txt']);
19
 * $categoryList = new \SimaLand\API\Entities\CategoryList($client);
20
 * $categoryStorage = new Json(['filename' => 'path/to/category.txt']);
21
 * $parser = new Parser(['metaFilename' => 'path/to/file']);
22
 * $parser->addEntity($itemList, $itemStorage);
23
 * $parser->addEntity($categoryList, $categoryStorage);
24
 * $parser->run();
25
 *
26
 * ```
27
 */
28
class Parser extends Object
29
{
30
    /**
31
     * @var array
32
     */
33
    protected $list = [];
34
35
    /**
36
     * Путь до файла с мета данными.
37
     *
38
     * @var string
39
     */
40
    protected $metaFilename;
41
42
    /**
43
     * Мета данные.
44
     *
45
     * @var array
46
     */
47
    protected $metaData = [];
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function __construct(array $options = [])
53
    {
54
        if (empty($options['metaFilename'])) {
55
            throw new \Exception('Param "metaFilename" can`t be empty');
56
        }
57
        $this->metaFilename = $options['metaFilename'];
58
        unset($options['metaFilename']);
59
        parent::__construct($options);
60
    }
61
62
    /**
63
     * @param AbstractList $entity
64
     * @param StorageInterface $storage
65
     * @return Parser
66
     */
67
    public function addEntity(AbstractList $entity, StorageInterface $storage)
68
    {
69
        $this->list[] = [
70
            'entity' => $entity,
71
            'storage' => $storage
72
        ];
73
        return $this;
74
    }
75
76
    /**
77
     * Сбросить мета данные.
78
     *
79
     * @return Parser
80
     */
81
    public function reset()
82
    {
83
        if (file_exists($this->metaFilename)) {
84
            unlink($this->metaFilename);
85
        }
86
        return $this;
87
    }
88
89
    /**
90
     * Запустить парсер.
91
     *
92
     * @param bool|false $continue Продолжить парсить с место обрыва.
93
     */
94
    public function run($continue = true)
95
    {
96
        $logger = $this->getLogger();
97
        $this->loadMetaData();
98
        foreach ($this->list as $el) {
99
            /** @var AbstractList $entity */
100
            $entity = $el['entity'];
101
            $entityName = $entity->getEntity();
102
            if ($continue && isset($this->metaData[$entityName])) {
103
                if (isset($this->metaData[$entityName]['finish']) && $this->metaData[$entityName]['finish']) {
104
                    continue;
105
                }
106
                $entity->addGetParams($this->metaData[$entityName]['params']);
107
                $entity->setCountIteration($this->metaData[$entityName]['countIteration']);
108
            }
109
            /** @var StorageInterface $storage */
110
            $storage = $el['storage'];
111
            $logger->info("Parse \"{$entityName}\"");
112
            foreach ($entity as $key => $record) {
113
                if ($continue) {
114
                    $this->fillAndSaveMetaData($entity);
115
                }
116
                $storage->save($record);
117
            }
118
            $logger->info("Finish parse \"{$entityName}\"");
119
            if ($continue) {
120
                $this->finishParseEntity($entity);
121
                $this->saveMetaData();
122
            }
123
        }
124
    }
125
126
    /**
127
     * Загрузить мета данные.
128
     */
129
    protected function loadMetaData()
130
    {
131
        if (!file_exists($this->metaFilename)) {
132
            return;
133
        }
134
        $data = file_get_contents($this->metaFilename);
135
        $this->metaData = (array)json_decode($data, true);
136
    }
137
138
    /**
139
     * Заполнить мета данные.
140
     *
141
     * @param AbstractList $entity
142
     */
143
    protected function fillAndSaveMetaData(AbstractList $entity)
144
    {
145
        $entityName = $entity->getEntity();
146
        if (!isset($this->metaData[$entityName])) {
147
            $this->metaData[$entityName] = [
148
                'params' => [],
149
                'countIteration' => 0
150
            ];
151
            return;
152
        }
153
154
        $countIteration = $entity->getCountIteration();
155
        if ($countIteration != $this->metaData[$entityName]['countIteration']) {
156
            $this->metaData[$entityName]['params'][$entity->keyThreads] = $countIteration * $entity->countThreads + 1;
157
            $this->metaData[$entityName]['countIteration'] = $countIteration;
158
            $this->saveMetaData();
159
        }
160
    }
161
162
    /**
163
     * Записать в мета данные об успешном сохранение сущности.
164
     *
165
     * @param AbstractList $entity
166
     */
167
    protected function finishParseEntity(AbstractList $entity)
168
    {
169
        $entityName = $entity->getEntity();
170
        if (!isset($this->metaData[$entityName])) {
171
            $this->metaData[$entityName] = [];
172
        }
173
        $this->metaData[$entityName]['finish'] = true;
174
    }
175
176
    /**
177
     * Сохранить мета данные.
178
     */
179
    protected function saveMetaData()
180
    {
181
        $data = json_encode($this->metaData);
182
        file_put_contents($this->metaFilename, $data);
183
    }
184
}
185