1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Cache; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\Group as GroupModel; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Group as GroupRepositoryContract; |
7
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Group as GroupRepository; |
8
|
|
|
use Illuminate\Contracts\Cache\Repository; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
|
11
|
|
|
class Group implements GroupRepository |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
|
|
|
|
14
|
|
|
* @var GroupRepositoryContract |
15
|
|
|
*/ |
16
|
|
|
private $groupRepository; |
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* @var Repository |
19
|
|
|
*/ |
20
|
|
|
private $cache; |
|
|
|
|
21
|
|
|
|
22
|
44 |
|
public function __construct(GroupRepositoryContract $groupRepository, Repository $cache) |
|
|
|
|
23
|
|
|
{ |
24
|
44 |
|
$this->groupRepository = $groupRepository; |
25
|
44 |
|
$this->cache = $cache; |
26
|
44 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get a group by ID |
30
|
|
|
* |
31
|
|
|
* @param int $id ID of the group |
32
|
|
|
* @return GroupModel |
|
|
|
|
33
|
|
|
*/ |
34
|
27 |
|
public function getById(int $id): GroupModel |
35
|
|
|
{ |
36
|
|
|
return $this->cache->rememberForever(static::class . '@getById:' . $id, function() use ($id) { |
|
|
|
|
37
|
27 |
|
return $this->groupRepository->getById($id); |
38
|
27 |
|
}); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get a group by its data provider ID |
43
|
|
|
* |
44
|
|
|
* @param int $dataProviderId |
|
|
|
|
45
|
|
|
* @return GroupModel |
|
|
|
|
46
|
|
|
*/ |
47
|
3 |
|
public function getByDataProviderId(int $dataProviderId): GroupModel |
48
|
|
|
{ |
49
|
|
|
return $this->cache->rememberForever(static::class . '@getByDataProviderId:' . $dataProviderId, function() use ($dataProviderId) { |
|
|
|
|
50
|
3 |
|
return $this->groupRepository->getByDataProviderId($dataProviderId); |
51
|
3 |
|
}); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all groups |
56
|
|
|
* |
57
|
|
|
* @return Collection|GroupModel[] |
58
|
|
|
*/ |
59
|
2 |
|
public function all(): Collection |
60
|
|
|
{ |
61
|
2 |
|
return $this->groupRepository->all(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a new group |
66
|
|
|
* |
67
|
|
|
* @param int $dataProviderId |
|
|
|
|
68
|
|
|
* @return GroupModel |
|
|
|
|
69
|
|
|
*/ |
70
|
3 |
|
public function create(int $dataProviderId): GroupModel |
71
|
|
|
{ |
72
|
3 |
|
return $this->groupRepository->create($dataProviderId); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Delete a group by ID |
77
|
|
|
* |
78
|
|
|
* @param int $id |
|
|
|
|
79
|
|
|
*/ |
|
|
|
|
80
|
2 |
|
public function delete(int $id): void |
81
|
|
|
{ |
82
|
2 |
|
$this->groupRepository->delete($id); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Paginate through all the groups |
87
|
|
|
* |
88
|
|
|
* @param int $page The page number to return |
|
|
|
|
89
|
|
|
* @param int $perPage The number of results to return per page |
90
|
|
|
* |
91
|
|
|
* @return Collection|GroupModel[] |
92
|
|
|
*/ |
93
|
3 |
|
public function paginate(int $page, int $perPage): Collection |
94
|
|
|
{ |
95
|
3 |
|
return $this->groupRepository->paginate($page, $perPage); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the number of groups |
100
|
|
|
* |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
3 |
|
public function count(): int |
104
|
|
|
{ |
105
|
|
|
return $this->cache->rememberForever(static::class . '@count', function() { |
|
|
|
|
106
|
3 |
|
return $this->groupRepository->count(); |
107
|
3 |
|
}); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Update the group model |
112
|
|
|
* |
113
|
|
|
* @param int $id |
114
|
|
|
* @param int $dataProviderId New data provider ID |
115
|
|
|
* @return GroupModel |
|
|
|
|
116
|
|
|
*/ |
117
|
2 |
|
public function update(int $id, int $dataProviderId): GroupModel |
118
|
|
|
{ |
119
|
2 |
|
return $this->groupRepository->update($id, $dataProviderId); |
120
|
|
|
} |
121
|
|
|
} |