Passed
Pull Request — master (#18)
by
02:27
created

AuthenticationWorkflow   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 154
Duplicated Lines 11.69 %

Importance

Changes 0
Metric Value
wmc 18
dl 18
loc 154
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getConfig() 0 34 1
A doApplyEnterPassword() 9 20 4
B doApplyEnterUsernameAndPassword() 9 21 6
A doApplyEnterUsername() 0 18 4
A captureUserAndPassword() 0 5 1
A doApplyStart() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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