Completed
Push — master ( 54527b...d7d176 )
by Richan
01:11
created

CacheableTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A baseClassName() 0 10 2
A cacheDuration() 0 8 2
A cacheExcept() 0 10 2
A cacheKey() 0 7 1
A cacheTags() 0 8 2
model() 0 1 ?
1
<?php
2
3
namespace Suitmedia\Cacheable\Traits\Repository;
4
5
trait CacheableTrait
6
{
7
    /**
8
     * Get the base class name, without 'Repository' suffix.
9
     *
10
     * @return string
11
     */
12
    protected function baseClassName()
13
    {
14
        $class = class_basename(get_class($this));
15
16
        if (ends_with($class, 'Repository')) {
17
            $class = substr($class, 0, -10);
18
        }
19
20
        return $class;
21
    }
22
23
    /**
24
     * Return the cache duration value
25
     * which would be used by the repository.
26
     *
27
     * @return int
28
     */
29
    public function cacheDuration()
30
    {
31
        if (property_exists($this, 'cacheDuration')) {
32
            return (int) static::$cacheDuration;
33
        }
34
35
        return (int) config('cacheable.duration');
36
    }
37
38
    /**
39
     * Return an array of method names which
40
     * you don't wish to be cached.
41
     *
42
     * @return array
43
     */
44
    public function cacheExcept()
45
    {
46
        $result = (array) config('cacheable.except');
47
48
        if (property_exists($this, 'cacheExcept')) {
49
            $result = array_unique(array_merge($result, (array) static::$cacheExcept));
50
        }
51
52
        return $result;
53
    }
54
55
    /**
56
     * Generate cache key.
57
     *
58
     * @param string $method
59
     * @param mixed  $args
60
     *
61
     * @return string
62
     */
63
    public function cacheKey($method, $args)
64
    {
65
        $class = $this->baseClassName();
66
        $args = sha1(serialize($args));
67
68
        return implode(':', [$class, $method, $args]);
69
    }
70
71
    /**
72
     * Return the cache tags which would
73
     * be used by the repository.
74
     *
75
     * @return mixed
76
     */
77
    public function cacheTags()
78
    {
79
        if (property_exists($this, 'cacheTags')) {
80
            return (array) static::$cacheTags;
81
        }
82
83
        return $this->model()->cacheTags();
84
    }
85
86
    /**
87
     * Return the primary model object which would
88
     * be used by the repository.
89
     *
90
     * @return \Suitmedia\Cacheable\Contracts\CacheableModel
91
     */
92
    abstract public function model();
93
}
94