Completed
Push — master ( 1322c6...ce98c1 )
by Ben
03:33
created

LaravelTranslationsReader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.04%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 114
ccs 50
cts 51
cp 0.9804
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 8
47 17
            $filename = substr($file['path'],strrpos($file['path'],'/'));
48 16
            $filename = ltrim($filename,'/');
49 16
            $filename = rtrim($filename,'.php');
50 8
51 16
            if (in_array($filename, $excluded)) {
52 2
                continue;
53 9
            }
54
55 16
            $this->translations[$filename] = require $this->path . DIRECTORY_SEPARATOR . $file['path'];
56
        }
57
58 18
        return $this;
59 1
    }
60
61 1
    /**
62
     * @return \Illuminate\Support\Collection
63
     */
64 2
    public function get()
65
    {
66 2
        return $this->translations;
67
    }
68
69 1
    /**
70
     * Flatten per file a multi-dimensional associative array with dots.
71
72 1
     * @return \Illuminate\Support\Collection
73 1
     */
74 2
    public function flattenPerFile()
75
    {
76
        return $this->translations->map(function ($values) {
77 2
            return Arr::dot($values);
78 2
        });
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 7
     *
85 6
     * @return \Illuminate\Support\Collection
86 7
     */
87
    public function flatten()
88
    {
89 14
        return $this->translations->map(function ($values, $groupkey) {
90 12
            return $this->prependGroupkey($values, $groupkey);
91 14
        })->collapse();
92
    }
93
94 6
    /**
95
     * @param $values
96 6
     * @param $groupkey
97 6
     * @return array
98
     */
99 18
    private function prependGroupkey($values, $groupkey)
100
    {
101 18
        $values = Arr::dot($values);
102 12
        $combined = [];
103
104 12
        foreach ($values as $key => $value) {
105 6
            // Empty arrays will be ignored in our flattening
106 12
            if (is_array($value) && empty($value)) {
107
                continue;
108 6
            }
109
110 12
            $combined[$groupkey . '.' . $key] = $value;
111
        }
112
113 12
        return $combined;
114
    }
115
116 18
    private function getSquantoLangPath()
117
    {
118 18
        $path = config('squanto.lang_path');
119 18
        return is_null($path) ? app('path.lang') : $path;
120
    }
121
}
122