|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SebastianBerc\Repositories\Mediators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container as Application; |
|
6
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use SebastianBerc\Repositories\Repository; |
|
9
|
|
|
use SebastianBerc\Repositories\Services\CacheService; |
|
10
|
|
|
use SebastianBerc\Repositories\Services\CriteriaService; |
|
11
|
|
|
use SebastianBerc\Repositories\Services\DatabaseService; |
|
12
|
|
|
use SebastianBerc\Repositories\Services\TransformService; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class RepositoryMediator. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Sebastian Berć <[email protected]> |
|
18
|
|
|
* @copyright Copyright (c) Sebastian Berć |
|
19
|
|
|
*/ |
|
20
|
|
|
class RepositoryMediator |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Contains Laravel Application instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @var Application |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $app; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Contains a repository instance. |
|
31
|
|
|
* |
|
32
|
|
|
* @var Repository |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $repository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Contains a database service. |
|
38
|
|
|
* |
|
39
|
|
|
* @var DatabaseService |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $database; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Contains an cache service. |
|
45
|
|
|
* |
|
46
|
|
|
* @var CacheService |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $cache; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Contains a transform service. |
|
52
|
|
|
* |
|
53
|
|
|
* @var TransformService |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $transform; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Contains an Criteria service. |
|
59
|
|
|
* |
|
60
|
|
|
* @var CriteriaService |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $criteria; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create a new Repositry Mediator instance. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Application $app |
|
68
|
|
|
* @param Repository $repository |
|
69
|
|
|
*/ |
|
70
|
120 |
|
public function __construct(Application $app, Repository $repository) |
|
71
|
|
|
{ |
|
72
|
120 |
|
$this->app = $app; |
|
73
|
120 |
|
$this->repository = $repository; |
|
74
|
120 |
|
$this->cache = new CacheService($app, $repository); |
|
75
|
120 |
|
$this->database = new DatabaseService($app, $repository); |
|
76
|
120 |
|
$this->transform = new TransformService($app, $repository); |
|
77
|
120 |
|
$this->criteria = new CriteriaService($app, $repository); |
|
78
|
120 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Retrieve data from cache storage, or execute method |
|
82
|
|
|
* on database and store result then return it. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $caller |
|
85
|
|
|
* @param array $parameters |
|
86
|
|
|
* |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
20 |
|
public function cache($caller, array $parameters = []) |
|
90
|
|
|
{ |
|
91
|
20 |
|
return call_user_func_array([$this->cache, $caller], $parameters); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Execute method with parameters on database service. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $caller |
|
98
|
|
|
* @param array $parameters |
|
99
|
|
|
* |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
86 |
|
public function database($caller, array $parameters = []) |
|
103
|
|
|
{ |
|
104
|
86 |
|
return call_user_func_array([$this->database, $caller], $parameters); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Execute transform method on specified transformer. |
|
109
|
|
|
* |
|
110
|
|
|
* @param mixed $collection |
|
111
|
|
|
* |
|
112
|
|
|
* @return Collection |
|
113
|
|
|
*/ |
|
114
|
68 |
|
public function transform($collection) |
|
115
|
|
|
{ |
|
116
|
68 |
|
if (!$collection instanceof Collection) { |
|
117
|
|
|
// If given "collection" is an object we need to wrap it into an array, and pass into collection. |
|
118
|
48 |
|
$collection = new Collection(is_array($collection) ? $collection : [$collection]); |
|
119
|
48 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
// If transform is declared or set, we can execute transform on his object. |
|
122
|
68 |
|
if ($this->hasTransformer()) { |
|
123
|
26 |
|
return $this->transform->executeOn($collection); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Otherwise, we can return collection without transformation. |
|
127
|
42 |
|
return $collection; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Execute transform method on paginator items with specified transformer. |
|
132
|
|
|
* |
|
133
|
|
|
* @param LengthAwarePaginator $paginator |
|
134
|
|
|
* |
|
135
|
|
|
* @return LengthAwarePaginator |
|
136
|
|
|
*/ |
|
137
|
20 |
|
public function transformPaginator(LengthAwarePaginator $paginator) |
|
138
|
|
|
{ |
|
139
|
20 |
|
if (!$this->hasTransformer()) { |
|
140
|
16 |
|
return $paginator; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
4 |
|
$items = $this->transform($paginator->items())->toArray(); |
|
144
|
|
|
|
|
145
|
4 |
|
return new LengthAwarePaginator($items, $paginator->total(), $paginator->perPage(), $paginator->currentPage()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Determinate if repository has a transformer. |
|
150
|
|
|
* |
|
151
|
|
|
* @return bool |
|
152
|
|
|
*/ |
|
153
|
82 |
|
public function hasTransformer() |
|
154
|
|
|
{ |
|
155
|
82 |
|
return (bool) $this->repository->transformer; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns criteria service on model query. |
|
160
|
|
|
* |
|
161
|
|
|
* @return CriteriaService |
|
162
|
|
|
*/ |
|
163
|
24 |
|
public function criteria() |
|
164
|
|
|
{ |
|
165
|
24 |
|
return $this->criteria; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Determinate if repository has an criteria. |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
84 |
|
public function hasCriteria() |
|
174
|
|
|
{ |
|
175
|
84 |
|
return $this->criteria->hasCriteria(); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|