Completed
Push — master ( d289a1...5c6899 )
by
unknown
07:56
created

Translatable::initializeTranslatable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BBS\Nova\Translation\Models\Traits;
4
5
use BBS\Nova\Translation\Models\Scopes\TranslatableScope;
6
use BBS\Nova\Translation\Models\Translation;
7
8
/**
9
 * @property array $nonTranslatable
10
 * @property \Illuminate\Database\Eloquent\Collection $translations
11
 * @mixin \Illuminate\Database\Eloquent\Model
12
 */
13
trait Translatable
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public static function bootTranslatable()
19
    {
20
        static::addGlobalScope(new TranslatableScope);
21
22
        static::deleted(function ($model) {
23
            Translation::query()
24
                ->where('translatable_id', '=', $model->getKey())
25
                ->where('translatable_type', '=', get_class($model))
26
                ->delete();
27
        });
28
    }
29
30
    /**
31
     * Initialize a translatable model.
32
     *
33
     * @return void
34
     */
35
    public function initializeTranslatable()
36
    {
37
        $translationIdField = $this->translationIdField();
38
39
        $this->fillable[] = $translationIdField;
0 ignored issues
show
Bug introduced by
The property fillable 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
        $this->nonTranslatable[] = $translationIdField;
41
        $this->casts[$translationIdField] = 'integer';
0 ignored issues
show
Bug introduced by
The property casts 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...
42
    }
43
44
    /**
45
     * Return next fresh translation ID.
46
     *
47
     * @return int
48
     */
49
    public static function freshTranslationId()
50
    {
51
        $instance = new static;
52
        $translationIdField = $instance->translationIdField();
53
54
        /** @var \Illuminate\Database\Eloquent\Model $lastTranslation */
55
        $lastTranslation = static::newQueryWithoutScope(TranslatableScope::class)
56
            ->select($instance->getTable().'.'.$translationIdField)
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...
57
            ->orderBy($translationIdField, 'desc')
58
            ->first();
59
60
        return ! empty($lastTranslation) ? ($lastTranslation->getAttribute($translationIdField) + 1) : 1;
61
    }
62
63
    /**
64
     * Translations relationship.
65
     *
66
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
67
     */
68
    public function translations(): MorphMany
69
    {
70
        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...
71
    }
72
73
    /**
74
     * Get the list of non translatable fields.
75
     *
76
     * @return array
77
     */
78
    public function getNonTranslatable()
79
    {
80
        return $this->nonTranslatable;
81
    }
82
83
    /**
84
     * Return translation ID value.
85
     *
86
     * @return int
87
     */
88
    public function translationId()
89
    {
90
        return $this->getAttribute($this->translationIdField());
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
91
    }
92
93
    /**
94
     * Define translation ID field name.
95
     *
96
     * @return string
97
     */
98
    public function translationIdField()
99
    {
100
        return 'translation_id';
101
    }
102
103
    /**
104
     * Create and return a translation entry for given locale ID.
105
     *
106
     * @param  int  $localeId
107
     * @return \BBS\Nova\Translation\Models\Translation
108
     */
109
    public function createTranslationEntry(int $localeId)
110
    {
111
        Translation::query()->create([
112
            'locale_id' => $localeId,
113
            'translation_id' => $this->translationId(),
114
            '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...
115
            'translatable_type' => get_class($this),
116
        ]);
117
    }
118
}
119