DatabaseTranslationRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getTranslation() 0 14 3
A getTransformedTranslations() 0 7 1
1
<?php
2
3
namespace SaasReady\Services\TranslationRepositories;
4
5
use Illuminate\Foundation\Application;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SaasReady\Contracts\TranslationRepositoryContract;
7
use SaasReady\Models\Translation;
0 ignored issues
show
Bug introduced by
The type SaasReady\Models\Translation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class DatabaseTranslationRepository implements TranslationRepositoryContract
10
{
11
    protected string $langCode;
12
    protected array $translations = [];
13
14
    public function __construct(Application $laravel)
15
    {
16
        $this->langCode = $laravel->getLocale();
17
18
        if (config('saas-ready.translation.strategy') === 'all') {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

18
        if (/** @scrutinizer ignore-call */ config('saas-ready.translation.strategy') === 'all') {
Loading history...
19
            $this->translations = $this->getTransformedTranslations();
20
        }
21
    }
22
23
    public function getTranslation(string $key): string
24
    {
25
        if (isset($this->translations[$key])) {
26
            return $this->translations[$key];
27
        }
28
29
        if (config('saas-ready.translation.strategy') === 'single') {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

29
        if (/** @scrutinizer ignore-call */ config('saas-ready.translation.strategy') === 'single') {
Loading history...
30
            $translation = Translation::findByKey($key, ['translations']);
31
            $this->translations[$key] = $translation?->translations[$this->langCode] ?? $key;
32
33
            return $this->translations[$key];
34
        }
35
36
        return $key;
37
    }
38
39
    public function getTransformedTranslations(): array
40
    {
41
        return Translation::all(['key', 'translations'])
42
            ->mapWithKeys(fn (Translation $translation) => [
43
                $translation->key => $translation->translations[$this->langCode] ?? $translation->key,
44
            ])
45
            ->toArray();
46
    }
47
}
48