Completed
Push — master ( 27165a...0e6761 )
by
unknown
09:36
created

Translatable::bootTranslatable()   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 BBS\Nova\Translation\Models\Traits;
4
5
use BBS\Nova\Translation\Models\Scopes\TranslatableScope;
6
7
trait Translatable
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    public static function bootTranslatable()
13
    {
14
        static::addGlobalScope(new TranslatableScope);
15
    }
16
17
    /**
18
     * Return next fresh translation ID.
19
     *
20
     * @return int
21
     */
22
    public static function freshTranslationId()
23
    {
24
        $instance = new static;
25
        $translationIdField = $instance->translationIdField();
26
27
        $lastTranslation = static::query()
28
            ->withoutGlobalScope(TranslatableScope::class)
29
            ->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...
30
            ->orderBy($translationIdField, 'desc')
31
            ->first();
32
33
        return ! empty($lastTranslation) ? ($lastTranslation->getAttribute($translationIdField) + 1) : 1;
34
    }
35
36
    /**
37
     * Return translation ID value.
38
     *
39
     * @return int
40
     */
41
    public function translationId()
42
    {
43
        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...
44
    }
45
46
    /**
47
     * Define translation ID field name.
48
     *
49
     * @return string
50
     */
51
    public function translationIdField()
52
    {
53
        return 'translation_id';
54
    }
55
}
56