1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LineMob\Core\Mocky\Auth; |
4
|
|
|
|
5
|
|
|
use LineMob\Core\Input; |
6
|
|
|
use LineMob\Core\Mocky\Auth\Command\BaseCommand; |
7
|
|
|
use LineMob\Core\Mocky\Doctrine\Model\User; |
8
|
|
|
use LineMob\Core\Workflow\AbstractWorkflow; |
9
|
|
|
|
10
|
|
|
class AuthenticationWorkflow extends AbstractWorkflow |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
protected function getConfig() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
'name' => 'Authentication', |
19
|
|
|
'marking_store' => [ |
20
|
|
|
'type' => 'multiple_state', |
21
|
|
|
'arguments' => ['state'], |
22
|
|
|
], |
23
|
|
|
'supported' => [ |
24
|
|
|
User::class, |
25
|
|
|
], |
26
|
|
|
'places' => [ |
27
|
|
|
'started', |
28
|
|
|
'wait_for_username', |
29
|
|
|
'wait_for_password', |
30
|
|
|
'wait_for_username_n_password', |
31
|
|
|
'finished', |
32
|
|
|
], |
33
|
|
|
'transitions' => [ |
34
|
|
|
'start' => [ |
35
|
|
|
'from' => 'started', |
36
|
|
|
'to' => ['wait_for_username_n_password', 'wait_for_username'], |
37
|
|
|
], |
38
|
|
|
'enter_username' => [ |
39
|
|
|
'from' => 'wait_for_username', |
40
|
|
|
'to' => 'wait_for_password', |
41
|
|
|
], |
42
|
|
|
'enter_password' => [ |
43
|
|
|
'from' => 'wait_for_password', |
44
|
|
|
'to' => 'finished', |
45
|
|
|
], |
46
|
|
|
'enter_username_n_password' => [ |
47
|
|
|
'from' => 'wait_for_username_n_password', |
48
|
|
|
'to' => 'finished', |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Input $input |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
private function captureUserAndPassword(Input $input) |
60
|
|
|
{ |
61
|
|
|
$text = trim(preg_replace('|\W+|', ' ', $input->text)); |
62
|
|
|
|
63
|
|
|
return explode(' ', $text); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param BaseCommand $command |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function doApplyStart(BaseCommand $command) |
72
|
|
|
{ |
73
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
74
|
|
|
|
75
|
|
|
if ($workflow->can($subject, 'start')) { |
76
|
|
|
$workflow->apply($subject, 'start'); |
77
|
|
|
|
78
|
|
|
$command->message->text = 'Please Enter username & password.'; |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param BaseCommand $command |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function doApplyEnterUsernameAndPassword(BaseCommand $command) |
92
|
|
|
{ |
93
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
94
|
|
|
|
95
|
|
|
@list($username, $password) = $this->captureUserAndPassword($command->input); |
96
|
|
|
|
97
|
|
|
if ($username && $password && $workflow->can($subject, 'enter_username_n_password')) { |
98
|
|
View Code Duplication |
if ($username === 'demo' && $password === 'demo') { |
|
|
|
|
99
|
|
|
$workflow->apply($subject, 'enter_username_n_password'); |
100
|
|
|
|
101
|
|
|
$command->storage->setLineLastLogin(new \DateTimeImmutable()); |
102
|
|
|
$command->active = false; |
103
|
|
|
$command->message->text = 'Success!'; |
104
|
|
|
} else { |
105
|
|
|
$command->message->text = 'Try again ...'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param BaseCommand $command |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function doApplyEnterUsername(BaseCommand $command) |
120
|
|
|
{ |
121
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
122
|
|
|
$username = $this->captureUserAndPassword($command->input)[0]; |
123
|
|
|
|
124
|
|
|
if ($username && $workflow->can($subject, 'enter_username')) { |
125
|
|
|
if ($username === 'demo') { |
126
|
|
|
$workflow->apply($subject, 'enter_username'); |
127
|
|
|
|
128
|
|
|
$command->message->text = 'Please Enter password!'; |
129
|
|
|
} else { |
130
|
|
|
$command->message->text = 'Not found username, Try again ...'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param BaseCommand $command |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function doApplyEnterPassword(BaseCommand $command) |
145
|
|
|
{ |
146
|
|
|
$workflow = $this->registry->get($subject = $command->storage); |
147
|
|
|
$password = $this->captureUserAndPassword($command->input)[0]; |
148
|
|
|
|
149
|
|
|
if ($password && $workflow->can($subject, 'enter_password')) { |
150
|
|
View Code Duplication |
if ($password === 'demo') { |
|
|
|
|
151
|
|
|
$workflow->apply($subject, 'enter_password'); |
152
|
|
|
|
153
|
|
|
$command->storage->setLineLastLogin(new \DateTimeImmutable()); |
154
|
|
|
$command->active = false; |
155
|
|
|
$command->message->text = 'Success!'; |
156
|
|
|
} else { |
157
|
|
|
$command->message->text = 'Password not match, Try again ...'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.