Completed
Push — master ( 054322...ddecdf )
by
unknown
08:45
created

Translatable::getOnCreateTranslatable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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