Passed
Push — master ( 7213a5...680f22 )
by Mike
04:43 queued 02:18
created

CacheKey::getWhereClauses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
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(array $columns = ['*'], $idColumn = null) : string
21
    {
22
        $key = $this->getModelSlug();
23
        $key .= $this->getIdColumn($idColumn ?: '');
24
        $key .= $this->getQueryColumns($columns);
25
        $key .= $this->getWhereClauses();
26
        $key .= $this->getWithModels();
27
        $key .= $this->getOrderByClauses();
28
        $key .= $this->getOffsetClause();
29
        $key .= $this->getLimitClause();
30
31
        return $key;
32
    }
33
34
    protected function getIdColumn(string $idColumn) : string
35
    {
36
        return $idColumn ? "_{$idColumn}" : '';
37
    }
38
39
    protected function getLimitClause() : string
40
    {
41
        if (! $this->query->limit) {
42
            return '';
43
        }
44
45
        return "-limit_{$this->query->limit}";
46
    }
47
48
    protected function getModelSlug() : string
49
    {
50
        return str_slug(get_class($this->model));
51
    }
52
53
    protected function getOffsetClause() : string
54
    {
55
        if (! $this->query->offset) {
56
            return '';
57
        }
58
59
        return "-offset_{$this->query->offset}";
60
    }
61
62
    protected function getOrderByClauses() : string
63
    {
64
        $orders = collect($this->query->orders);
65
66
        return $orders->reduce(function($carry, $order){
67
            return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
68
        })
69
        ?: '';
70
    }
71
72
    protected function getQueryColumns(array $columns) : string
73
    {
74
        if ($columns === ['*'] || $columns === []) {
75
            return '';
76
        }
77
78
        return '_' . implode('_', $columns);
79
    }
80
81
    protected function getTypeClause($where) : string
82
    {
83
        return in_array($where['type'], ['In', 'Null', 'NotNull'])
84
            ? strtolower($where['type'])
85
            : '';
86
    }
87
88
    protected function getValuesClause(array $where = null) : string
89
    {
90
        return is_array(array_get($where, 'values'))
91
            ? '_' . implode('_', $where['values'])
92
            : '';
93
    }
94
95
    protected function getWhereClauses(array $wheres = []) : string
96
    {
97
        return $this->getWheres($wheres)
98
            ->reduce(function ($carry, $where) {
99
                $value = $this->getNestedClauses($where);
100
                $value .= $this->getColumnClauses($where);
101
                $value .= $this->getRawClauses($where);
102
                $value .= $this->getOtherClauses($where, $carry);
103
104
                return $value;
105
            })
106
            . '';
107
    }
108
109
    protected function getNestedClauses(array $where) : string
110
    {
111 View Code Duplication
        if (! in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
112
            return '';
113
        }
114
115
        return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
116
    }
117
118
    protected function getColumnClauses(array $where) : string
119
    {
120
        if ($where['type'] !== 'Column') {
121
            return '';
122
        }
123
124
        return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
125
    }
126
127
    protected function getRawClauses(array $where) : string
128
    {
129
        if ($where['type'] !== 'raw') {
130
            return '';
131
        }
132
133
        return "_{$where['boolean']}_" . str_slug($where['sql']);
134
    }
135
136
    protected function getOtherClauses(array $where, string $carry = null) : string
137
    {
138 View Code Duplication
        if (in_array($where['type'], ['Exists', 'Nested', 'NotExists', 'raw', 'Column'])) {
139
            return '';
140
        }
141
142
        $value = array_get($where, 'value');
143
        $value .= $this->getTypeClause($where);
144
        $value .= $this->getValuesClause($where);
145
146
        return "{$carry}-{$where['column']}_{$value}";
147
    }
148
149
    protected function getWheres(array $wheres) : Collection
150
    {
151
        $wheres = collect($wheres);
152
153
        if ($wheres->isEmpty()) {
154
            $wheres = collect($this->query->wheres);
155
        }
156
157
        return $wheres;
158
    }
159
160
    protected function getWithModels() : string
161
    {
162
        $eagerLoads = collect($this->eagerLoad);
163
164
        if ($eagerLoads->isEmpty()) {
165
            return '';
166
        }
167
168
        return '-' . implode('-', $eagerLoads->keys()->toArray());
169
    }
170
}
171