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

Table   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 66
ccs 9
cts 18
cp 0.5
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hash() 0 11 3
A verify() 0 11 3
A tryLogin() 0 15 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
 * @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