Completed
Pull Request — master (#295)
by Anton
06:14
created

Table::generateToken()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 30
rs 8.8571
ccs 16
cts 16
cp 1
crap 1
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
use Bluz\Auth\AbstractTable;
15
use Bluz\Auth\AuthException;
16
use Bluz\Proxy\Auth;
17
use Bluz\Proxy\Session;
18
19
/**
20
 * Auth Table
21
 *
22
 * @package  Application\Auth
23
 *
24
 * @method   static Row findRow($primaryKey)
25
 * @method   static Row findRowWhere($whereList)
26
 *
27
 * @author   Anton Shevchuk
28
 * @created  12.07.11 15:28
29
 */
30
class Table extends AbstractTable
31
{
32
    /**
33
     * Call Hash Function
34
     *
35
     * @param string $password
36
     *
37
     * @throws \Application\Exception
38
     * @return string
39
     */
40
    public static function hash($password)
41
    {
42
        $hash = Auth::getInstance()->getOption('hash');
43
44
        if (!$hash or !is_callable($hash)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
45
            throw new Exception('Hash function for `Auth` package is not callable');
46
        }
47
48
        // encrypt password with secret
49
        return $hash($password);
50
    }
51
52
    /**
53
     * Call Verify Function
54
     *
55
     * @param string $password
56
     * @param string $hash
57
     *
58
     * @throws \Application\Exception
59
     * @return string
60
     */
61 4
    public static function verify($password, $hash)
62
    {
63 4
        $verify = Auth::getInstance()->getOption('verify');
64
65 4
        if (!$verify or !is_callable($verify)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
66
            throw new Exception('Verify function for `Auth` package is not callable');
67
        }
68
69
        // verify password with hash
70 4
        return $verify($password, $hash);
71
    }
72
73
    /**
74
     * Can entity login
75
     *
76
     * @param $user
77
     *
78
     * @throws AuthException
79
     */
80 1
    public static function tryLogin($user)
81
    {
82 1
        switch ($user->status) {
83 1
            case (Users\Table::STATUS_PENDING):
84
                throw new AuthException('Your account is pending activation', 403);
85 1
            case (Users\Table::STATUS_DISABLED):
86
                throw new AuthException('Your account is disabled by administrator', 403);
87 1
            case (Users\Table::STATUS_ACTIVE):
88
                // save user to new session
89 1
                Auth::setIdentity($user);
90 1
                break;
91
            default:
92
                throw new AuthException('User not found');
93
        }
94 1
    }
95
}
96