Completed
Push — master ( ca46fd...1d0b8e )
by
unknown
19:27 queued 09:34
created

Translatable::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BBSLab\NovaTranslation\Models\Traits;
4
5
use BBSLab\NovaTranslation\Models\Contracts\IsTranslatable;
6
use BBSLab\NovaTranslation\Models\Locale;
7
use BBSLab\NovaTranslation\Models\Observers\TranslatableObserver;
8
use BBSLab\NovaTranslation\Models\Translation;
9
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
10
use Illuminate\Database\Eloquent\Collection;
11
use Illuminate\Database\Eloquent\Relations\MorphOne;
12
use Illuminate\Database\Query\JoinClause;
13
14
/**
15
 * @property \BBSLab\NovaTranslation\Models\Translation $translation
16
 * @method static \Illuminate\Database\Eloquent\Builder locale(?string $iso = null)
17
 */
18
trait Translatable
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public static function bootTranslatable()
24
    {
25
        static::observe(TranslatableObserver::class);
26
    }
27
28
    /**
29
     * Get the list of non translatable fields.
30
     *
31
     * @return array
32
     */
33
    public function getNonTranslatable(): array
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(): array
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(): MorphOne
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|self[]
63
     */
64
    public function translations(): Collection
65
    {
66
        return static::query()
67
            ->select($this->qualifyColumn('*'), 'translations.locale_id', 'translations.translation_id')
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...
68
            ->with('translation')
69
            ->join('translations', $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...
70
            ->where('translations.translation_id', '=', optional($this->translation)->translation_id)
71
            ->where('translations.translatable_type', '=', static::class)
72
            ->where($this->getQualifiedKeyName(), '<>', $this->getKey())
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...
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...
73
            ->get();
74
    }
75
76
    /**
77
     * Create and return a translation entry for given locale ID.
78
     *
79
     * @param  int  $localeId
80
     * @param  int  $translationId
81
     * @return \BBSLab\NovaTranslation\Models\Translation
82
     */
83
    public function upsertTranslationEntry(int $localeId, int $translationId = 0): Translation
84
    {
85
        /** @var \BBSLab\NovaTranslation\Models\Translation $translation */
86
        $translation = Translation::query()
87
            ->firstOrCreate([
88
                'locale_id' => $localeId,
89
                'translation_id' => ! empty($translationId) ? $translationId : $this->freshTranslationId(),
90
                '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...
91
                'translatable_type' => static::class,
92
            ]);
93
94
        return $translation;
95
    }
96
97
    /**
98
     * Return next fresh translation ID.
99
     *
100
     * @return int
101
     */
102
    public function freshTranslationId(): int
103
    {
104
        $lastTranslation = Translation::query()
105
            ->where('translatable_type', '=', static::class)
106
            ->max('translation_id') ?? 0;
107
108
        return $lastTranslation + 1;
109
    }
110
111
    /**
112
     * Scope a query to only retrieve items from given locale.
113
     *
114
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
115
     * @param  string  $iso
116
     * @return \Illuminate\Database\Eloquent\Builder
117
     */
118
    public function scopeLocale(EloquentBuilder $builder, string $iso = null)
119
    {
120
        return $builder->join('translations', function (JoinClause $join) {
121
            $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...
122
                ->where('translations.translatable_type', '=', static::class);
123
        })
124
            ->join('locales', function (JoinClause $join) use ($iso) {
125
                $join->on('locales.id', '=', 'translations.locale_id')
126
                    ->where('locales.iso', '=', $iso ?? app()->getLocale());
127
            })
128
            ->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...
129
    }
130
131
    /**
132
     * Translate model to given locale and return translated model.
133
     *
134
     * @param  \BBSLab\NovaTranslation\Models\Locale  $locale
135
     * @return \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable
136
     */
137
    public function translate(Locale $locale)
138
    {
139
        /** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translated */
140
        $translated = $this->translations()->first(function (IsTranslatable $translatable) use ($locale) {
141
            return $translatable->translation->locale_id === $locale->getKey();
0 ignored issues
show
Bug introduced by
Accessing translation on the interface BBSLab\NovaTranslation\M...ontracts\IsTranslatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
142
        });
143
144
        return $translated ?? static::withoutEvents(function () use ($locale) {
145
            /** @var self $translated */
146
            $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...
147
                $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...
148
                    $this->getOnCreateTranslatable()
149
                )
150
            );
151
            $translated->upsertTranslationEntry($locale->getKey(), $this->translation->translation_id);
152
153
            return $translated;
154
        });
155
    }
156
}
157