Completed
Push — develop ( 36f41e...c17836 )
by Nicolas
02:57
created

BaseCacheDecorator::getByAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 1
eloc 7
nc 1
nop 3
1
<?php namespace Modules\Core\Repositories\Cache;
2
3
use Modules\Core\Repositories\BaseRepository;
4
5
abstract class BaseCacheDecorator implements BaseRepository
6
{
7
    /**
8
     * @var \Modules\Core\Repositories\BaseRepository
9
     */
10
    protected $repository;
11
    /**
12
     * @var \Illuminate\Cache\Repository
13
     */
14
    protected $cache;
15
    /**
16
     * @var string The entity name
17
     */
18
    protected $entityName;
19
    /**
20
     * @var string The application locale
21
     */
22
    protected $locale;
23
24
    /**
25
     * @var int caching time
26
     */
27
    protected $cacheTime;
28
29
    public function __construct()
30
    {
31
        $this->cache = app('Illuminate\Cache\Repository');
32
        $this->cacheTime = app('Illuminate\Config\Repository')->get('cache.time', 60);
33
        $this->locale = app()->getLocale();
34
    }
35
36
    /**
37
     * @param  int   $id
38
     * @return mixed
39
     */
40 View Code Duplication
    public function find($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        return $this->cache
43
            ->tags($this->entityName, 'global')
44
            ->remember("{$this->locale}.{$this->entityName}.find.{$id}", $this->cacheTime,
45
                function () use ($id) {
46
                    return $this->repository->find($id);
47
                }
48
            );
49
    }
50
51
    /**
52
     * @return \Illuminate\Database\Eloquent\Collection
53
     */
54 View Code Duplication
    public function all()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        return $this->cache
57
            ->tags($this->entityName, 'global')
58
            ->remember("{$this->locale}.{$this->entityName}.all", $this->cacheTime,
59
                function () {
60
                    return $this->repository->all();
61
                }
62
            );
63
    }
64
65
    /**
66
     * Return all categories in the given language
67
     *
68
     * @param  string $lang
69
     * @return object
70
     */
71 View Code Duplication
    public function allTranslatedIn($lang)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        return $this->cache
74
            ->tags($this->entityName, 'global')
75
            ->remember("{$this->locale}.{$this->entityName}.allTranslatedIn.{$lang}", $this->cacheTime,
76
                function () use ($lang) {
77
                    return $this->repository->allTranslatedIn($lang);
78
                }
79
            );
80
    }
81
82
    /**
83
     * Find a resource by the given slug
84
     * @param  string $slug
85
     * @return object
86
     */
87 View Code Duplication
    public function findBySlug($slug)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        return $this->cache
90
            ->tags($this->entityName, 'global')
91
            ->remember("{$this->locale}.{$this->entityName}.findBySlug.{$slug}", $this->cacheTime,
92
                function () use ($slug) {
93
                    return $this->repository->findBySlug($slug);
94
                }
95
            );
96
    }
97
98
    /**
99
     * Create a resource
100
     *
101
     * @param $data
102
     * @return mixed
103
     */
104
    public function create($data)
105
    {
106
        $this->cache->tags($this->entityName)->flush();
107
108
        return $this->repository->create($data);
109
    }
110
111
    /**
112
     * Update a resource
113
     *
114
     * @param        $model
115
     * @param  array $data
116
     * @return mixed
117
     */
118
    public function update($model, $data)
119
    {
120
        $this->cache->tags($this->entityName)->flush();
121
122
        return $this->repository->update($model, $data);
123
    }
124
125
    /**
126
     * Destroy a resource
127
     *
128
     * @param $model
129
     * @return mixed
130
     */
131
    public function destroy($model)
132
    {
133
        $this->cache->tags($this->entityName)->flush();
134
135
        return $this->repository->destroy($model);
136
    }
137
138
    /**
139
     * Find a resource by an array of attributes
140
     * @param  array  $attributes
141
     * @return object
142
     */
143 View Code Duplication
    public function findByAttributes(array $attributes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        $tagIdentifier = json_encode($attributes);
146
147
        return $this->cache
148
            ->tags($this->entityName, 'global')
149
            ->remember("{$this->locale}.{$this->entityName}.findByAttributes.{$tagIdentifier}", $this->cacheTime,
150
                function () use ($attributes) {
151
                    return $this->repository->findByAttributes($attributes);
152
                }
153
            );
154
    }
155
156
    /**
157
     * Get resources by an array of attributes
158
     * @param array $attributes
159
     * @param null|string $orderBy
160
     * @param string $sortOrder
161
     * @return \Illuminate\Database\Eloquent\Collection
162
     */
163
    public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
164
    {
165
        $tagIdentifier = json_encode($attributes);
166
167
        return $this->cache
168
            ->tags($this->entityName, 'global')
169
            ->remember("{$this->locale}.{$this->entityName}.findByAttributes.{$tagIdentifier}.{$orderBy}.{$sortOrder}", $this->cacheTime,
170
                function () use ($attributes, $orderBy, $sortOrder) {
171
                    return $this->repository->getByAttributes($attributes, $orderBy, $sortOrder);
172
                }
173
            );
174
    }
175
176
    /**
177
     * Return a collection of elements who's ids match
178
     * @param array $ids
179
     * @return mixed
180
     */
181 View Code Duplication
    public function findByMany(array $ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $tagIdentifier = json_encode($ids);
184
185
        return $this->cache
186
            ->tags($this->entityName, 'global')
187
            ->remember("{$this->locale}.{$this->entityName}.findByMany.{$tagIdentifier}", $this->cacheTime,
188
                function () use ($ids) {
189
                    return $this->repository->findByMany($ids);
190
                }
191
            );
192
    }
193
194
    /**
195
     * Clear the cache for this Repositories' Entity
196
     * @return bool
197
     */
198
    public function clearCache()
199
    {
200
        return $this->cache->tags($this->entityName)->flush();
201
    }
202
}
203