Passed
Push — master ( b49e12...a5278f )
by Mike
03:13
created

CacheKey::getOffsetClause()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php namespace GeneaLabs\LaravelModelCaching;
2
3
use Illuminate\Database\Eloquent\Model;
4
use Illuminate\Database\Query\Builder;
5
use Illuminate\Support\Collection;
6
7
class CacheKey
8
{
9
    protected $eagerLoad;
10
    protected $model;
11
    protected $query;
12
13
    public function __construct(array $eagerLoad, Model $model, Builder $query)
14
    {
15
        $this->eagerLoad = $eagerLoad;
16
        $this->model = $model;
17
        $this->query = $query;
18
    }
19
20
    public function make(
21
        array $columns = ['*'],
22
        $idColumn = null,
23
        string $keyDifferentiator = ''
24
    ) : string {
25
        $key = $this->getModelSlug();
26
        $key .= $this->getIdColumn($idColumn ?: '');
27
        $key .= $this->getQueryColumns($columns);
28
        $key .= $this->getWhereClauses();
29
        $key .= $this->getWithModels();
30
        $key .= $this->getOrderByClauses();
31
        $key .= $this->getOffsetClause();
32
        $key .= $this->getLimitClause();
33
        $key .= $keyDifferentiator;
34
        $key = sha1($key);
35
36
        return $key;
37
    }
38
39
    protected function getIdColumn(string $idColumn) : string
40
    {
41
        return $idColumn ? "_{$idColumn}" : '';
42
    }
43
44
    protected function getLimitClause() : string
45
    {
46
        if (! $this->query->limit) {
47
            return '';
48
        }
49
50
        return "-limit_{$this->query->limit}";
51
    }
52
53
    protected function getModelSlug() : string
54
    {
55
        return str_slug(get_class($this->model));
56
    }
57
58
    protected function getOffsetClause() : string
59
    {
60
        if (! $this->query->offset) {
61
            return '';
62
        }
63
64
        return "-offset_{$this->query->offset}";
65
    }
66
67
    protected function getOrderByClauses() : string
68
    {
69
        $orders = collect($this->query->orders);
70
71
        return $orders
72
            ->reduce(function ($carry, $order) {
73
                if (($order['type'] ?? '') === 'Raw') {
74
                    return $carry . '_orderByRaw_' . str_slug($order['sql']);
75
                }
76
77
                return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
78
            })
79
            ?: '';
80
    }
81
82
    protected function getQueryColumns(array $columns) : string
83
    {
84
        if ($columns === ['*'] || $columns === []) {
85
            return '';
86
        }
87
88
        return '_' . implode('_', $columns);
89
    }
90
91
    protected function getTypeClause($where) : string
92
    {
93
        $type =in_array($where['type'], ['In', 'Null', 'NotNull', 'between'])
94
            ? strtolower($where['type'])
95
            : strtolower($where['operator']);
96
97
        return str_replace(' ', '_', $type);
98
    }
99
100
    protected function getValuesClause(array $where = null) : string
101
    {
102
        if (in_array($where['type'], ['NotNull'])) {
103
            return '';
104
        }
105
106
        $values = is_array(array_get($where, 'values'))
107
            ? implode('_', $where['values'])
108
            : '';
109
110
        if (! $values && $this->query->bindings['where'] ?? false) {
111
            $values = implode('_', $this->query->bindings['where']);
112
        }
113
114
        return '_' . $values;
115
    }
116
117
    protected function getWhereClauses(array $wheres = []) : string
118
    {
119
        return $this->getWheres($wheres)
120
            ->reduce(function ($carry, $where) {
121
                $value = $this->getNestedClauses($where);
122
                $value .= $this->getColumnClauses($where);
123
                $value .= $this->getRawClauses($where);
124
                $value .= $this->getOtherClauses($where, $carry);
125
126
                return $value;
127
            })
128
            . '';
129
    }
130
131
    protected function getNestedClauses(array $where) : string
132
    {
133
        if (! in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
134
            return '';
135
        }
136
137
        return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
138
    }
139
140
    protected function getColumnClauses(array $where) : string
141
    {
142
        if ($where['type'] !== 'Column') {
143
            return '';
144
        }
145
146
        return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
147
    }
148
149
    protected function getRawClauses(array $where) : string
150
    {
151
        if ($where['type'] !== 'raw') {
152
            return '';
153
        }
154
155
        return "_{$where['boolean']}_" . str_slug($where['sql']);
156
    }
157
158
    protected function getOtherClauses(array $where, string $carry = null) : string
159
    {
160
        if (in_array($where['type'], ['Exists', 'Nested', 'NotExists', 'raw', 'Column'])) {
161
            return '';
162
        }
163
164
        $value = $this->getTypeClause($where);
165
        $value .= $this->getValuesClause($where);
166
167
        return "{$carry}-{$where['column']}_{$value}";
168
    }
169
170
    protected function getWheres(array $wheres) : Collection
171
    {
172
        $wheres = collect($wheres);
173
174
        if ($wheres->isEmpty()) {
175
            $wheres = collect($this->query->wheres);
176
        }
177
178
        return $wheres;
179
    }
180
181
    protected function getWithModels() : string
182
    {
183
        $eagerLoads = collect($this->eagerLoad);
184
185
        if ($eagerLoads->isEmpty()) {
186
            return '';
187
        }
188
189
        return '-' . implode('-', $eagerLoads->keys()->toArray());
190
    }
191
}
192