Completed
Push — master ( 52052f...b5e4b5 )
by Ben
04:12
created

LaravelTranslationsReader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 114
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A read() 0 22 3
A get() 0 4 1
A flattenPerFile() 0 6 1
A flatten() 0 6 1
A prependGroupkey() 0 16 4
A getSquantoLangPath() 0 5 2
1
<?php
2
3
namespace Thinktomorrow\Squanto\Services;
4
5
use Illuminate\Support\Arr;
6
use League\Flysystem\Filesystem;
7
8
class LaravelTranslationsReader
9
{
10
    /**
11
     * @var \Illuminate\Support\Collection
12
     */
13
    private $translations;
14
15
    /**
16
     * Local filesystem. Already contains the path to our translation files
17
     * e.g. storage/app/trans
18
     *
19
     * @var Filesystem
20
     */
21
    private $filesystem;
22
    private $path;
23
24 27
    public function __construct(Filesystem $filesystem)
25
    {
26 27
        $this->filesystem = $filesystem;
27 27
        $this->path = $this->getSquantoLangPath();
28 27
        $this->translations = collect();
29 27
    }
30
31
    /**
32
     * Read the lang files for a specific locale
33
     *
34
     * @param $locale
35
     * @param array $excluded filenames that should be excluded from the read
36
     * @return \Illuminate\Support\Collection
37
     */
38 27
    public function read($locale, array $excluded = [])
39
    {
40
        // Empty our translations for a new read
41 27
        $this->translations = collect();
42
43 27
        $files = $this->filesystem->listContents($locale);
44
45 27
        foreach ($files as $file) {
46 24
47 3
            $filename = substr($file['path'],strrpos($file['path'],'/'));
48
            $filename = ltrim($filename,'/');
49
            $filename = str_replace('.php','',$filename);
50 24
51
            if (in_array($filename, $excluded)) {
52
                continue;
53 27
            }
54
55
            $this->translations[$filename] = require $this->path . DIRECTORY_SEPARATOR . $file['path'];
56
        }
57
58
        return $this;
59 3
    }
60
61 3
    /**
62
     * @return \Illuminate\Support\Collection
63
     */
64
    public function get()
65
    {
66
        return $this->translations;
67
    }
68
69 3
    /**
70
     * Flatten per file a multi-dimensional associative array with dots.
71
72 3
     * @return \Illuminate\Support\Collection
73 3
     */
74
    public function flattenPerFile()
75
    {
76
        return $this->translations->map(function ($values) {
77
            return Arr::dot($values);
78
        });
79
    }
80
81
    /**
82
     * Flatten all files which also flattens the filenames (groups) with dots.
83
     * Note: This will remove any empty language files
84 21
     *
85 18
     * @return \Illuminate\Support\Collection
86 21
     */
87
    public function flatten()
88
    {
89
        return $this->translations->map(function ($values, $groupkey) {
90
            return $this->prependGroupkey($values, $groupkey);
91
        })->collapse();
92
    }
93
94 18
    /**
95
     * @param $values
96 18
     * @param $groupkey
97 18
     * @return array
98
     */
99 18
    private function prependGroupkey($values, $groupkey)
100
    {
101 18
        $values = Arr::dot($values);
102
        $combined = [];
103
104
        foreach ($values as $key => $value) {
105 18
            // Empty arrays will be ignored in our flattening
106
            if (is_array($value) && empty($value)) {
107
                continue;
108 18
            }
109
110
            $combined[$groupkey . '.' . $key] = $value;
111
        }
112
113
        return $combined;
114
    }
115
116
    private function getSquantoLangPath()
117
    {
118
        $path = config('squanto.lang_path');
119
        return is_null($path) ? app('path.lang') : $path;
120
    }
121
}
122