Completed
Push — master ( ce3b69...ca46fd )
by
unknown
17:45 queued 07:49
created

Translatable::initializeTranslatable()   A

Complexity

Conditions 3
Paths 4

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 3
nc 4
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 \BBSLab\NovaTranslation\Models\Translation $translation
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
     * Get the list of non translatable fields.
30
     *
31
     * @return array
32
     */
33
    public function getNonTranslatable()
34
    {
35
        return isset($this->nonTranslatable) ? $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...
36
    }
37
38
    /**
39
     * Get the list of fields to duplicate on create.
40
     * (Other fields MUST BE nullable in database).
41
     *
42
     * @return array
43
     */
44
    public function getOnCreateTranslatable()
45
    {
46
        return isset($this->onCreateTranslatable) ? $this->onCreateTranslatable : $this->getNonTranslatable();
0 ignored issues
show
Bug introduced by
The property onCreateTranslatable 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...
47
    }
48
49
    /**
50
     * Translation relationship.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
53
     */
54
    public function translation()
55
    {
56
        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...
57
    }
58
59
    /**
60
     * Return current item translations.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Collection
63
     */
64
    public function translations()
65
    {
66
        return static::query()
67
            ->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...
68
            ->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...
69
            ->where('translations.translatable_type', '=', get_class($this))
70
            ->where('translations.translation_id', '=', $this->translation->translation_id)
71
            ->get();
72
    }
73
74
    /**
75
     * Create and return a translation entry for given locale ID.
76
     *
77
     * @param  int  $localeId
78
     * @return \BBS\Nova\Translation\Models\Translation
79
     */
80
    public function upsertTranslationEntry(int $localeId, int $translationId = 0)
81
    {
82
        $data = [
83
            'locale_id' => $localeId,
84
            'translation_id' => ! empty($translationId) ? $translationId : $this->freshTranslationId(),
85
            '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...
86
            'translatable_type' => get_class($this),
87
        ];
88
89
        $translation = Translation::query()->where($data)->first();
90
        if (empty($translation)) {
91
            $translation = Translation::query()->create($data);
92
        }
93
94
        return $translation;
95
    }
96
97
    /**
98
     * Return next fresh translation ID.
99
     *
100
     * @return int
101
     */
102
    public function freshTranslationId()
103
    {
104
        /** @var \BBSLab\NovaTranslation\Models\Translation $lastTranslation */
105
        $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...
106
            ->select('translation_id')
107
            ->where('translatable_type', '=', get_class($this))
108
            ->orderBy('translation_id', 'desc')
109
            ->first();
110
111
        return ! empty($lastTranslation) ? ($lastTranslation->translation_id + 1) : 1;
112
    }
113
114
    /**
115
     * Scope a query to only retrieve items from given locale.
116
     *
117
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
118
     * @param  string  $iso
119
     * @return \Illuminate\Database\Eloquent\Builder
120
     * @throws \Exception
121
     */
122
    public function scopeLocale(Builder $builder, string $iso = '')
123
    {
124
        $iso = ! empty($iso) ? $iso : app()->getLocale();
125
126
        /** @var \BBSLab\NovaTranslation\Models\Locale $locale */
127
        $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...
128
        if (empty($locale)) {
129
            throw new Exception('Invalid locale provided in locale() scope "'.$iso.'"');
130
        }
131
132
        return $builder->join('translations', function ($join) use ($locale) {
133
            $model = new static;
134
135
            $join
136
                ->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...
137
                ->where('translations.translatable_type', '=', get_class($model))
138
                ->where('translations.locale_id', '=', $locale->id);
139
        });
140
    }
141
}
142