Completed
Push — master ( b9cb31...17909b )
by
unknown
14s queued 11s
created

Translatable::deletingTranslation()   A

Complexity

Conditions 1
Paths 1

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 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\Observers\TranslatableObserver;
7
use BBSLab\NovaTranslation\Models\Translation;
8
use BBSLab\NovaTranslation\Models\TranslationRelation;
9
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
10
use Illuminate\Database\Eloquent\Relations\MorphOne;
11
use Illuminate\Database\Query\JoinClause;
12
13
/**
14
 * @property \BBSLab\NovaTranslation\Models\Translation $translation
15
 * @property \Illuminate\Database\Eloquent\Collection|\BBSLab\NovaTranslation\Models\Translation[] $translations
16
 * @method static \Illuminate\Database\Eloquent\Builder locale(?string $iso = null)
17
 */
18
trait Translatable
19
{
20
    protected $_deleting_translation = false;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public static function bootTranslatable()
26
    {
27
        static::observe(TranslatableObserver::class);
28
    }
29
30
    /**
31
     * Get the list of non translatable fields.
32
     *
33
     * @return array
34
     */
35
    public function getNonTranslatable(): array
36
    {
37
        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...
38
    }
39
40
    /**
41
     * Get the list of fields to duplicate on create.
42
     * (Other fields MUST BE nullable in database).
43
     *
44
     * @return array
45
     */
46
    public function getOnCreateTranslatable(): array
47
    {
48
        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...
49
    }
50
51
    /**
52
     * Translation relationship.
53
     *
54
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
55
     */
56
    public function translation(): MorphOne
57
    {
58
        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...
59
    }
60
61
    /**
62
     * Return current item translations.
63
     *
64
     * @return \BBSLab\NovaTranslation\Models\TranslationRelation
65
     */
66
    public function translations(): TranslationRelation
67
    {
68
        return new TranslationRelation($this);
69
    }
70
71
    /**
72
     * Create and return a translation entry for given locale ID.
73
     *
74
     * @param  int  $localeId
75
     * @param  int  $translationId
76
     * @return \BBSLab\NovaTranslation\Models\Translation
77
     */
78
    public function upsertTranslationEntry(int $localeId, int $translationId = 0): Translation
79
    {
80
        /** @var \BBSLab\NovaTranslation\Models\Translation $translation */
81
        $translation = Translation::query()
82
            ->firstOrCreate([
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' => static::class,
87
            ]);
88
89
        return $translation;
90
    }
91
92
    /**
93
     * Return next fresh translation ID.
94
     *
95
     * @return int
96
     */
97
    public function freshTranslationId(): int
98
    {
99
        $lastTranslation = Translation::query()
100
            ->where('translatable_type', '=', static::class)
101
            ->max('translation_id') ?? 0;
102
103
        return $lastTranslation + 1;
104
    }
105
106
    /**
107
     * Scope a query to only retrieve items from given locale.
108
     *
109
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
110
     * @param  string  $iso
111
     * @return \Illuminate\Database\Eloquent\Builder
112
     */
113
    public function scopeLocale(EloquentBuilder $builder, string $iso = null)
114
    {
115
        return $builder->join('translations', function (JoinClause $join) {
116
            $join->on($this->getQualifiedKeyName(), '=', 'translations.translatable_id')
0 ignored issues
show
Bug introduced by
It seems like getQualifiedKeyName() 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...
117
                ->where('translations.translatable_type', '=', static::class);
118
        })
119
            ->join('locales', function (JoinClause $join) use ($iso) {
120
                $join->on('locales.id', '=', 'translations.locale_id')
121
                    ->where('locales.iso', '=', $iso ?? app()->getLocale());
122
            })
123
            ->select($this->qualifyColumn('*'));
0 ignored issues
show
Bug introduced by
It seems like qualifyColumn() 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...
124
    }
125
126
    /**
127
     * Translate model to given locale and return translated model.
128
     *
129
     * @param  \BBSLab\NovaTranslation\Models\Locale  $locale
130
     * @return \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable
131
     */
132
    public function translate(Locale $locale)
133
    {
134
        /** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translated */
135
        $translated = optional(
136
            $this->translations()
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<BBSLab\NovaTransl...ls\TranslationRelation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
137
                ->where('locale_id', '=', $locale->getKey())
138
                ->with('translatable')
139
                ->first()
140
        )->translatable;
141
142
        return $translated ?? static::withoutEvents(function () use ($locale) {
143
            /** @var self $translated */
144
            $translated = $this->newQuery()->create(
0 ignored issues
show
Bug introduced by
It seems like newQuery() 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...
145
                $this->only(
0 ignored issues
show
Bug introduced by
It seems like only() 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...
146
                    $this->getOnCreateTranslatable()
147
                )
148
            );
149
            $translated->upsertTranslationEntry($locale->getKey(), $this->translation->translation_id);
150
151
            return $translated;
152
        });
153
    }
154
155
    /**
156
     * Set deleting translation state.
157
     *
158
     * @return void
159
     */
160
    public function deletingTranslation(): void
161
    {
162
        $this->_deleting_translation = true;
163
    }
164
165
    /**
166
     * Determine is the model currently in a delete translation process.
167
     *
168
     * @return bool
169
     */
170
    public function isDeletingTranslation(): bool
171
    {
172
        return $this->_deleting_translation;
173
    }
174
}
175