1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Models\Lookup\JobPosterStatusTransition; |
6
|
|
|
use App\Models\User; |
7
|
|
|
|
8
|
|
|
class JobStatusTransitionManager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The list of all status transitions. |
12
|
|
|
* |
13
|
|
|
* @var \Illuminate\Support\Collection |
14
|
|
|
*/ |
15
|
|
|
private $status_transitions = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Return the collection of all transitions. |
19
|
|
|
* This method is meant to cache the results from the database. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Support\Collection |
22
|
|
|
*/ |
23
|
|
|
protected function transitions() |
24
|
|
|
{ |
25
|
|
|
if ($this->status_transitions == null) { |
26
|
|
|
$this->status_transitions = JobPosterStatusTransition::all(); |
27
|
|
|
} |
28
|
|
|
return $this->status_transitions; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns true if a JobStatusTransition exists with these $from and $to keys. |
33
|
|
|
* |
34
|
|
|
* @param string $from |
35
|
|
|
* @param string $to |
36
|
|
|
* @return boolean |
37
|
|
|
*/ |
38
|
|
|
public function isLegalTransition(string $from, string $to): bool |
39
|
|
|
{ |
40
|
|
|
return $this->transitions()->some(function ($transition) use ($from, $to) { |
41
|
|
|
return $transition->from->key === $from |
42
|
|
|
&& $transition->to->key === $to; |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return an array of the keys of all the possible legal destination states. |
48
|
|
|
* |
49
|
|
|
* @param string $from |
50
|
|
|
* @return string[] |
51
|
|
|
*/ |
52
|
|
|
public function legalDestinations(string $from) |
53
|
|
|
{ |
54
|
|
|
return $this->legalTransitions($from) |
55
|
|
|
->pluck('to.key') |
56
|
|
|
->unique() |
57
|
|
|
->all(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return collection of all transitions models with same from key. |
62
|
|
|
* |
63
|
|
|
* @param string $from |
64
|
|
|
* @return \Illuminate\Support\Collection |
65
|
|
|
*/ |
66
|
|
|
public function legalTransitions(string $from) |
67
|
|
|
{ |
68
|
|
|
return $this->transitions()->where('from.key', $from); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Does the user have the correct role to claim ownership of this transition. |
73
|
|
|
* |
74
|
|
|
* @param User $user |
75
|
|
|
* @param JobPosterStatusTransition $transition |
76
|
|
|
* @return boolean |
77
|
|
|
*/ |
78
|
|
|
protected function userOwnsTransition(User $user, JobPosterStatusTransition $transition): bool |
79
|
|
|
{ |
80
|
|
|
return $user->isAdmin() |
81
|
|
|
|| $user->user_role_id === $transition->owner_user_role_id; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns true if there is a legal transition exists with these from and to keys, |
86
|
|
|
* which this user has permissions for. |
87
|
|
|
* |
88
|
|
|
* @param User $user |
89
|
|
|
* @param string $from |
90
|
|
|
* @param string $to |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
|
|
public function userCanTransition(User $user, string $from, string $to): bool |
94
|
|
|
{ |
95
|
|
|
return $this->transitions()->some(function ($transition) use ($user, $from, $to) { |
96
|
|
|
return $transition->from->key === $from |
97
|
|
|
&& $transition->to->key === $to |
98
|
|
|
&& $this->userOwnsTransition($user, $transition); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|