GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

getAvailableLanguagesForModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace WeAreNeopix\LaravelModelTranslation\Drivers;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use PHPUnit\Framework\Assert as PHPUnit;
8
use WeAreNeopix\LaravelModelTranslation\Contracts\TranslationDriver;
9
10
class ArrayTranslationDriver implements TranslationDriver
11
{
12
    /** @var array */
13
    private $translations = [];
14
15
    public function storeTranslationsForModel(Model $model, string $language, array $translations): bool
16
    {
17
        $this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()][$language] = $translations;
18
19
        return true;
20
    }
21
22
    public function getTranslationsForModel(Model $model, string $language): array
23
    {
24
        $modelIdentifier = $model->getModelIdentifier();
25
        $instanceIdentifier = $model->getInstanceIdentifier();
26
27
        if (isset($this->translations[$modelIdentifier][$instanceIdentifier][$language])) {
28
            return $this->translations[$modelIdentifier][$instanceIdentifier][$language];
29
        }
30
31
        return [];
32
    }
33
34
    public function getTranslationsForModels(Collection $models, string $language): Collection
35
    {
36
        return $models->mapWithKeys(function (Model $model) use ($language) {
37
            return [
38
                $model->getInstanceIdentifier() => $this->getTranslationsForModel($model, $language),
39
            ];
40
        });
41
    }
42
43
    public function getAvailableLanguagesForModel(Model $model): array
44
    {
45
        if (isset($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()])) {
46
            return array_keys($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()]);
47
        }
48
49
        return [];
50
    }
51
52
    public function getModelsAvailableInLanguage(string $modelIdentifier, string $language): array
53
    {
54
        $instances = $this->translations[$modelIdentifier] ?? [];
55
56
        if (empty($instances)) {
57
            return $instances;
58
        }
59
60
        $modelsWithLanguage = array_filter($instances, function ($modelLanguages) use ($language) {
61
            return array_key_exists($language, $modelLanguages);
62
        });
63
64
        return array_keys($modelsWithLanguage);
65
    }
66
67
    public function putTranslationsForModel(Model $model, string $language, array $translations): bool
68
    {
69
        $modelIdentifier = $model->getModelIdentifier();
70
        $instanceIdentifier = $model->getInstanceIdentifier();
71
72
        $this->translations[$modelIdentifier][$instanceIdentifier][$language] = $translations;
73
74
        return true;
75
    }
76
77
    public function patchTranslationsForModel(Model $model, string $language, array $translations): bool
78
    {
79
        $modelIdentifier = $model->getModelIdentifier();
80
        $instanceIdentifier = $model->getInstanceIdentifier();
81
82
        foreach ($translations as $attribute => $translation) {
83
            $this->translations[$modelIdentifier][$instanceIdentifier][$language][$attribute] = $translation;
84
        }
85
86
        return true;
87
    }
88
89
    public function deleteAllTranslationsForModel(Model $model): bool
90
    {
91
        unset($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()]);
92
93
        return true;
94
    }
95
96
    public function deleteLanguagesForModel(Model $model, array $languages): bool
97
    {
98
        foreach ($languages as $language) {
99
            unset($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()][$language]);
100
        }
101
102
        return true;
103
    }
104
105
    public function deleteAttributesForModel(Model $model, array $attributes, string $language = null): bool
106
    {
107
        $languages = ($language !== null)
108
            ? [$language]
109
            : array_keys($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()]);
110
111
        foreach ($languages as $language) {
112
            foreach ($attributes as $attribute) {
113
                unset($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()][$language][$attribute]);
114
            }
115
116
            if (empty($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()][$language])) {
117
                unset($this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()][$language]);
118
            }
119
        }
120
121
        return true;
122
    }
123
124
    /**
125
     * Assert that a model has an attribute translation stored in the provided language.
126
     *
127
     * @param \Illuminate\Database\Eloquent\Model $model
128
     * @param string $attribute
129
     * @param string $language
130
     */
131
    public function assertModelHasTranslation(Model $model, string $attribute, string $language)
132
    {
133
        PHPUnit::assertTrue(
134
            $this->translationExists($model, $attribute, $language),
135
            "Provided model's {$attribute} was not translated to {$language}"
136
        );
137
    }
138
139
    /**
140
     * Assert that the model hasn't got a translation for the specified attribute in the specified language.
141
     *
142
     * @param \Illuminate\Database\Eloquent\Model $model
143
     * @param string $attribute
144
     * @param string $language
145
     */
146
    public function assertModelNotHasTranslation(Model $model, string $attribute, string $language)
147
    {
148
        PHPUnit::assertFalse(
149
            $this->translationExists($model, $attribute, $language),
150
            "The provided model has {$attribute} translated to {$language}"
151
        );
152
    }
153
154
    /**
155
     * Assert that the model's attribute's translation in the provided language has the provided value.
156
     *
157
     * @param \Illuminate\Database\Eloquent\Model $model
158
     * @param string $attribute
159
     * @param string $language
160
     * @param string $expected
161
     */
162
    public function assertModelTranslation(Model $model, string $attribute, string $language, string $expected)
163
    {
164
        if (! $this->translationExists($model, $attribute, $language)) {
165
            throw new \InvalidArgumentException('The requested translation does not exist.');
166
        }
167
168
        $translation = $this->translations[$model->getModelIdentifier()]
169
                                          [$model->getInstanceIdentifier()]
170
                                          [$language]
171
                                          [$attribute];
172
173
        PHPUnit::assertEquals(
174
            $expected,
175
            $translation,
176
            "The provided model's {$attribute} was translated as {$translation} instead of the expected {$expected}."
177
        );
178
    }
179
180
    protected function translationExists(Model $model, string $translation, string $language)
181
    {
182
        return isset(
183
            $this->translations[$model->getModelIdentifier()][$model->getInstanceIdentifier()][$language][$translation]
184
        );
185
    }
186
}
187