LoadTranslationsFromFiles   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 176
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A handle() 0 10 1
A loadGroupLines() 0 6 1
A loadNameSpacedLines() 0 7 2
A processFiles() 0 8 2
A loadLines() 0 19 3
A getGroup() 0 6 1
A getLines() 0 4 1
A getLangFiles() 0 6 1
A getLocaleDirPath() 0 4 1
1
<?php
2
3
namespace MikeZange\LaravelDatabaseTranslation\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Translation\FileLoader;
8
use MikeZange\LaravelDatabaseTranslation\Repositories\TranslationRepository;
9
10
/**
11
 * Class LoadTranslationsFromFiles.
12
 */
13
class LoadTranslationsFromFiles extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'trans-db:load {locale}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Loads the translations from file into the DB for a given locale';
28
29
    /**
30
     * The Laravel File Loader.
31
     *
32
     * @var FileLoader
33
     */
34
    protected $laravelLoader;
35
36
    /**
37
     * Translation repository.
38
     *
39
     * @var TranslationRepository
40
     */
41
    protected $translationRepository;
42
43
    /**
44
     * Laravel Filesystem.
45
     *
46
     * @var Filesystem
47
     */
48
    protected $filesystem;
49
50
    /**
51
     * The locale.
52
     *
53
     * @var string
54
     */
55
    protected $locale;
56
57
    /**
58
     * Create a new command instance.
59
     *
60
     * @param Filesystem            $filesystem
61
     * @param TranslationRepository $translationRepository
62
     */
63
    public function __construct(Filesystem $filesystem, TranslationRepository $translationRepository)
64
    {
65
        parent::__construct();
66
67
        //this is so we can benefit from namespaces that are already loaded
68
        $this->laravelLoader = app()['translator']->getLoader()->getFileLoader();
69
        $this->translationRepository = $translationRepository;
70
        $this->filesystem = $filesystem;
71
    }
72
73
    /**
74
     * Execute the console command.
75
     *
76
     * @return mixed
77
     */
78
    public function handle()
79
    {
80
        $this->locale = $this->argument('locale');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->argument('locale') can also be of type array. However, the property $locale is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
82
        $this->loadGroupLines();
83
84
        $this->loadNameSpacedLines();
85
86
        return $this->info('Complete');
87
    }
88
89
    protected function loadGroupLines()
90
    {
91
        $langFiles = $this->getLangFiles(resource_path().'/lang');
92
93
        $this->processFiles($langFiles);
94
    }
95
96
    protected function loadNameSpacedLines()
97
    {
98
        foreach ($this->laravelLoader->namespaces() as $namespace => $path) {
99
            $langFiles = $this->getLangFiles($path);
100
            $this->processFiles($langFiles, $namespace);
101
        }
102
    }
103
104
    /**
105
     * @param $files
106
     * @param null|string $namespace
107
     */
108
    protected function processFiles($files, $namespace = null)
109
    {
110
        foreach ($files as $file) {
111
            $group = $this->getGroup($file);
112
            $lines = $this->getLines($file);
113
            $this->loadLines($lines, $namespace, $group, $this->locale);
114
        }
115
    }
116
117
    /**
118
     * Load the lines into the database.
119
     *
120
     * @param $lines
121
     * @param $namespace
122
     * @param $group
123
     * @param $locale
124
     */
125
    protected function loadLines($lines, $namespace, $group, $locale)
126
    {
127
        foreach ($lines as $key => $value) {
128
            $line = $this->translationRepository->getItem($namespace, $group, $key);
129
            if ($line) {
130
                $this->translationRepository->updateTranslation($line, $this->locale, $value, false);
0 ignored issues
show
Compatibility introduced by
$line of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<MikeZange\Laravel...ion\Models\Translation>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
131
            } else {
132
                $attributes = [
133
                    'namespace' => $namespace,
134
                    'group'     => $group,
135
                    'key'       => $key,
136
                    'values'    => [
137
                        "{$locale}" => $value,
138
                    ],
139
                ];
140
                $this->translationRepository->create($attributes);
141
            }
142
        }
143
    }
144
145
    /**
146
     * @param $file
147
     *
148
     * @return mixed
149
     */
150
    protected function getGroup($file)
151
    {
152
        preg_match('/'.$this->locale.'\/([^\[]+).php/i', $file, $group);
153
154
        return $group[1];
155
    }
156
157
    /**
158
     * @param $file
159
     *
160
     * @return array
161
     */
162
    protected function getLines($file)
163
    {
164
        return array_dot($this->filesystem->getRequire($file));
165
    }
166
167
    /**
168
     * @param $path
169
     *
170
     * @return array
171
     */
172
    protected function getLangFiles($path)
173
    {
174
        $dir = $this->getLocaleDirPath($path);
175
176
        return $this->filesystem->files($dir);
177
    }
178
179
    /**
180
     * @param $path
181
     *
182
     * @return string
183
     */
184
    protected function getLocaleDirPath($path)
185
    {
186
        return $path.'/'.$this->locale;
187
    }
188
}
189