1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AtlassianConnectCore\Repositories; |
4
|
|
|
|
5
|
|
|
use AtlassianConnectCore\Models\Tenant; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class TenantRepository |
10
|
|
|
* |
11
|
|
|
* @package AtlassianConnectCore\Repositories |
12
|
|
|
*/ |
13
|
|
|
class TenantRepository |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Create a query instance |
17
|
|
|
* |
18
|
|
|
* @param bool $withTrashed |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
21
|
|
|
*/ |
22
|
|
|
protected function query($withTrashed = true) |
23
|
|
|
{ |
24
|
|
|
$query = Tenant::query(); |
25
|
|
|
|
26
|
|
|
if($withTrashed) { |
27
|
|
|
$query->withTrashed(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $query; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Find by ID |
35
|
|
|
* |
36
|
|
|
* @param int $id |
37
|
|
|
* |
38
|
|
|
* @return Tenant|null |
39
|
|
|
*/ |
40
|
|
|
public function findById($id) |
41
|
|
|
{ |
42
|
|
|
/** @var Tenant|null $model */ |
43
|
|
|
$model = $this->query() |
44
|
|
|
->find($id); |
45
|
|
|
|
46
|
|
|
return $model; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Find all tenants |
51
|
|
|
* |
52
|
|
|
* @param bool $withTrashed |
53
|
|
|
* |
54
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|Tenant[] |
55
|
|
|
*/ |
56
|
|
|
public function findAll($withTrashed = false) |
57
|
|
|
{ |
58
|
|
|
return $this->query($withTrashed) |
|
|
|
|
59
|
|
|
->latest() |
60
|
|
|
->get(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Find by ID |
65
|
|
|
* |
66
|
|
|
* @param array $condition |
67
|
|
|
* @param bool $withTrashed |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|Tenant[] |
70
|
|
|
*/ |
71
|
|
|
public function findWhere(array $condition, $withTrashed = true) |
72
|
|
|
{ |
73
|
|
|
return $this->query($withTrashed) |
|
|
|
|
74
|
|
|
->where($condition) |
|
|
|
|
75
|
|
|
->get(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Find dummy tenant |
80
|
|
|
* |
81
|
|
|
* @return Tenant|null |
82
|
|
|
*/ |
83
|
|
|
public function findDummy() |
84
|
|
|
{ |
85
|
|
|
/** @var Tenant|null $model */ |
86
|
|
|
$model = $this->query(false) |
87
|
|
|
->where(['is_dummy' => true]) |
88
|
|
|
->latest() |
89
|
|
|
->first(); |
90
|
|
|
|
91
|
|
|
return $model; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create a tenant |
96
|
|
|
* |
97
|
|
|
* @param array $attributes |
98
|
|
|
* |
99
|
|
|
* @return Tenant |
100
|
|
|
*/ |
101
|
|
|
public function create(array $attributes) |
102
|
|
|
{ |
103
|
|
|
$model = (new Tenant())->fill($attributes); |
104
|
|
|
|
105
|
|
|
$model->save(); |
106
|
|
|
|
107
|
|
|
return $model; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Update a tenant |
112
|
|
|
* |
113
|
|
|
* @param int $id |
114
|
|
|
* @param array $attributes |
115
|
|
|
* |
116
|
|
|
* @return Tenant |
117
|
|
|
*/ |
118
|
|
|
public function update($id, array $attributes) |
119
|
|
|
{ |
120
|
|
|
$model = $this->findById($id); |
121
|
|
|
|
122
|
|
|
if(!$model) { |
123
|
|
|
throw new NotFoundHttpException(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$model->update($attributes); |
127
|
|
|
|
128
|
|
|
return $model; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Update a tenant by addon key |
133
|
|
|
* |
134
|
|
|
* @param string $addonKey |
135
|
|
|
* @param array $attributes |
136
|
|
|
* |
137
|
|
|
* @return Tenant |
138
|
|
|
*/ |
139
|
|
|
public function updateByAddonKey($addonKey, array $attributes) |
140
|
|
|
{ |
141
|
|
|
$model = $this->findWhere(['addon_key' => $addonKey])->first(); |
142
|
|
|
|
143
|
|
|
if(!$model) { |
144
|
|
|
throw new NotFoundHttpException(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->update($model->id, $attributes); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Update a tenant by addon key |
152
|
|
|
* |
153
|
|
|
* @param array $condition |
154
|
|
|
* @param array $attributes |
155
|
|
|
* |
156
|
|
|
* @return Tenant |
157
|
|
|
*/ |
158
|
|
|
public function updateWhere(array $condition, array $attributes) |
159
|
|
|
{ |
160
|
|
|
$model = $this->findWhere($condition)->first(); |
161
|
|
|
|
162
|
|
|
if(!$model) { |
163
|
|
|
throw new NotFoundHttpException(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->update($model->id, $attributes); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Delete a tenant by ID |
171
|
|
|
* |
172
|
|
|
* @param int $id |
173
|
|
|
* |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
public function delete($id) |
177
|
|
|
{ |
178
|
|
|
if($model = $this->findById($id)) { |
179
|
|
|
$model->delete(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Force delete a tenant by ID |
185
|
|
|
* |
186
|
|
|
* @param int $id |
187
|
|
|
* |
188
|
|
|
* @return mixed |
189
|
|
|
*/ |
190
|
|
|
public function forceDelete($id) |
191
|
|
|
{ |
192
|
|
|
if($model = $this->findById($id)) { |
193
|
|
|
$model->forceDelete(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |