Completed
Push — master ( edb9ee...b465c5 )
by
unknown
10:14
created

Translatable::bootTranslatable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BBSLab\NovaTranslation\Models\Traits;
4
5
use BBSLab\NovaTranslation\Models\Locale;
6
use BBSLab\NovaTranslation\Models\Translation;
7
use Exception;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * @property array $non_translatable
12
 * @property \Illuminate\Database\Eloquent\Collection $translations
13
 */
14
trait Translatable
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public static function bootTranslatable()
20
    {
21
        static::deleted(function ($model) {
22
            Translation::query()
23
                ->where('translatable_id', '=', $model->getKey())
24
                ->where('translatable_type', '=', get_class($model))
25
                ->delete();
26
        });
27
    }
28
29
    /**
30
     * Initialize a translatable model.
31
     *
32
     * @return void
33
     */
34
    public function initializeTranslatable()
35
    {
36
        $this->nonTranslatable = [];
0 ignored issues
show
Bug introduced by
The property nonTranslatable does not seem to exist. Did you mean non_translatable?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
37
    }
38
39
    /**
40
     * Get the list of non translatable fields.
41
     *
42
     * @return array
43
     */
44
    public function getNonTranslatable()
45
    {
46
        return $this->nonTranslatable;
0 ignored issues
show
Bug introduced by
The property nonTranslatable does not seem to exist. Did you mean non_translatable?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
    }
48
49
    /**
50
     * Translations relationship.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
53
     */
54
    public function translations(): MorphMany
55
    {
56
        return $this->morphMany(Translation::class, 'translatable')->withPivot(['locale_id', 'translation_id']);
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
    }
58
59
    /**
60
     * Create and return a translation entry for given locale ID.
61
     *
62
     * @param  int  $localeId
63
     * @return \BBS\Nova\Translation\Models\Translation
64
     */
65
    public function createTranslationEntry(int $localeId, int $translationId = 0)
66
    {
67
        Translation::query()->create([
68
            'locale_id' => $localeId,
69
            'translation_id' => ! empty($translationId) ? $translationId : static::freshTranslationId(),
70
            'translatable_id' => $this->getKey(),
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71
            'translatable_type' => get_class($this),
72
        ]);
73
    }
74
75
    /**
76
     * Return next fresh translation ID.
77
     *
78
     * @return int
79
     */
80
    public static function freshTranslationId()
81
    {
82
        /** @var \BBSLab\NovaTranslation\Models\Translation $lastTranslation */
83
        $lastTranslation = Translation::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
84
            ->select('translation_id')
85
            ->orderBy('translation_id', 'desc')
86
            ->first();
87
88
        return ! empty($lastTranslation) ? ($lastTranslation->translation_id + 1) : 1;
89
    }
90
91
    /**
92
     * Scope a query to only retrieve items from given locale.
93
     *
94
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
95
     * @param  string  $iso
96
     * @return \Illuminate\Database\Eloquent\Builder
97
     * @throws \Exception
98
     */
99
    public function scopeInLocale(Builder $builder, string $iso = '')
100
    {
101
        $iso = ! empty($iso) ? $iso : app()->getLocale();
102
103
        /** @var \BBSLab\NovaTranslation\Models\Locale $locale */
104
        $locale = Locale::query()->select('id')->where('iso', '=', $iso)->first();
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
        if (empty($locale)) {
106
            throw new Exception('Invalid locale provided in inLocale() scope "'.$iso.'"');
107
        }
108
109
        return $builder->join('translations', function ($join) use ($locale) {
110
            $model = new static;
111
112
            $join
113
                ->on($model->getTable().'.'.$model->getKeyName(), '=', 'translations.translatable_id')
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
114
                ->where('translations.translatable_type', '=', get_class($model))
115
                ->where('translations.locale_id', '=', $locale->id);
116
        });
117
    }
118
}
119