Passed
Push — master ( 033b76...80b8b4 )
by Anton
02:44
created

AuthController::token()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\App\Controller;
13
14
use Spiral\Auth\AuthContextInterface;
15
use Spiral\Auth\AuthScope;
16
use Spiral\Auth\TokenStorageInterface;
17
use Spiral\Core\Exception\ControllerException;
18
use Spiral\Security\GuardInterface;
19
20
class AuthController
21
{
22
    private $auth;
23
24
    public function __construct(AuthScope $auth)
25
    {
26
        $this->auth = $auth;
27
    }
28
29
    public function do(GuardInterface $guard)
30
    {
31
        if (!$guard->allows('do')) {
32
            throw new ControllerException("Unauthorized permission 'do'", ControllerException::FORBIDDEN);
33
        }
34
35
        return 'ok';
36
    }
37
38
    public function token(AuthContextInterface $authContext)
39
    {
40
        if ($authContext->getToken() !== null) {
41
            return $authContext->getToken()->getID();
42
        }
43
44
        return 'none';
45
    }
46
47
    public function login(AuthContextInterface $authContext, TokenStorageInterface $tokenStorage)
48
    {
49
        $authContext->start(
50
            $tokenStorage->create(['userID' => 1])
51
        );
52
53
        return 'OK';
54
    }
55
56
    public function token2()
57
    {
58
        if ($this->auth->getToken() !== null) {
59
            return $this->auth->getToken()->getID();
60
        }
61
62
        return 'none';
63
    }
64
65
    public function token3()
66
    {
67
        return $this->auth->getToken()->getPayload();
68
    }
69
70
    public function login2(TokenStorageInterface $tokenStorage)
71
    {
72
        $this->auth->start(
73
            $tokenStorage->create(['userID' => 1])
74
        );
75
76
        return 'OK';
77
    }
78
}
79