Passed
Push — master ( b30c75...6d865c )
by Michael
02:36
created

CustomOrderTrait::isJoinedToTable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Blasttech\EloquentRelatedPlus;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use InvalidArgumentException;
7
8
/**
9
 * Trait CustomOrderTrait
10
 *
11
 * @property array order_fields
12
 * @property array order_defaults
13
 * @property array order_relations
14
 * @property array order_with
15
 * @property array search_fields
16
 * @property string connection
17
 */
18
trait CustomOrderTrait
19
{
20
    /**
21
     * Check $order_fields and $order_defaults are set
22
     *
23
     * @param string $orderField
24
     * @param string $direction
25
     * @return bool
26
     */
27
    protected function hasOrderFieldsAndDefaults($orderField, $direction)
28
    {
29
        return $this->hasOrderFields() && $this->hasOrderDefaults($orderField, $direction);
30
    }
31
32
    /**
33
     * Check $this->order_fields set correctly
34
     *
35
     * @return bool
36
     */
37
    protected function hasOrderFields()
38
    {
39
        if (!isset($this->order_fields) || !is_array($this->order_fields)) {
40
            throw new InvalidArgumentException(get_class($this) . ' order fields not set correctly.');
41
        } else {
42
            return true;
43
        }
44
    }
45
46
    /**
47
     * Check order defaults set correctly
48
     *
49
     * @param string $orderField
50
     * @param string $direction
51
     * @return bool
52
     */
53
    protected function hasOrderDefaults($orderField, $direction)
54
    {
55
        if (($orderField === '' || $direction === '')
56
            && (!isset($this->order_defaults) || !is_array($this->order_defaults))) {
57
            throw new InvalidArgumentException(get_class($this) . ' order defaults not set and not overriden.');
58
        } else {
59
            return true;
60
        }
61
    }
62
63
    /**
64
     * Override column if provided column not valid
65
     *
66
     * @param string $column
67
     * @return string
68
     */
69
    protected function setOrderColumn($column)
70
    {
71
        // If $column not in order_fields list, use default
72
        if ($column == '' || !isset($this->order_fields[$column])) {
73
            $column = $this->order_defaults['field'];
74
        }
75
76
        return $column;
77
    }
78
79
    /**
80
     * Override direction if provided direction not valid
81
     *
82
     * @param string $direction
83
     * @return string
84
     */
85
    protected function setOrderDirection($direction)
86
    {
87
        // If $direction not asc or desc, use default
88
        if ($direction == '' || !in_array(strtoupper($direction), ['ASC', 'DESC'])) {
89
            $direction = $this->order_defaults['dir'];
90
        }
91
92
        return $direction;
93
    }
94
95
    /**
96
     * Set order based on order_fields
97
     *
98
     * @param Builder $query
99
     * @param string $column
100
     * @param string $direction
101
     * @return Builder
102
     */
103
    protected function setOrder($query, $column, $direction)
104
    {
105
        if (!is_array($this->order_fields[$column])) {
106
            $query->orderByCheckModel($this->order_fields[$column], $direction);
107
        } else {
108
            foreach ($this->order_fields[$column] as $dbField) {
109
                $query->orderByCheckModel($dbField, $direction);
110
            }
111
        }
112
113
        return $query;
114
    }
115
116
    /**
117
     * Join a related table if not already joined
118
     *
119
     * @param Builder $query
120
     * @param string $table
121
     * @return Builder
122
     */
123
    protected function joinRelatedTable($query, $table)
124
    {
125
        if (isset($this->order_relations[$table]) &&
126
            !$this->hasJoin($query, $table, $this->order_relations[$table])) {
127
            $columnRelations = $this->order_relations[$table];
128
129
            $query->modelJoin(
130
                $columnRelations,
131
                '=',
132
                'left',
133
                false,
134
                false
135
            );
136
        }
137
138
        return $query;
139
    }
140
141
    /**
142
     * Check if this model has already been joined to a table or relation
143
     *
144
     * @param Builder $builder
145
     * @param string $table
146
     * @param \Illuminate\Database\Eloquent\Relations\Relation $relation
147
     * @return bool
148
     */
149
    protected function hasJoin(Builder $builder, $table, $relation)
150
    {
151
        if (!$this->isJoinedToTable($builder, $table)) {
152
            return $this->isEagerLoaded($builder, $relation);
0 ignored issues
show
Bug introduced by
It seems like isEagerLoaded() 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...
153
        } else {
154
            return true;
155
        }
156
    }
157
158
    /**
159
     * Check if model is currently joined to $table
160
     *
161
     * @param Builder $builder
162
     * @param string $table
163
     * @return bool
164
     */
165
    protected function isJoinedToTable(Builder $builder, $table)
166
    {
167
        $joins = $builder->getQuery()->joins;
168
        if (!is_null($joins)) {
169
            foreach ($joins as $joinClause) {
170
                if ($joinClause->table == $table) {
171
                    return true;
172
                }
173
            }
174
        }
175
176
        return false;
177
    }
178
}
179