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

AuthenticationMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace LineMob\Core\Mocky\Auth;
4
5
use League\Tactician\Middleware;
6
use LineMob\Core\Input;
7
use LineMob\Core\Mocky\Doctrine\Model\User;
8
use LineMob\Core\Template\TextTemplate;
9
use Symfony\Component\Workflow\DefinitionBuilder;
10
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore;
11
use Symfony\Component\Workflow\Registry;
12
use Symfony\Component\Workflow\Transition;
13
use Symfony\Component\Workflow\Workflow;
14
15
class AuthenticationMiddleware implements Middleware
16
{
17
    /**
18
     * @var Registry
19
     */
20
    private $registry;
21
22
    public function __construct()
23
    {
24
        $definitionBuilder = new DefinitionBuilder();
25
        $definition = $definitionBuilder->addPlaces(
26
            ['started', 'wait_for_username', 'wait_for_password', 'wait_for_username_n_password', 'finished']
27
        )
28
            ->addTransition(new Transition('start', 'started', ['wait_for_username_n_password', 'wait_for_username']))
29
            ->addTransition(new Transition('enter_username', 'wait_for_username', 'wait_for_password'))
30
            ->addTransition(new Transition('enter_username_n_password', 'wait_for_username_n_password', 'finished'))
31
            ->addTransition(new Transition('enter_password', 'wait_for_password', 'finished'))
32
            ->build();
33
34
        $workflow = new Workflow($definition, new MultipleStateMarkingStore('state'));
35
        $this->registry = new Registry();
36
        $this->registry->add($workflow, User::class);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function execute($command, callable $next)
43
    {
44
        if (!$subject = $command->storage) {
45
            throw new \LogicException("Require storage before using this AuthenticationMiddleware!");
46
        }
47
48
        if (!$command instanceof LoginCommand) {
49
            return $next($command);
50
        }
51
52
        $command->active = true;
53
        $command->message = new TextTemplate();
54
55
        $workflow = $this->registry->get($subject);
56
57
        if ($workflow->can($subject, 'start')) {
58
            $workflow->apply($subject, 'start');
59
60
            $command->message->text = 'Please Enter username & password.';
61
62
            return $next($command);
63
        }
64
65
        @list($username, $password) = $this->captureUserAndPassword($command->input);
66
67
        if ($username && $password && $workflow->can($subject, 'enter_username_n_password')) {
68 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...
69
                $workflow->apply($subject, 'enter_username_n_password');
70
71
                $command->storage->setLineLastLogin(new \DateTimeImmutable());
0 ignored issues
show
Bug introduced by
The method setLineLastLogin() does not exist on LineMob\Core\Storage\CommandDataInterface. It seems like you code against a sub-type of LineMob\Core\Storage\CommandDataInterface such as LineMob\Core\Mocky\Doctrine\Model\User or LineMob\Core\Mocky\Doctrine\Model\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                $command->storage->/** @scrutinizer ignore-call */ setLineLastLogin(new \DateTimeImmutable());
Loading history...
72
                $command->active = false;
73
                $command->message->text = 'Success!';
74
            } else {
75
                $command->message->text = 'Try again ...';
76
            }
77
78
            return $next($command);
79
        }
80
81
        if ($username && $workflow->can($subject, 'enter_username')) {
82
            if ($username === 'demo') {
83
                $workflow->apply($subject, 'enter_username');
84
85
                $command->message->text = 'Please Enter password!';
86
            } else {
87
                $command->message->text = 'Not found username, Try again ...';
88
            }
89
90
            return $next($command);
91
        }
92
93
        $password = $username;
94
95
        if ($password && $workflow->can($subject, 'enter_password')) {
96 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...
97
                $workflow->apply($subject, 'enter_password');
98
99
                $command->storage->setLineLastLogin(new \DateTimeImmutable());
100
                $command->active = false;
101
                $command->message->text = 'Success!';
102
            } else {
103
                $command->message->text = 'Password not match, Try again ...';
104
            }
105
106
            return $next($command);
107
        }
108
109
        throw new \LogicException("Unknown case!");
110
    }
111
112
    private function captureUserAndPassword(Input $input)
113
    {
114
        $text = trim(preg_replace('|\W+|', ' ', $input->text));
115
116
        return explode(' ', $text);
117
    }
118
}
119