1
|
|
|
<?php namespace Modules\Blog\Repositories\Cache; |
2
|
|
|
|
3
|
|
|
use Modules\Blog\Repositories\Collection; |
4
|
|
|
use Modules\Blog\Repositories\PostRepository; |
5
|
|
|
use Modules\Core\Repositories\Cache\BaseCacheDecorator; |
6
|
|
|
|
7
|
|
|
class CachePostDecorator extends BaseCacheDecorator implements PostRepository |
8
|
|
|
{ |
9
|
|
|
public function __construct(PostRepository $post) |
10
|
|
|
{ |
11
|
|
|
parent::__construct(); |
12
|
|
|
$this->entityName = 'posts'; |
13
|
|
|
$this->repository = $post; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Return the latest x blog posts |
18
|
|
|
* @param int $amount |
19
|
|
|
* @return Collection |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function latest($amount = 5) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
return $this->cache |
24
|
|
|
->tags($this->entityName, 'global') |
25
|
|
|
->remember("{$this->locale}.{$this->entityName}.latest.{$amount}", $this->cacheTime, |
26
|
|
|
function () use ($amount) { |
27
|
|
|
return $this->repository->latest($amount); |
28
|
|
|
} |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the previous post of the given post |
34
|
|
|
* @param object $post |
35
|
|
|
* @return object |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function getPreviousOf($post) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$postId = $post->id; |
40
|
|
|
|
41
|
|
|
return $this->cache |
42
|
|
|
->tags($this->entityName, 'global') |
43
|
|
|
->remember("{$this->locale}.{$this->entityName}.getPreviousOf.{$postId}", $this->cacheTime, |
44
|
|
|
function () use ($post) { |
45
|
|
|
return $this->repository->getPreviousOf($post); |
46
|
|
|
} |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the next post of the given post |
52
|
|
|
* @param object $post |
53
|
|
|
* @return object |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function getNextOf($post) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$postId = $post->id; |
58
|
|
|
|
59
|
|
|
return $this->cache |
60
|
|
|
->tags($this->entityName, 'global') |
61
|
|
|
->remember("{$this->locale}.{$this->entityName}.getNextOf.{$postId}", $this->cacheTime, |
62
|
|
|
function () use ($post) { |
63
|
|
|
return $this->repository->getNextOf($post); |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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.