Completed
Push — master ( 5fa5e5...71d4f3 )
by Christian
03:25
created

ImporterFromExcel::parseTranslations()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.2888
c 0
b 0
f 0
cc 5
nc 8
nop 2
crap 5
1
<?php
2
3
namespace Omatech\Mage\Core\Adapters\Translations\Importers;
4
5
use Box\Spout\Common\Type;
6
use Box\Spout\Reader\ReaderFactory;
7
use Omatech\Mage\Core\Domains\Translations\Contracts\ImportTranslationInterface;
8
use Rap2hpoutre\FastExcel\FastExcel;
9
10
class ImporterFromExcel implements ImportTranslationInterface
11
{
12
    /**
13
     * @param string $path
14
     * @param string $locale
15
     * @return array
16
     */
17 3
    public function import(string $path, string $locale = ''): array
18
    {
19 3
        $sheetNames = $this->getSheets($path, $locale);
20 3
        $sheets = $this->importSheets($path, $sheetNames);
21
22 3
        if (count($sheetNames) <= 0) {
23 1
            return [];
24
        }
25
26 2
        return $this->parseTranslations($sheetNames, $sheets);
27
    }
28
29
    /**
30
     * @param string $path
31
     * @param string $locale
32
     * @return array
33
     */
34 3
    private function getSheets(string $path, string $locale = ''): array
35
    {
36 3
        $sheetNames = [];
37
38 3
        $reader = ReaderFactory::create(Type::XLSX);
39 3
        $reader->open($path);
40
41 3
        foreach ($reader->getSheetIterator() as $sheet) {
42 3
            array_push($sheetNames, $sheet->getName());
43
        }
44
45 3
        if ('' != $locale && in_array($locale, $sheetNames)) {
46 1
            $sheetNames = [0 => $locale];
47 2
        } elseif ('' != $locale && ! in_array($locale, $sheetNames)) {
48 1
            $sheetNames = [];
49
        }
50
51 3
        return $sheetNames;
52
    }
53
54
    /**
55
     * @param string $path
56
     * @param array $sheetNames
57
     * @return array
58
     */
59 3
    private function importSheets(string $path, array $sheetNames): array
60
    {
61 3
        $sheets = (new FastExcel());
62
63 3
        if (count($sheetNames) > 1) {
64 1
            $sheets = $sheets->importSheets($path)->toArray();
65
        } else {
66 2
            $sheets = [$sheets->sheet(key($sheetNames) + 1)->import($path)->toArray()];
67
        }
68
69 3
        return $sheets;
70
    }
71
72
    /**
73
     * @param array $sheetNames
74
     * @param array $sheets
75
     * @return array
76
     */
77 2
    private function parseTranslations(array $sheetNames, array $sheets): array
78
    {
79 2
        $translations = [];
80 2
        $parsedKeys = [];
81
82 2
        foreach ($sheets as $lang => $trans) {
83 2
            $translations[$sheetNames[$lang]] = $trans;
84
        }
85
86 2
        foreach ($translations as $lang => $keys) {
87 2
            foreach ($keys as $key) {
88 2
                if ('' != $key['key2']) {
89 2
                    $parsedKeys[$key['key2']]['key'] = $key['key2'];
90 2
                    $parsedKeys[$key['key2']]['value'][$lang] = $key['value'];
91
                }
92
            }
93
        }
94
95 2
        return array_values($parsedKeys);
96
    }
97
}
98