|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Modules\Script\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Modules\Script\Dtos\CreateScriptDto; |
|
6
|
|
|
use Modules\Script\Dtos\GrantUserExclusivityDto; |
|
7
|
|
|
use Modules\Script\Dtos\UpdateUserExclusivityDto; |
|
8
|
|
|
use Modules\Script\Entities\Script; |
|
9
|
|
|
use Modules\Script\Entities\ScriptConfigProfile; |
|
10
|
|
|
use Modules\Script\Entities\ScriptExclusivity; |
|
11
|
|
|
use Modules\Script\Entities\ScriptRelease; |
|
12
|
|
|
use Modules\Script\Entities\ScriptReview; |
|
13
|
|
|
use Modules\Script\Entities\ScriptReviewReply; |
|
14
|
|
|
use Modules\Script\Events\ScriptWasCreatedEvent; |
|
15
|
|
|
use Modules\Script\Events\ScriptWasUpdatedEvent; |
|
16
|
|
|
use Modules\Script\Events\ScriptWasDeletedEvent; |
|
17
|
|
|
use Modules\Script\Contracts\ScriptServiceContract; |
|
18
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
19
|
|
|
use Modules\Script\Exceptions\ScriptAlreadyReviewedException; |
|
20
|
|
|
use Modules\Script\Exceptions\ScriptReviewReplyAlreadyExistsException; |
|
21
|
|
|
use Modules\Script\Exceptions\UserAlreadyHasExclusivityException; |
|
22
|
|
|
use Modules\Script\Exceptions\UserDoesNotHaveExclusivityException; |
|
23
|
|
|
use Modules\Script\Guards\ScriptAlreadyReviewedGuard; |
|
24
|
|
|
use Modules\Script\Support\Version; |
|
25
|
|
|
|
|
26
|
|
|
class ScriptService implements ScriptServiceContract |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @param $id |
|
30
|
|
|
* @return Script|null |
|
31
|
|
|
*/ |
|
32
|
10 |
|
public function resolve($id): ?Script |
|
33
|
|
|
{ |
|
34
|
10 |
|
if ($id instanceof Script) { |
|
35
|
8 |
|
return $id; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
return Script::find($id); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $userId |
|
43
|
|
|
* @return Script[] |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getByAuthorId($userId): Collection |
|
46
|
|
|
{ |
|
47
|
1 |
|
return Script::where(Script::AUTHOR_ID, $userId)->get(); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $id |
|
52
|
|
|
* @return Script|null |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function update($id, array $data): Script |
|
55
|
|
|
{ |
|
56
|
1 |
|
$script = $this->resolve($id); |
|
57
|
1 |
|
$script->update($data); |
|
58
|
1 |
|
event(new ScriptWasUpdatedEvent($script)); |
|
59
|
1 |
|
return $script; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $id |
|
64
|
|
|
* @return Script |
|
65
|
|
|
*/ |
|
66
|
7 |
|
public function create(CreateScriptDto $data): Script |
|
67
|
|
|
{ |
|
68
|
7 |
|
$data->user_id = get_authenticated_user_id(); |
|
69
|
7 |
|
$script = Script::create($data->toArray()); |
|
70
|
7 |
|
event(new ScriptWasCreatedEvent($script)); |
|
71
|
7 |
|
return $script; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $id |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function delete($id): bool |
|
79
|
|
|
{ |
|
80
|
1 |
|
$script = $this->resolve($id); |
|
81
|
1 |
|
$deleted = $script->delete(); |
|
82
|
1 |
|
if ($deleted) |
|
83
|
1 |
|
event(new ScriptWasDeletedEvent($script)); |
|
84
|
1 |
|
return $deleted; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
public function releaseVersion($id, array $data): Script |
|
88
|
|
|
{ |
|
89
|
7 |
|
$script = $this->resolve($id); |
|
90
|
7 |
|
$releases = $script->releases()->get(); |
|
91
|
7 |
|
if ($releases->isEmpty()) |
|
92
|
7 |
|
$lastVersion = "0.0.0"; |
|
93
|
|
|
else |
|
94
|
1 |
|
$lastVersion = $releases->last()->version; |
|
95
|
|
|
|
|
96
|
7 |
|
$version = new Version($lastVersion); |
|
97
|
|
|
|
|
98
|
7 |
|
switch ($data['type']) { |
|
99
|
7 |
|
case 'MAJOR': |
|
100
|
1 |
|
$version->incrementMajor(); |
|
101
|
1 |
|
break; |
|
102
|
7 |
|
case 'MINOR': |
|
103
|
7 |
|
$version->incrementMinor(); |
|
104
|
7 |
|
break; |
|
105
|
|
|
case 'PATCH': |
|
106
|
|
|
$version->incrementPatch(); |
|
107
|
|
|
break; |
|
108
|
|
|
default: |
|
109
|
|
|
throw new \Exception("Invalid release type specified"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
//TODO ADD CONFIG TEMPLATE |
|
113
|
7 |
|
$script->releases()->create([ |
|
114
|
7 |
|
ScriptRelease::VERSION => $version->__toString(), |
|
115
|
7 |
|
ScriptRelease::TYPE => $data['type'], |
|
116
|
7 |
|
ScriptRelease::CHANGELOG => $data['changelog'] |
|
117
|
|
|
]); |
|
118
|
|
|
|
|
119
|
7 |
|
return $script; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
public function publishReview($id, array $data): ScriptReview |
|
123
|
|
|
{ |
|
124
|
2 |
|
$script = $this->resolve($id); |
|
125
|
|
|
|
|
126
|
2 |
|
guard(new ScriptAlreadyReviewedGuard($script)); |
|
127
|
|
|
|
|
128
|
|
|
//TODO CHECK IF THE USER IS ACTUALLY SUBSCRIBED TO THE SCRIPT |
|
129
|
|
|
//TODO PREVENT THE AUTHOR FROM REVIEWING HIS OWN SCRIPT |
|
130
|
|
|
|
|
131
|
2 |
|
$review = $script->reviews()->create([ |
|
132
|
2 |
|
ScriptReview::REVIEWER_ID => get_authenticated_user_id(), |
|
133
|
2 |
|
ScriptReview::VERSION => $script->releases->last()->version, |
|
|
|
|
|
|
134
|
2 |
|
ScriptReview::MESSAGE => $data['message'] |
|
135
|
|
|
]); |
|
136
|
|
|
|
|
137
|
2 |
|
return $review; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
public function publishReviewReply($id, array $data): ScriptReviewReply |
|
141
|
|
|
{ |
|
142
|
1 |
|
$script = $this->resolve($id); |
|
143
|
|
|
|
|
144
|
1 |
|
$review = $script->reviews()->withTrashed()->find($data['review_id']); |
|
145
|
|
|
|
|
146
|
1 |
|
if (!isset($review)) { |
|
147
|
|
|
throw new ScriptAlreadyReviewedException(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
if ($review->reply !== null) { |
|
151
|
1 |
|
throw new ScriptReviewReplyAlreadyExistsException(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
$reply = $review->reply()->create([ |
|
155
|
1 |
|
ScriptReviewReply::REPLIER_ID => get_authenticated_user_id(), |
|
156
|
1 |
|
ScriptReviewReply::MESSAGE => $data['message'] |
|
157
|
|
|
]); |
|
158
|
|
|
|
|
159
|
1 |
|
return $reply; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
3 |
|
public function grantUserExclusivity($id, GrantUserExclusivityDto $data): ScriptExclusivity |
|
163
|
|
|
{ |
|
164
|
3 |
|
$script = $this->resolve($id); |
|
165
|
3 |
|
if ($script->exclusivity()->where(ScriptExclusivity::USER_ID, $data->user_id)->first() !== null) { |
|
166
|
|
|
throw new UserAlreadyHasExclusivityException(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
3 |
|
return $script->exclusivity()->create($data->toArray()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
public function removeUserExclusivity($id, $userId): bool |
|
173
|
|
|
{ |
|
174
|
1 |
|
$script = $this->resolve($id); |
|
175
|
|
|
|
|
176
|
1 |
|
if (($exclusivity = $script->exclusivity()->where(ScriptExclusivity::USER_ID, $userId)->first()) === null) { |
|
177
|
|
|
throw new UserDoesNotHaveExclusivityException(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
return $exclusivity->delete(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
public function updateUserExclusivity($id, UpdateUserExclusivityDto $data): ScriptExclusivity |
|
184
|
|
|
{ |
|
185
|
1 |
|
$script = $this->resolve($id); |
|
186
|
|
|
|
|
187
|
1 |
|
if (($exclusivity = $script->exclusivity()->where(ScriptExclusivity::USER_ID, $data->user_id ?? null)->first()) === null) { |
|
188
|
|
|
throw new UserDoesNotHaveExclusivityException(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
1 |
|
$exclusivity->update($data->toArray()); |
|
192
|
|
|
|
|
193
|
1 |
|
return $exclusivity; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function subscribe($id, array $data): ScriptExclusivity |
|
197
|
|
|
{ |
|
198
|
|
|
// TODO: Implement subscribe() method. |
|
199
|
|
|
} |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
public function unsubscribe($id, array $data): ScriptExclusivity |
|
202
|
|
|
{ |
|
203
|
|
|
// TODO: Implement unsubscribe() method. |
|
204
|
|
|
} |
|
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
public function createConfigProfile($id, array $data): ScriptConfigProfile |
|
207
|
|
|
{ |
|
208
|
|
|
// TODO: Implement createConfigProfile() method. |
|
209
|
|
|
} |
|
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
public function updateConfigProfile($id, array $data): ScriptConfigProfile |
|
212
|
|
|
{ |
|
213
|
|
|
// TODO: Implement updateConfigProfile() method. |
|
214
|
|
|
} |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
public function deleteConfigProfile($id): bool |
|
217
|
|
|
{ |
|
218
|
|
|
// TODO: Implement deleteConfigProfile() method. |
|
219
|
|
|
} |
|
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
|