Completed
Push — master ( 79fd8a...81e819 )
by Christian
05:05
created

ExporterToArrayFile::zipFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Omatech\Mage\Core\Adapters\Translations\Exporters;
4
5
use ZipArchive;
6
use Carbon\Carbon;
7
use Illuminate\Support\Facades\Storage;
8
use Omatech\Mage\Core\Domains\Translations\Contracts\ExportTranslationInterface;
9
10
class ExporterToArrayFile implements ExportTranslationInterface
11
{
12
    /**
13
     * @param $translations
14
     * @return string
15
     */
16
    public function export(array $translations): string
17
    {
18
        Storage::deleteDirectory("translations/tmp");
19
20
        foreach ($translations as $language => $values) {
21
            $groupedTranslations = $this->groupTranslations($values->toArray());
22
            $this->storeInFiles($language, $groupedTranslations);
23
        }
24
25
        $path = $this->zipFiles();
26
27
        Storage::deleteDirectory("translations/tmp");
28
29
        return $path;
30
    }
31
32
    /**
33
     * @param array $translations
34
     * @return array
35
     */
36
    private function groupTranslations(array $translations): array
37
    {
38
        $groupedTranslations = [];
39
40
        foreach ($translations as $translation) {
41
            $groupedTranslations[$translation['group']][] = [$translation['key'] => $translation['value']];
42
        }
43
44
        return $groupedTranslations;
45
    }
46
47
    /**
48
     * @param $language
49
     * @param $groupedTranslations
50
     */
51
    private function storeInFiles($language, $groupedTranslations): void
52
    {
53
        foreach ($groupedTranslations as $group => $keys) {
54
            $file = "<?php\n\nreturn [\n\n";
55
            $file .= "    /*\n";
56
            $file .= "    |--------------------------------------------------------------------------\n";
57
            $file .= "    | $group\n";
58
            $file .= "    |--------------------------------------------------------------------------\n";
59
            $file .= "    */\n";
60
61
            Storage::append("translations/tmp/$language/$group.php", $file);
62
63
            foreach ($keys as $key) {
64
                $currentKey = key($key);
65
                $currentValue = $key[$currentKey];
66
67
                Storage::append("translations/tmp/$language/$group.php", "    '$currentKey' => '$currentValue',");
68
            }
69
70
            $file = "];\n";
71
            Storage::append("translations/tmp/$language/$group.php", $file);
72
        }
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    private function zipFiles(): string
79
    {
80
        $date = Carbon::now('Europe/Madrid')->format('dmY_His');
81
        $path = storage_path('app/translations/' . $date . '_laravel.zip');
82
83
        $this->zipDir(storage_path('app/translations/tmp'), $path);
84
85
        return $path;
86
    }
87
88
    /**
89
     * @param $sourcePath
90
     * @param $outZipPath
91
     */
92
    private function zipDir($sourcePath, $outZipPath): void
93
    {
94
        $pathInfo = pathInfo($sourcePath);
95
        $parentPath = $pathInfo['dirname'] . '/' . $pathInfo['basename'];
96
        $z = new ZipArchive();
97
        $z->open($outZipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
98
        self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
99
        $z->close();
100
    }
101
102
    /**
103
     * @param $folder
104
     * @param $zipFile
105
     * @param $exclusiveLength
106
     */
107
    private static function folderToZip($folder, &$zipFile, $exclusiveLength): void
108
    {
109
        $handle = opendir($folder);
110
        while (false !== $f = readdir($handle)) {
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        while (false !== $f = readdir(/** @scrutinizer ignore-type */ $handle)) {
Loading history...
111
            if ($f !== '.' && $f !== '..') {
112
                $filePath = "$folder/$f";
113
                // Remove prefix from file path before add to zip.
114
                $localPath = substr($filePath, $exclusiveLength);
115
                if (is_file($filePath)) {
116
                    $zipFile->addFile($filePath, $localPath);
117
                } elseif (is_dir($filePath)) {
118
                    // Add sub-directory.
119
                    $zipFile->addEmptyDir($localPath);
120
                    self::folderToZip($filePath, $zipFile, $exclusiveLength);
121
                }
122
            }
123
        }
124
        closedir($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
        closedir(/** @scrutinizer ignore-type */ $handle);
Loading history...
125
    }
126
}
127