Completed
Push — master ( d84b0e...706b03 )
by
unknown
05:32
created

Translatable   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 3
dl 0
loc 133
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A bootTranslatable() 0 9 1
A initializeTranslatable() 0 8 2
A getNonTranslatable() 0 4 1
A translation() 0 4 1
A translations() 0 12 1
A upsertTranslationEntry() 0 14 3
A freshTranslationId() 0 11 2
A scopeLocale() 0 19 3
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 \Illuminate\Database\Eloquent\Collection $translations
12
 */
13
trait Translatable
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public static function bootTranslatable()
19
    {
20
        static::deleted(function ($model) {
21
            Translation::query()
22
                ->where('translatable_id', '=', $model->getKey())
23
                ->where('translatable_type', '=', get_class($model))
24
                ->delete();
25
        });
26
    }
27
28
    /**
29
     * Initialize a translatable model.
30
     *
31
     * @return void
32
     */
33
    public function initializeTranslatable()
34
    {
35
        // $this->with[] = 'translation';
36
37
        if (! isset($this->nonTranslatable)) {
38
            $this->nonTranslatable = [];
0 ignored issues
show
Bug introduced by
The property nonTranslatable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
        }
40
    }
41
42
    /**
43
     * Get the list of non translatable fields.
44
     *
45
     * @return array
46
     */
47
    public function getNonTranslatable()
48
    {
49
        return $this->nonTranslatable;
50
    }
51
52
    /**
53
     * Translation relationship.
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
56
     */
57
    public function translation()
58
    {
59
        return $this->morphOne(Translation::class, 'translatable');
0 ignored issues
show
Bug introduced by
It seems like morphOne() 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...
60
    }
61
62
    /**
63
     * Return current item translations.
64
     *
65
     * @return \Illuminate\Database\Eloquent\Collection
66
     */
67
    public function translations()
68
    {
69
        /** @var \BBSLab\NovaTranslation\Models\Translation $currentTranslation */
70
        $currentTranslation = $this->translation;
0 ignored issues
show
Bug introduced by
The property translation does not seem to exist. Did you mean translations?

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...
71
72
        return static::query()
73
            ->select($this->getTable().'.*', 'translations.locale_id', 'translations.translation_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...
74
            ->join('translations', $this->getTable().'.'.$this->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...
75
            ->where('translations.translatable_type', '=', get_class($this))
76
            ->where('translations.translation_id', '=', $currentTranslation->translation_id)
77
            ->get();
78
    }
79
80
    /**
81
     * Create and return a translation entry for given locale ID.
82
     *
83
     * @param  int  $localeId
84
     * @return \BBS\Nova\Translation\Models\Translation
85
     */
86
    public function upsertTranslationEntry(int $localeId, int $translationId = 0)
87
    {
88
        $data = [
89
            'locale_id' => $localeId,
90
            'translation_id' => ! empty($translationId) ? $translationId : static::freshTranslationId(),
91
            '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...
92
            'translatable_type' => get_class($this),
93
        ];
94
95
        $translation = Translation::query()->where($data)->first();
96
        if (empty($translation)) {
97
            Translation::query()->create($data);
98
        }
99
    }
100
101
    /**
102
     * Return next fresh translation ID.
103
     *
104
     * @return int
105
     */
106
    public function freshTranslationId()
107
    {
108
        /** @var \BBSLab\NovaTranslation\Models\Translation $lastTranslation */
109
        $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...
110
            ->select('translation_id')
111
            ->where('translatable_type', '=', get_class($this))
112
            ->orderBy('translation_id', 'desc')
113
            ->first();
114
115
        return ! empty($lastTranslation) ? ($lastTranslation->translation_id + 1) : 1;
116
    }
117
118
    /**
119
     * Scope a query to only retrieve items from given locale.
120
     *
121
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
122
     * @param  string  $iso
123
     * @return \Illuminate\Database\Eloquent\Builder
124
     * @throws \Exception
125
     */
126
    public function scopeLocale(Builder $builder, string $iso = '')
127
    {
128
        $iso = ! empty($iso) ? $iso : app()->getLocale();
129
130
        /** @var \BBSLab\NovaTranslation\Models\Locale $locale */
131
        $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...
132
        if (empty($locale)) {
133
            throw new Exception('Invalid locale provided in locale() scope "'.$iso.'"');
134
        }
135
136
        return $builder->join('translations', function ($join) use ($locale) {
137
            $model = new static;
138
139
            $join
140
                ->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...
141
                ->where('translations.translatable_type', '=', get_class($model))
142
                ->where('translations.locale_id', '=', $locale->id);
143
        });
144
    }
145
}
146