EloquentBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 81
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 28 3
A pluck() 0 34 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Cacheable;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * @mixin \Illuminate\Database\Query\Builder
12
 */
13
class EloquentBuilder extends Builder
14
{
15
    /**
16
     * Execute the query as a "select" statement.
17
     *
18
     * @param array $columns
19
     *
20
     * @return \Illuminate\Database\Eloquent\Collection|static[]
21
     */
22
    public function get($columns = ['*'])
23
    {
24
        $builder = $this->applyScopes();
25
26
        $closure = function () use ($builder, $columns) {
27
            // If we actually found models we will also eager load any relationships that
28
            // have been specified as needing to be eager loaded, which will solve the
29
            // n+1 query issue for the developers to avoid running a lot of queries.
30
            if (count($models = $builder->getModels($columns)) > 0) {
31
                $models = $builder->eagerLoadRelations($models);
32
            }
33
34
            return $builder->model->newCollection($models);
35
        };
36
37
        // Check if cache is enabled
38
        if ($builder->model->getCacheLifetime()) {
39
            return $builder->model->cacheQuery($builder, $columns, $closure);
40
        }
41
42
        // Cache disabled, just execute query & return result
43
        $results = call_user_func($closure);
44
45
        // We're done, let's clean up!
46
        $builder->model->resetCacheConfig();
47
48
        return $results;
49
    }
50
51
    /**
52
     * Get an array with the values of a given column.
53
     *
54
     * @param string      $column
55
     * @param string|null $key
56
     *
57
     * @return \Illuminate\Support\Collection
58
     */
59
    public function pluck($column, $key = null): Collection
60
    {
61
        $builder = $this->toBase();
62
63
        $closure = function () use ($builder, $column, $key) {
64
            $results = $builder->pluck($column, $key);
65
66
            // If the model has a mutator for the requested column, we will spin through
67
            // the results and mutate the values so that the mutated version of these
68
            // columns are returned as you would expect from these Eloquent models.
69
            if (! $this->model->hasGetMutator($column) &&
70
                ! $this->model->hasCast($column) &&
71
                ! in_array($column, $this->model->getDates())) {
72
                return $results;
73
            }
74
75
            return $results->map(function ($value) use ($column) {
76
                return $this->model->newFromBuilder([$column => $value])->{$column};
77
            });
78
        };
79
80
        // Check if cache is enabled
81
        if ($this->model->getCacheLifetime()) {
82
            return $this->model->cacheQuery($builder, (array) $column, $closure);
83
        }
84
85
        // Cache disabled, just execute query & return result
86
        $results = call_user_func($closure);
87
88
        // We're done, let's clean up!
89
        $this->model->resetCacheConfig();
90
91
        return $results;
92
    }
93
}
94