Completed
Push — master ( 5dab40...ca534d )
by Maxime
120:18 queued 114:35
created

Translatable   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 17.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 2
dl 23
loc 133
ccs 0
cts 69
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A bootTranslatable() 0 4 1
A withoutTranslation() 0 7 1
A getQualifiedIsoColumn() 0 6 1
A getQualifiedIdElementColumn() 0 6 1
A getQualifiedModelColumn() 0 6 1
A getQualifiedIdSourceColumn() 0 6 1
A getQualifiedTable() 0 6 1
A setTranslation() 0 20 2
A getIso() 7 7 2
A hasBeenTranslated() 9 9 2
A hasTranslation() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Distilleries\Expendable\Scopes;
2
3
use Distilleries\Expendable\Models\Translation;
4
5
trait Translatable {
6
7
    /**
8
     * Boot the soft deleting trait for a model.
9
     *
10
     * @return void
11
     */
12
    public static function bootTranslatable()
13
    {
14
        static::addGlobalScope(new TranslatableScope);
15
    }
16
17
18
    /**
19
     * Get a new query builder that only includes soft deletes.
20
     *
21
     * @return \Illuminate\Database\Eloquent\Builder|static
22
     */
23
    public static function withoutTranslation()
24
    {
25
        $instance = new static;
26
27
        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?

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...
28
29
    }
30
31
    /**
32
     * Get the fully qualified "iso" column.
33
     *
34
     * @return string
35
     */
36
    public function getQualifiedIsoColumn()
37
    {
38
        $translation = new Translation;
39
40
        return $translation->getTable() . '.' . $translation->getIsoColumn();
41
    }
42
43
    /**
44
     * Get the fully qualified "id_element" column.
45
     *
46
     * @return string
47
     */
48
    public function getQualifiedIdElementColumn()
49
    {
50
        $translation = new Translation;
51
52
        return $translation->getTable() . '.' . $translation->getIdElementColumn();
53
    }
54
55
    /**
56
     * Get the fully qualified "id_element" column.
57
     *
58
     * @return string
59
     */
60
    public function getQualifiedModelColumn()
61
    {
62
        $translation = new Translation;
63
64
        return $translation->getTable() . '.' . $translation->getModelColumn();
65
    }
66
67
    /**
68
     * Get the fully qualified "id_source" column.
69
     *
70
     * @return string
71
     */
72
    public function getQualifiedIdSourceColumn()
73
    {
74
        $translation = new Translation;
75
76
        return $translation->getTable() . '.' . $translation->getIdSourceColumn();
77
    }
78
79
    /**
80
     * Get the fully qualified "table" column.
81
     *
82
     * @return string
83
     */
84
    public function getQualifiedTable()
85
    {
86
        $translation = new Translation;
87
88
        return $translation->getTable();
89
    }
90
91
    public function setTranslation($id_element, $model, $id_source, $iso)
92
    {
93
94
        $translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element)
95
            ->where($this->getQualifiedModelColumn(), '=', $model)
96
            ->where($this->getQualifiedIsoColumn(), '=', $iso)->get();
97
98
        if (!$translation->isEmpty()) {
99
            $translation->delete();
100
        }
101
102
103
        $translation             = new Translation;
104
        $translation->id_element = $id_element;
105
        $translation->model      = $model;
106
        $translation->id_source  = $id_source;
107
        $translation->iso        = $iso;
108
109
        return $translation->save();
110
    }
111
112 View Code Duplication
    public function getIso($model, $id_element)
113
    {
114
        $translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element)
115
            ->where($this->getQualifiedModelColumn(), '=', $model)->get()->last();
116
117
        return (!empty($translation->iso)) ? $translation->iso : false;
118
    }
119
120 View Code Duplication
    public function hasBeenTranslated($model, $id_source, $iso)
121
    {
122
        $translation = Translation::where($this->getQualifiedIdSourceColumn(), '=', $id_source)
123
            ->where($this->getQualifiedModelColumn(), '=', $model)
124
            ->where($this->getQualifiedIsoColumn(), '=', $iso)
125
            ->get()->last();
126
127
        return (!empty($translation->id_element)) ? $translation->id_element : false;
128
    }
129
130 View Code Duplication
    public function hasTranslation($model, $id_element)
131
    {
132
        $translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element)
133
            ->where($this->getQualifiedModelColumn(), '=', $model)->count();
134
135
        return (!empty($translation)) ? true : false;
136
    }
137
}