1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Iben\Statable; |
4
|
|
|
|
5
|
|
|
use Iben\Statable\Models\StateHistory; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Sebdesign\SM\StateMachine\StateMachine; |
8
|
|
|
|
9
|
|
|
trait Statable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var StateMachine |
13
|
|
|
*/ |
14
|
|
|
protected $SM; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
18
|
|
|
*/ |
19
|
|
|
public function stateHistory() |
20
|
|
|
{ |
21
|
|
|
return $this->morphMany( |
|
|
|
|
22
|
|
|
Config::get('laravel-statable.models.state_history', StateHistory::class), |
23
|
|
|
'statable' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $transitionData |
29
|
|
|
*/ |
30
|
|
|
public function addHistoryLine(array $transitionData) |
31
|
|
|
{ |
32
|
|
|
if ($this->getKey()) { |
|
|
|
|
33
|
|
|
$transitionData['actor_id'] = $this->getActorId(); |
34
|
|
|
$this->stateHistory()->create($transitionData); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return int|null |
40
|
|
|
*/ |
41
|
|
|
public function getActorId() |
42
|
|
|
{ |
43
|
|
|
return auth()->id(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return mixed|string |
48
|
|
|
* @throws \Illuminate\Container\EntryNotFoundException |
49
|
|
|
*/ |
50
|
|
|
public function stateIs() |
51
|
|
|
{ |
52
|
|
|
return $this->stateMachine()->getState(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $transition |
57
|
|
|
* @param bool $soft |
58
|
|
|
* @param array $context |
59
|
|
|
* @return bool |
60
|
|
|
* @throws \Illuminate\Container\EntryNotFoundException |
61
|
|
|
* @throws \SM\SMException |
62
|
|
|
*/ |
63
|
|
|
public function apply($transition, $soft = false, $context = []) |
64
|
|
|
{ |
65
|
|
|
if ($this->getKey() === null && $this->saveBeforeTransition()) { |
|
|
|
|
66
|
|
|
$this->save(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->stateMachine()->apply($transition, $soft, $context); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $transition |
74
|
|
|
* @param array $context |
75
|
|
|
* @return bool |
76
|
|
|
* @throws \Illuminate\Container\EntryNotFoundException |
77
|
|
|
* @throws \SM\SMException |
78
|
|
|
*/ |
79
|
|
|
public function canApply($transition, $context = []) |
80
|
|
|
{ |
81
|
|
|
return $this->stateMachine()->can($transition, $context); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return mixed|\Sebdesign\SM\StateMachine\StateMachine |
86
|
|
|
* @throws \Illuminate\Container\EntryNotFoundException |
87
|
|
|
*/ |
88
|
|
|
public function stateMachine() |
89
|
|
|
{ |
90
|
|
|
if (! $this->SM) { |
91
|
|
|
$this->SM = app('sm.factory')->get($this, $this->getGraph()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->SM; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
protected function getGraph() |
101
|
|
|
{ |
102
|
|
|
return 'default'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function saveBeforeTransition() |
109
|
|
|
{ |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.