DB2Reader::getIndexes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace FreedomCore\TrinityCore\Support;
2
3
use FreedomCore\TrinityCore\Support\DB2Reader\FileManager;
4
use FreedomCore\TrinityCore\Support\DB2Reader\Processor\BaseFormat;
5
use FreedomCore\TrinityCore\Support\DB2Reader\Processor\WDB2;
6
use FreedomCore\TrinityCore\Support\DB2Reader\Processor\WDB5;
7
8
/**
9
 * Class DB2Reader
10
 * @package FreedomCore\TrinityCore\Support
11
 */
12
class DB2Reader
13
{
14
15
    /**
16
     * File manager instance
17
     * @var FileManager|null
18
     */
19
    protected $fileManager = null;
20
21
    /**
22
     * File system instance
23
     * @var Filesystem|null
24
     */
25
    protected $fileSystem = null;
26
27
    /**
28
     * Processor Instance
29
     * @var WDB5|WDB2|BaseFormat|null
30
     */
31
    protected $processor = null;
32
33
    /**
34
     * Language which will be used to open DBC files
35
     * @var string
36
     */
37
    protected $selectedLanguage = 'enUS';
38
39
    /**
40
     * Build used for extraction
41
     * @var null|integer
42
     */
43
    protected $build = null;
44
45
    /**
46
     * Displays whether the file is already opened or not
47
     * @var bool
48
     */
49
    protected $fileOpened = false;
50
51
    /**
52
     * DB2Reader constructor.
53
     * @param bool $initializeImmediately
54
     */
55
    public function __construct(bool $initializeImmediately = false)
56
    {
57
        $this->fileSystem = new Filesystem();
58
        $this->getLatestBuild();
59
        $this->fileManager = new FileManager($this->fileSystem, $this->getBuild());
60
        if ($initializeImmediately) {
61
            $this->fileManager->loadEverything($this->selectedLanguage);
62
        } else {
63
            $this->fileManager->loadDataDirectory();
64
            $this->fileManager->loadAvailableLanguages();
65
        }
66
    }
67
68
    /**
69
     * Get file manager instance
70
     * @return FileManager
71
     */
72
    public function getFileManager() : FileManager
73
    {
74
        return $this->fileManager;
75
    }
76
77
    /**
78
     * Set new file manager instance
79
     * @param FileManager $fileManager
80
     */
81
    public function setFileManager(FileManager $fileManager)
82
    {
83
        $this->fileManager = $fileManager;
84
    }
85
86
    /**
87
     * Get language currently used for processing
88
     * @return string
89
     */
90
    public function getLanguage() : string
91
    {
92
        return $this->selectedLanguage;
93
    }
94
95
    /**
96
     * Set language used for DBC processing
97
     * @param string $language
98
     * @param bool $throwIfAny
99
     */
100
    public function setLanguage(string $language, bool $throwIfAny = true)
101
    {
102
        if (in_array($language, $this->fileManager->getLanguageCodes())) {
103
            $this->selectedLanguage = $language;
104
            $this->fileManager->loadEverything($this->selectedLanguage);
105
        } else {
106
            if ($throwIfAny) {
107
                throw new \RuntimeException('Language ' . $language . ' does not exists, you can choose from: [' . implode(', ', $this->fileManager->getLanguageCodes()) . ']');
108
            }
109
        }
110
    }
111
112
    /**
113
     * Get latest build
114
     */
115
    public function getLatestBuild()
116
    {
117
        $folders = Filesystem::filesInFolder($this->fileSystem->getStructuresFolder());
118
        $buildNumbers = [];
119
        foreach ($folders as $folder) {
120
            $buildNumbers[] = intval(ltrim(str_replace($this->fileSystem->getStructuresFolder(), '', $folder), DIRECTORY_SEPARATOR));
121
        }
122
        $this->setBuild(max($buildNumbers));
123
    }
124
125
    /**
126
     * Get build used for processing
127
     * @return int
128
     */
129
    public function getBuild() : int
130
    {
131
        return ($this->build === null) ? 0 : $this->build;
132
    }
133
134
    /**
135
     * Set build which will be used for processing
136
     * @param int $build
137
     */
138
    public function setBuild(int $build)
139
    {
140
        $this->build = $build;
141
    }
142
143
    /**
144
     * Open requested file
145
     * @param string $fileName
146
     * @throws \Exception
147
     */
148
    public function openFile(string $fileName)
149
    {
150
        if (!$this->fileManager->isReady()) {
151
            throw new \RuntimeException('Data sources are not loaded!');
152
        }
153
        if ($this->fileManager->isFileAvailable($fileName)) {
154
            $fileName = $this->fileManager->formatFileName($fileName);
155
            $this->fileManager->setFileName($fileName);
156
            $this->fileManager->openFile($fileName);
157
            $this->processor = $this->fileManager->getProcessor();
158
            $this->fileOpened = true;
159
        } else {
160
            throw new \RuntimeException('File ' . $fileName . ' does not exists!');
161
        }
162
    }
163
164
    /**
165
     * Check if file is already opened
166
     * @return bool
167
     */
168
    public function isFileOpened() : bool
169
    {
170
        return $this->fileOpened;
171
    }
172
173
    /**
174
     * Get record by ID
175
     * @param int $id
176
     * @return array|null
177
     * @throws \Exception
178
     */
179
    public function getRecord(int $id)
180
    {
181
        return $this->processor->getRecord($id);
182
    }
183
184
    /**
185
     * Generate Records
186
     * @return \Generator
187
     * @throws \Exception
188
     */
189
    public function generateRecords() : \Generator
190
    {
191
        return $this->processor->generateRecords();
192
    }
193
194
    /**
195
     * Get indexes
196
     * @return array
197
     */
198
    public function getIndexes() : array
199
    {
200
        return $this->processor->getIDs();
201
    }
202
203
    /**
204
     * Get Processor Instance
205
     * @return BaseFormat|WDB2|WDB5|null
206
     */
207
    public function getProcessor()
208
    {
209
        return $this->processor;
210
    }
211
212
    /**
213
     * Flatten record
214
     * @param array $record
215
     * @return array
216
     */
217
    public static function flattenRecord(array $record) : array
218
    {
219
        $result = [];
220
        foreach ($record as $k => $v) {
221
            if (!is_array($v)) {
222
                $result[$k] = $v;
223
                continue;
224
            }
225
            $idx = 0;
226
            foreach ($v as $vv) {
227
                $result["$k-" . $idx++] = $vv;
228
            }
229
        }
230
        return array_values($result);
231
    }
232
}
233