ImporterFromArrayFilesZipped   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 126
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A import() 0 13 1
A unzip() 0 9 2
A parseFile() 0 18 3
A joinArrayValues() 0 16 5
A parseTranslations() 0 19 3
A getDirContents() 0 15 5
1
<?php
2
3
namespace Omatech\Mage\Core\Adapters\Translations\Importers;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\Storage;
7
use Omatech\Mage\Core\Domains\Translations\Contracts\ImportTranslationInterface;
8
use ZipArchive;
9
10
class ImporterFromArrayFilesZipped implements ImportTranslationInterface
11
{
12
    /**
13
     * @param string $path
14
     * @param string $locale
15
     * @return array
16
     */
17 4
    public function import(string $path, string $locale = ''): array
18
    {
19 4
        Storage::deleteDirectory('translations/tmp');
20 4
        $unzipPath = storage_path('app/translations/tmp');
21
22 4
        $this->unzip($path, $unzipPath);
23
24 4
        $translations = $this->parseTranslations($unzipPath, $locale);
25
26 4
        Storage::deleteDirectory('translations/tmp');
27
28 4
        return $translations;
29
    }
30
31
    /**
32
     * @param string $path
33
     * @param string $unzipPath
34
     */
35 4
    public function unzip(string $path, string $unzipPath)
36
    {
37 4
        $zip = new ZipArchive;
38
39 4
        if ($zip->open($path) === true) {
40 4
            $zip->extractTo($unzipPath);
41 4
            $zip->close();
42
        }
43 4
    }
44
45
    /**
46
     * @param $files
47
     * @param $locale
48
     * @return array
49
     */
50 4
    private function parseFile($files, $locale)
51
    {
52
        $parsedFile = array_map(function ($file) use ($locale) {
53 4
            $parsedFile = str_replace(storage_path('app/translations/tmp'), '', $file);
54 4
            $parsedFile = str_replace('.php', '', $parsedFile);
55 4
            $parsedFile = preg_split('@/@', $parsedFile, null, PREG_SPLIT_NO_EMPTY);
56
57 4
            if ('' !== $locale && $parsedFile[0] != $locale) {
58 2
                return;
59
            }
60
61 3
            $parsedFile[] = require $file;
62
63 3
            return $parsedFile;
64 4
        }, $files);
65
66 4
        return array_filter($parsedFile);
67
    }
68
69
    /**
70
     * @param $parsedFile
71
     * @return mixed
72
     */
73 4
    private function joinArrayValues($parsedFile)
74
    {
75 4
        foreach ($parsedFile as &$locale) {
76 3
            foreach ($locale[array_key_last($locale)] as $key => $value) {
77 3
                if (is_array($value)) {
78 1
                    $value = Arr::dot($value);
79 1
                    foreach ($value as $arrKey => $arrValue) {
80 1
                        $locale[array_key_last($locale)][$key.'.'.$arrKey] = $arrValue;
81
                    }
82 1
                    unset($locale[array_key_last($locale)][$key]);
83
                }
84
            }
85
        }
86
87 4
        return $parsedFile;
88
    }
89
90
    /**
91
     * @param $unzipPath
92
     * @param $locale
93
     * @return array
94
     */
95 4
    private function parseTranslations($unzipPath, $locale)
96
    {
97 4
        $files = $this->getDirContents($unzipPath);
98 4
        $parsedFile = $this->parseFile($files, $locale);
99 4
        $parsedFile = $this->joinArrayValues($parsedFile);
100
101 4
        $translations = [];
102
103 4
        foreach ($parsedFile as $translation) {
104 3
            $group = array_slice($translation, 1, count($translation) - 2);
105 3
            $group = implode('.', $group);
106 3
            foreach ($translation[array_key_last($translation)] as $key => $value) {
107 3
                $translations[$group.'.'.$key]['key'] = $group.'.'.$key;
108 3
                $translations[$group.'.'.$key]['value'][$translation[array_key_first($translation)]] = $value;
109
            }
110
        }
111
112 4
        return array_values($translations);
113
    }
114
115
    /**
116
     * @param $dir
117
     * @param array $results
118
     * @return array
119
     */
120 4
    private function getDirContents($dir, &$results = [])
121
    {
122 4
        $files = scandir($dir);
123
124 4
        foreach ($files as $value) {
125 4
            $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
126 4
            if (! is_dir($path)) {
127 4
                $results[] = $path;
128 4
            } elseif ($value != '.' && $value != '..') {
129 4
                $this->getDirContents($path, $results);
130
            }
131
        }
132
133 4
        return $results;
134
    }
135
}
136