HasTranslations::bootHasTranslations()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Fomvasss\LaravelTranslatable\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\HasMany;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\Collection;
8
9
trait HasTranslations
10
{
11
    use HasLanguage;
12
13
    protected static function bootHasTranslations()
14
    {
15
        static::saved(function ($model) {
16
            if ($model->{$model->getLangcodeColumn()} && empty($model->{$model->getTranslationUuidColumn()})) {
17
                $model->{$model->getTranslationUuidColumn()} = Str::uuid();
18
                $model->save();
19
            }
20
        });
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function getTranslationUuidColumn(): string
27
    {
28
        return config('translatable.db.columns.translation_uuid');
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

28
        return /** @scrutinizer ignore-call */ config('translatable.db.columns.translation_uuid');
Loading history...
29
    }
30
31
    /**
32
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
33
     */
34
    public function translations(): HasMany
35
    {
36
        return $this->hasMany(static::class, $this->getTranslationUuidColumn(), $this->getTranslationUuidColumn());
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        return $this->/** @scrutinizer ignore-call */ hasMany(static::class, $this->getTranslationUuidColumn(), $this->getTranslationUuidColumn());
Loading history...
37
    }
38
39
    public function saveTranslatable(string $langcode, ?string $uuid = null)
40
    {
41
        $this->setAttribute($this->getLangcodeColumn(), $langcode);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

41
        $this->/** @scrutinizer ignore-call */ 
42
               setAttribute($this->getLangcodeColumn(), $langcode);
Loading history...
42
        if ($uuid) {
43
            $this->setAttribute($this->getTranslationUuidColumn(), $uuid);
44
        }
45
46
        return $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
        return $this->/** @scrutinizer ignore-call */ save();
Loading history...
47
    }
48
49
    /**
50
     * @return Collection
51
     */
52
    public function getTranslationList(): Collection
53
    {
54
        $languages = config('translatable.languages', []);
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

54
        $languages = /** @scrutinizer ignore-call */ config('translatable.languages', []);
Loading history...
55
        $translations = $this->translations;
56
        $result = new Collection();
57
58
        foreach ($languages as $langcode => $language) {
59
            $model = $translations->where($this->getLangcodeColumn(), $langcode)->first();
60
            $result->push([
61
                'model_key' => optional($model)->{$this->getKeyName()},
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

61
                'model_key' => optional($model)->{$this->/** @scrutinizer ignore-call */ getKeyName()},
Loading history...
62
                'model_type' => Str::snake(class_basename(static::class)),
63
                'translation_uuid' => optional($model)->{$this->getTranslationUuidColumn()},
64
                'langcode' => $langcode,
65
                'language' => $language,
66
                'status' => $this->getTranslationStatus($langcode, $model)
67
            ]);
68
        }
69
70
        return $result;
71
    }
72
73
    /**
74
     * @param string $langcode
75
     * @param null $model
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $model is correct as it would always require null to be passed?
Loading history...
76
     * @return string
77
     */
78
    protected function getTranslationStatus(string $langcode, $model = null): string
79
    {
80
        if (empty($model)) {
81
            return 'empty';
82
        }
83
84
        if ($this->{$this->getLangcodeColumn()} === $langcode) {
85
            return 'current';
86
        }
87
88
        return 'translation';
89
    }
90
}