Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
IsCachedAsCollection
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 7
110
0.00% covered (danger)
0.00%
0 / 1
 getCached
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 removeCached
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCached
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 queryCached
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 filterQuery
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheKeyName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCallClassBaseName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Traits\Models;
4
5use Illuminate\Database\Eloquent\Builder;
6use Illuminate\Database\QueryException;
7use Illuminate\Support\Collection;
8use Illuminate\Support\Facades\Cache;
9
10/**
11 * ! Use this only on limited elements, (not on tags, or products, or posts for ex.)
12 *
13 * @property private static array $cacheConditions Eloquent condition to cache elements (to be used in where statement).
14 */
15trait IsCachedAsCollection
16{
17    /**
18     * Get all available instances from cache.
19     * If cache was reset, it will fetch them from DB.
20     *
21     * @return \Illuminate\Database\Eloquent\Collection
22     */
23    public static function getCached(): Collection
24    {
25        if (!Cache::has(static::getCacheKeyName())) {
26            try {
27                static::setCached();
28            } catch (QueryException $e) {
29                report($e);
30                return \collect();
31            }
32        }
33        return Cache::get(static::getCacheKeyName());
34    }
35
36    /**
37     * Remove cached elements
38     *
39     * @return void
40     */
41    public static function removeCached(): void
42    {
43        Cache::forget(static::getCacheKeyName());
44    }
45
46    /**
47     * Put all instances into cache.
48     *
49     * @return void
50     */
51    protected static function setCached(): void
52    {
53        $conditions = [];
54        if (property_exists(get_called_class(), 'cacheConditions')) {
55            $conditions = collect(static::${'cacheConditions'})->all();
56        }
57        Cache::put(static::getCacheKeyName(), static::queryCached(static::query())
58            ->where($conditions)->get()
59            ->pipe(fn (Collection $collection) => \call_user_func([self::class, 'filterQuery'], $collection)));
60    }
61
62    /**
63     * Change query for cached elements
64     *
65     * @param \Illuminate\Database\Eloquent\Builder $query
66     * @return \Illuminate\Database\Eloquent\Builder
67     */
68    protected static function queryCached(Builder $query): Builder
69    {
70        return $query;
71    }
72
73    /**
74     * Change query for cached elements
75     *
76     * @param \Illuminate\Support\Collection $results
77     * @return \Illuminate\Support\Collection
78     */
79    protected static function filterQuery(Collection $results): Collection
80    {
81        return $results;
82    }
83
84    /**
85     * Get the class basename
86     *
87     * @return string
88     */
89    protected static function getCacheKeyName(): string
90    {
91        return static::getCallClassBaseName();
92    }
93
94    /**
95     * Get the class basename
96     *
97     * @return string
98     */
99    protected static function getCallClassBaseName(): string
100    {
101        return str_replace(__NAMESPACE__ . '\\', '', get_called_class());
102    }
103}