Completed
Push — master ( f01bd2...878b1b )
by Anton
13s
created

Table::verify()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
nc 2
dl 0
loc 11
ccs 4
cts 5
cp 0.8
c 0
b 0
f 0
cc 3
eloc 5
nop 2
crap 3.072
rs 9.4285
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
 * @author   Anton Shevchuk
30
 * @created  12.07.11 15:28
31
 */
32
class Table extends AbstractTable
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
33
{
34
    /**
35
     * Call Hash Function
36
     *
37
     * @param string $password
38
     *
39
     * @throws \Application\Exception
40
     * @return string
41
     */
42
    public static function hash($password) : string
43
    {
44
        $hash = Auth::getInstance()->getOption('hash');
45
46
        if (!$hash || !is_callable($hash)) {
47
            throw new Exception('Hash function for `Auth` package is not callable');
48
        }
49
50
        // encrypt password with secret
51
        return $hash($password);
52
    }
53
54
    /**
55
     * Call Verify Function
56
     *
57
     * @param string $password
58
     * @param string $hash
59
     *
60
     * @throws \Application\Exception
61
     * @return bool
62
     */
63 4
    public static function verify($password, $hash) : bool
64
    {
65 4
        $verify = Auth::getInstance()->getOption('verify');
66
67 4
        if (!$verify || !is_callable($verify)) {
68
            throw new Exception('Verify function for `Auth` package is not callable');
69
        }
70
71
        // verify password with hash
72 4
        return $verify($password, $hash);
73
    }
74
75
    /**
76
     * Can entity login
77
     *
78
     * @param $user
79
     *
80
     * @throws AuthException
81
     */
82 1
    public static function tryLogin($user) : void
83
    {
84 1
        switch ($user->status) {
85
            case (Users\Table::STATUS_PENDING):
86
                throw new AuthException('Your account is pending activation', 403);
87
            case (Users\Table::STATUS_DISABLED):
88
                throw new AuthException('Your account is disabled by administrator', 403);
89
            case (Users\Table::STATUS_ACTIVE):
90
                // save user to new session
91 1
                Auth::setIdentity($user);
92 1
                break;
93
            default:
94
                throw new AuthException('User not found');
95
        }
96 1
    }
97
}
98