Completed
Push — master ( 71bb3f...8f261b )
by Christian
03:14
created

ImporterFromArrayFilesZipped   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 125
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 125
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 17 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[] = include $file;
62 3
            return $parsedFile;
63 4
        }, $files);
64
65 4
        return array_filter($parsedFile);
66
    }
67
68
    /**
69
     * @param $parsedFile
70
     * @return mixed
71
     */
72 4
    private function joinArrayValues($parsedFile)
73
    {
74 4
        foreach ($parsedFile as &$locale) {
75 3
            foreach ($locale[array_key_last($locale)] as $key => $value) {
76 3
                if (is_array($value)) {
77 1
                    $value = Arr::dot($value);
78 1
                    foreach ($value as $arrKey => $arrValue) {
79 1
                        $locale[array_key_last($locale)][$key.'.'.$arrKey] = $arrValue;
80
                    }
81 1
                    unset($locale[array_key_last($locale)][$key]);
82
                }
83
            }
84
        }
85
86 4
        return $parsedFile;
87
    }
88
89
    /**
90
     * @param $unzipPath
91
     * @param $locale
92
     * @return array
93
     */
94 4
    private function parseTranslations($unzipPath, $locale)
95
    {
96 4
        $files = $this->getDirContents($unzipPath);
97 4
        $parsedFile = $this->parseFile($files, $locale);
98 4
        $parsedFile = $this->joinArrayValues($parsedFile);
99
100 4
        $translations = [];
101
102 4
        foreach ($parsedFile as $translation) {
103 3
            $group = array_slice($translation, 1, sizeof($translation) -2);
104 3
            $group = implode('.', $group);
105 3
            foreach ($translation[array_key_last($translation)] as $key => $value) {
106 3
                $translations[$group.'.'.$key]['key'] = $group.'.'.$key;
107 3
                $translations[$group.'.'.$key]['value'][$translation[array_key_first($translation)]] = $value;
108
            }
109
        }
110
111 4
        return array_values($translations);
112
    }
113
114
    /**
115
     * @param $dir
116
     * @param array $results
117
     * @return array
118
     */
119 4
    private function getDirContents($dir, &$results = [])
120
    {
121 4
        $files = scandir($dir);
122
123 4
        foreach ($files as $key => $value) {
124 4
            $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
125 4
            if (!is_dir($path)) {
126 4
                $results[] = $path;
127 4
            } elseif ($value != "." && $value != "..") {
128 4
                $this->getDirContents($path, $results);
129
            }
130
        }
131
132 4
        return $results;
133
    }
134
}
135