Passed
Pull Request — master (#28)
by Mike
02:21
created

CacheKeyable::getOrderByClauses()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2
3
use Illuminate\Support\Collection;
4
5
trait CacheKeyable
6
{
7
    protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
8
    {
9
        $key = $this->getModelSlug();
10
        $key .= $this->getIdColumn($idColumn ?: '');
11
        $key .= $this->getQueryColumns($columns);
12
        $key .= $this->getWhereClauses();
13
        $key .= $this->getWithModels();
14
        $key .= $this->getOrderByClauses();
15
        $key .= $this->getOffsetClause();
16
        $key .= $this->getLimitClause();
17
18
        return $key;
19
    }
20
21
    protected function getIdColumn(string $idColumn) : string
22
    {
23
        return $idColumn ? "_{$idColumn}" : '';
24
    }
25
26
    protected function getLimitClause() : string
27
    {
28
        if (! $this->query->limit) {
1 ignored issue
show
Bug Best Practice introduced by
The property query does not exist on GeneaLabs\LaravelModelCaching\Traits\CacheKeyable. Did you maybe forget to declare it?
Loading history...
29
            return '';
30
        }
31
32
        return "-limit_{$this->query->limit}";
33
    }
34
35
    protected function getModelSlug() : string
36
    {
37
        return str_slug(get_class($this->model));
1 ignored issue
show
Bug Best Practice introduced by
The property model does not exist on GeneaLabs\LaravelModelCaching\Traits\CacheKeyable. Did you maybe forget to declare it?
Loading history...
38
    }
39
40
    protected function getOffsetClause() : string
41
    {
42
        if (! $this->query->offset) {
1 ignored issue
show
Bug Best Practice introduced by
The property query does not exist on GeneaLabs\LaravelModelCaching\Traits\CacheKeyable. Did you maybe forget to declare it?
Loading history...
43
            return '';
44
        }
45
46
        return "-offset_{$this->query->offset}";
47
    }
48
49
    protected function getOrderByClauses() : string
50
    {
51
        $orders = collect($this->query->orders);
1 ignored issue
show
Bug Best Practice introduced by
The property query does not exist on GeneaLabs\LaravelModelCaching\Traits\CacheKeyable. Did you maybe forget to declare it?
Loading history...
52
53
        return $orders->reduce(function($carry, $order){
54
            return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
55
        })
56
        ?: '';
57
    }
58
59
    protected function getQueryColumns(array $columns) : string
60
    {
61
        if ($columns === ['*'] || $columns === []) {
62
            return '';
63
        }
64
65
        return '_' . implode('_', $columns);
66
    }
67
68
    protected function getTypeClause($where) : string
69
    {
70
        return in_array($where['type'], ['In', 'Null', 'NotNull'])
71
            ? strtolower($where['type'])
72
            : '';
73
    }
74
75
    protected function getValuesClause(array $where = null) : string
76
    {
77
        return is_array(array_get($where, 'values'))
78
            ? '_' . implode('_', $where['values'])
79
            : '';
80
    }
81
82
    protected function getWhereClauses(array $wheres = []) : string
83
    {
84
        return $this->getWheres($wheres)
85
            ->reduce(function ($carry, $where) {
86
                if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
87
                    return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
88
                }
89
90
                if ($where['type'] === 'Column') {
91
                    return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
92
                }
93
94
                if ($where['type'] === 'raw') {
95
                    return "_{$where['boolean']}_" . str_slug($where['sql']);
96
                }
97
98
                $value = array_get($where, 'value');
99
                $value .= $this->getTypeClause($where);
100
                $value .= $this->getValuesClause($where);
101
102
                return "{$carry}-{$where['column']}_{$value}";
103
            })
104
            . '';
105
    }
106
107
    protected function getWheres(array $wheres) : Collection
108
    {
109
        $wheres = collect($wheres);
110
111
        if ($wheres->isEmpty()) {
112
            $wheres = collect($this->query->wheres);
1 ignored issue
show
Bug Best Practice introduced by
The property query does not exist on GeneaLabs\LaravelModelCaching\Traits\CacheKeyable. Did you maybe forget to declare it?
Loading history...
113
        }
114
115
        return $wheres;
116
    }
117
118
    protected function getWithModels() : string
119
    {
120
        $eagerLoads = collect($this->eagerLoad);
1 ignored issue
show
Bug Best Practice introduced by
The property eagerLoad does not exist on GeneaLabs\LaravelModelCaching\Traits\CacheKeyable. Did you maybe forget to declare it?
Loading history...
121
122
        if ($eagerLoads->isEmpty()) {
123
            return '';
124
        }
125
126
        return '-' . implode('-', $eagerLoads->keys()->toArray());
127
    }
128
}
129