1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AtlassianConnectCore\Services; |
4
|
|
|
|
5
|
|
|
use AtlassianConnectCore\Models\Tenant; |
6
|
|
|
use AtlassianConnectCore\Repositories\TenantRepository; |
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class TenantService |
11
|
|
|
* |
12
|
|
|
* @package AtlassianConnectCore\Services |
13
|
|
|
*/ |
14
|
|
|
class TenantService |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var TenantRepository |
18
|
|
|
*/ |
19
|
|
|
protected $repository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TenantService constructor. |
23
|
|
|
* |
24
|
|
|
* @param TenantRepository $tenantRepository |
25
|
|
|
*/ |
26
|
|
|
public function __construct(TenantRepository $tenantRepository) |
27
|
|
|
{ |
28
|
|
|
$this->repository = $tenantRepository; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create or update a tenant |
33
|
|
|
* |
34
|
|
|
* @param bool $withTrashed |
35
|
|
|
* |
36
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|Tenant[] |
37
|
|
|
*/ |
38
|
|
|
public function all($withTrashed = false) |
39
|
|
|
{ |
40
|
|
|
return $this->repository->findAll($withTrashed); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create or update a tenant |
45
|
|
|
* |
46
|
|
|
* @param array $attributes |
47
|
|
|
* |
48
|
|
|
* @return Tenant |
49
|
|
|
*/ |
50
|
|
|
public function createOrUpdate(array $attributes) |
51
|
|
|
{ |
52
|
|
|
/** @var Tenant|null $model */ |
53
|
|
|
$model = $this->repository |
54
|
|
|
->findWhere(['client_key' => $attributes['client_key']]) |
55
|
|
|
->first(); |
56
|
|
|
|
57
|
|
|
if(!$model) { |
58
|
|
|
return $this->repository->create($attributes); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->repository->update($model->id, $attributes); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Update state |
66
|
|
|
* |
67
|
|
|
* @param string $clientKey |
68
|
|
|
* @param string $eventType |
69
|
|
|
* |
70
|
|
|
* @return Tenant |
71
|
|
|
*/ |
72
|
|
|
public function updateState($clientKey, $eventType) |
73
|
|
|
{ |
74
|
|
|
return $this->repository->updateWhere(['client_key' => $clientKey], ['event_type' => $eventType]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Find a tenant by client key |
79
|
|
|
* |
80
|
|
|
* @param string $clientKey |
81
|
|
|
* @param bool $withTrashed |
82
|
|
|
* |
83
|
|
|
* @return Tenant|null |
84
|
|
|
*/ |
85
|
|
|
public function findByClientKey($clientKey, $withTrashed = true) |
86
|
|
|
{ |
87
|
|
|
return $this->repository |
88
|
|
|
->findWhere(['client_key' => $clientKey], $withTrashed) |
89
|
|
|
->first(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Find a tenant by client key |
94
|
|
|
* |
95
|
|
|
* @param string $clientKey |
96
|
|
|
* @param bool $withTrashed |
97
|
|
|
* |
98
|
|
|
* @return Tenant |
99
|
|
|
* |
100
|
|
|
* @throws NotFoundHttpException |
101
|
|
|
*/ |
102
|
|
|
public function findByClientKeyOrFail($clientKey, $withTrashed = true) |
103
|
|
|
{ |
104
|
|
|
if($tenant = $this->findByClientKey($clientKey, $withTrashed)) { |
105
|
|
|
return $tenant; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw new NotFoundHttpException(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Find not-dummied tenants |
113
|
|
|
* |
114
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|Tenant[] |
115
|
|
|
*/ |
116
|
|
|
public function findReals() |
117
|
|
|
{ |
118
|
|
|
return $this->repository->findWhere(['is_dummy' => false], false); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Delete the tenant |
123
|
|
|
* |
124
|
|
|
* @param int $id |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function delete($id) |
129
|
|
|
{ |
130
|
|
|
if(config('plugin.safeDelete')) { |
131
|
|
|
return $this->repository->delete($id); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->repository->forceDelete($id); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get dummy tenant |
139
|
|
|
* |
140
|
|
|
* @return Tenant|null |
141
|
|
|
*/ |
142
|
|
|
public function dummy() |
143
|
|
|
{ |
144
|
|
|
return $this->repository->findDummy(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Make tenant dummied |
149
|
|
|
* |
150
|
|
|
* @param int $id Tenant ID |
151
|
|
|
* |
152
|
|
|
* @return Tenant|null |
153
|
|
|
*/ |
154
|
|
|
public function makeDummy($id) |
155
|
|
|
{ |
156
|
|
|
if(!$this->repository->findById($id)) { |
157
|
|
|
throw new NotFoundHttpException(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->repository->updateWhere(['id' => $id], ['is_dummy' => true]); |
161
|
|
|
} |
162
|
|
|
} |