Passed
Push — develop ( cda46f...6a5a5a )
by Anton
07:07
created

Table   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 63
ccs 11
cts 20
cp 0.55
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hash() 0 10 3
A verify() 0 10 3
A tryLogin() 0 13 4
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application\Auth;
10
11
use Application\Exception;
12
use Application\Users;
13
use Bluz\Application;
14
15
use Bluz\Auth\AuthException;
16
use Bluz\Auth\Model\AbstractTable;
17
use Bluz\Proxy\Auth;
18
19
/**
20
 * Auth Table
21
 *
22
 * @package  Application\Auth
23
 *
24
 * @method   static ?Row findRow($primaryKey)
25
 * @see      \Bluz\Db\Table::findRow()
26
 * @method   static ?Row findRowWhere($whereList)
27
 * @see      \Bluz\Db\Table::findRowWhere()
28
 */
29
class Table extends AbstractTable
30
{
31
    /**
32
     * Call Hash Function
33
     *
34
     * @param string $password
35
     *
36
     * @throws \Application\Exception
37
     * @return string
38
     */
39
    public static function hash($password): string
40
    {
41
        $hash = Auth::getInstance()->getOption('hash');
42
43
        if (!$hash || !is_callable($hash)) {
44
            throw new Exception('Hash function for `Auth` package is not callable');
45
        }
46
47
        // encrypt password with secret
48
        return $hash($password);
49
    }
50
51
    /**
52
     * Call Verify Function
53
     *
54
     * @param string $password
55
     * @param string $hash
56
     *
57
     * @throws \Application\Exception
58
     * @return bool
59
     */
60 4
    public static function verify($password, $hash): bool
61
    {
62 4
        $verify = Auth::getInstance()->getOption('verify');
63
64 4
        if (!$verify || !is_callable($verify)) {
65
            throw new Exception('Verify function for `Auth` package is not callable');
66
        }
67
68
        // verify password with hash
69 4
        return $verify($password, $hash);
70
    }
71
72
    /**
73
     * Can entity login
74
     *
75
     * @param $user
76
     *
77
     * @throws AuthException
78
     */
79 1
    public static function tryLogin($user): void
80
    {
81 1
        switch ($user->status) {
82 1
            case (Users\Table::STATUS_PENDING):
83
                throw new AuthException('Your account is pending activation', 403);
84 1
            case (Users\Table::STATUS_DISABLED):
85
                throw new AuthException('Your account is disabled by administrator', 403);
86 1
            case (Users\Table::STATUS_ACTIVE):
87
                // save user to new session
88 1
                Auth::setIdentity($user);
89 1
                break;
90
            default:
91
                throw new AuthException('User not found');
92
        }
93 1
    }
94
}
95