Translatable   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 16
eloc 41
c 3
b 0
f 1
dl 0
loc 136
ccs 0
cts 73
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getIso() 0 6 2
A hasBeenTranslated() 0 8 2
A getQualifiedIdSourceColumn() 0 5 1
A bootTranslatable() 0 7 2
A withoutTranslation() 0 5 1
A setTranslation() 0 21 2
A getQualifiedIdElementColumn() 0 5 1
A getQualifiedModelColumn() 0 5 1
A getQualifiedTable() 0 5 1
A getQualifiedIsoColumn() 0 5 1
A hasTranslation() 0 6 2
1
<?php
2
3
namespace Distilleries\Expendable\Scopes;
4
5
use Distilleries\Expendable\Contracts\TranslatableObserverContract;
6
use Distilleries\Expendable\Models\Translation;
7
8
trait Translatable
9
{
10
    /**
11
     * Boot the soft deleting trait for a model.
12
     *
13
     * @return void
14
     */
15
    public static function bootTranslatable()
16
    {
17
        static::addGlobalScope(new TranslatableScope);
18
        try {
19
            $observer = app(TranslatableObserverContract::class);
20
            static::observe($observer);
21
        } catch (\Exception $exception) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
22
    }
23
24
    /**
25
     * Get a new query builder that only includes soft deletes.
26
     *
27
     * @return \Illuminate\Database\Eloquent\Builder|static
28
     */
29
    public static function withoutTranslation()
30
    {
31
        $instance = new static;
32
33
        return $instance->withoutGlobalScope(TranslatableScope::class);
0 ignored issues
show
Bug introduced by
It seems like withoutGlobalScope() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        return $instance->/** @scrutinizer ignore-call */ withoutGlobalScope(TranslatableScope::class);
Loading history...
34
35
    }
36
37
    /**
38
     * Get the fully qualified "iso" column.
39
     *
40
     * @return string
41
     */
42
    public function getQualifiedIsoColumn()
43
    {
44
        $translation = new Translation;
45
46
        return $translation->getTable().'.'.$translation->getIsoColumn();
47
    }
48
49
    /**
50
     * Get the fully qualified "id_element" column.
51
     *
52
     * @return string
53
     */
54
    public function getQualifiedIdElementColumn()
55
    {
56
        $translation = new Translation;
57
58
        return $translation->getTable().'.'.$translation->getIdElementColumn();
59
    }
60
61
    /**
62
     * Get the fully qualified "id_element" column.
63
     *
64
     * @return string
65
     */
66
    public function getQualifiedModelColumn()
67
    {
68
        $translation = new Translation;
69
70
        return $translation->getTable().'.'.$translation->getModelColumn();
71
    }
72
73
    /**
74
     * Get the fully qualified "id_source" column.
75
     *
76
     * @return string
77
     */
78
    public function getQualifiedIdSourceColumn()
79
    {
80
        $translation = new Translation;
81
82
        return $translation->getTable().'.'.$translation->getIdSourceColumn();
83
    }
84
85
    /**
86
     * Get the fully qualified "table" column.
87
     *
88
     * @return string
89
     */
90
    public function getQualifiedTable()
91
    {
92
        $translation = new Translation;
93
94
        return $translation->getTable();
95
    }
96
97
    public function setTranslation($id_element, $model, $id_source, $iso)
98
    {
99
100
        $translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element)
101
            ->where($this->getQualifiedModelColumn(), '=', $model)
102
            ->where($this->getQualifiedIsoColumn(), '=', $iso)->get();
103
104
        if (!$translation->isEmpty()) {
105
            $translation->each(function($trans) {
106
                $trans->delete();
107
            });
108
        }
109
110
111
        $translation             = new Translation;
112
        $translation->id_element = $id_element;
0 ignored issues
show
Bug Best Practice introduced by
The property id_element does not exist on Distilleries\Expendable\Models\Translation. Since you implemented __set, consider adding a @property annotation.
Loading history...
113
        $translation->model      = $model;
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on Distilleries\Expendable\Models\Translation. Since you implemented __set, consider adding a @property annotation.
Loading history...
114
        $translation->id_source  = $id_source;
0 ignored issues
show
Bug Best Practice introduced by
The property id_source does not exist on Distilleries\Expendable\Models\Translation. Since you implemented __set, consider adding a @property annotation.
Loading history...
115
        $translation->iso        = $iso;
0 ignored issues
show
Bug Best Practice introduced by
The property iso does not exist on Distilleries\Expendable\Models\Translation. Since you implemented __set, consider adding a @property annotation.
Loading history...
116
117
        return $translation->save();
118
    }
119
120
    public function getIso($model, $id_element)
121
    {
122
        $translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element)
123
            ->where($this->getQualifiedModelColumn(), '=', $model)->get()->last();
124
125
        return (!empty($translation->iso)) ? $translation->iso : false;
0 ignored issues
show
Bug Best Practice introduced by
The property iso does not exist on Distilleries\Expendable\Models\Translation. Since you implemented __get, consider adding a @property annotation.
Loading history...
126
    }
127
128
    public function hasBeenTranslated($model, $id_source, $iso)
129
    {
130
        $translation = Translation::where($this->getQualifiedIdSourceColumn(), '=', $id_source)
131
            ->where($this->getQualifiedModelColumn(), '=', $model)
132
            ->where($this->getQualifiedIsoColumn(), '=', $iso)
133
            ->get()->last();
134
135
        return (!empty($translation->id_element)) ? $translation->id_element : false;
0 ignored issues
show
Bug Best Practice introduced by
The property id_element does not exist on Distilleries\Expendable\Models\Translation. Since you implemented __get, consider adding a @property annotation.
Loading history...
136
    }
137
138
    public function hasTranslation($model, $id_element)
139
    {
140
        $translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element)
141
            ->where($this->getQualifiedModelColumn(), '=', $model)->count();
142
143
        return (!empty($translation)) ? true : false;
144
    }
145
}
146