Completed
Push — master ( a40390...c25130 )
by
11s
created

AuthenticationMiddleware   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C execute() 0 30 7
A __construct() 0 3 1
1
<?php
2
3
namespace LineMob\Core\Mocky\Auth\Middleware;
4
5
use League\Tactician\Middleware;
6
use LineMob\Core\Mocky\Auth\AuthenticationWorkflow;
7
use LineMob\Core\Mocky\Auth\Command\LoginCommand;
8
use LineMob\Core\Template\TextTemplate;
9
10
class AuthenticationMiddleware implements Middleware
11
{
12
    /**
13
     * @var AuthenticationWorkflow
14
     */
15
    private $workflow;
16
17
    public function __construct(AuthenticationWorkflow $workflow)
18
    {
19
        $this->workflow = $workflow;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function execute($command, callable $next)
26
    {
27
        if (!$command->storage) {
28
            throw new \LogicException("Require storage before using this AuthenticationMiddleware!");
29
        }
30
31
        if (!$command instanceof LoginCommand) {
32
            return $next($command);
33
        }
34
35
        $command->active = true;
36
        $command->message = new TextTemplate();
37
38
        if ($this->workflow->doApplyStart($command)) {
39
            return $next($command);
40
        }
41
42
        if ($this->workflow->doApplyEnterUsernameAndPassword($command)) {
43
            return $next($command);
44
        }
45
46
        if ($this->workflow->doApplyEnterUsername($command)) {
47
            return $next($command);
48
        }
49
50
        if ($this->workflow->doApplyEnterPassword($command)) {
51
            return $next($command);
52
        }
53
54
        throw new \LogicException("Unknown case!");
55
    }
56
}
57