Test Failed
Push — master ( dd2d06...7fea41 )
by Gabor
07:04
created

Result::getIdentity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Auth;
15
16
use WebHemi\Data\Entity\User\UserEntity;
17
18
/**
19
 * Class Result.
20
 */
21
final class Result
22
{
23
    public const FAILURE = 0;
24
    public const FAILURE_IDENTITY_NOT_FOUND = -1;
25
    public const FAILURE_CREDENTIAL_INVALID = -2;
26
    public const FAILURE_OTHER = -3;
27
    public const SUCCESS = 1;
28
29
    /** @var int */
30
    private $code;
31
    /** @var null|UserEntity */
32
    private $userEntity;
33
    /** @var array */
34
    private $messages = [
35
        self::FAILURE => 'Authentication failed.',
36
        self::FAILURE_IDENTITY_NOT_FOUND => 'User is not found.',
37
        self::FAILURE_CREDENTIAL_INVALID => 'The provided credentials are not valid.',
38
        self::FAILURE_OTHER => 'Authentication failed because of unknown reason.',
39
        self::SUCCESS => 'Authenticated.',
40
    ];
41
42
    /**
43
     * Checks the authentication result.
44
     *
45
     * @return bool
46
     */
47 2
    public function isValid() : bool
48
    {
49 2
        return $this->code == 1;
50
    }
51
52
    /**
53
     * Sets the result code.
54
     *
55
     * @param int $code
56
     * @return Result
57
     */
58 3
    public function setCode(int $code) : Result
59
    {
60 3
        if (!isset($this->messages[$code])) {
61 2
            $code = -3;
62
        }
63
64 3
        $this->code = $code;
65
66 3
        return $this;
67
    }
68
69
    /**
70
     * Gets the result code.
71
     *
72
     * @return int
73
     */
74 3
    public function getCode() : int
75
    {
76 3
        return $this->code;
77
    }
78
79
    /**
80
     * Gets the result message.
81
     *
82
     * @return string
83
     */
84
    public function getMessage() : string
85 3
    {
86
        return $this->messages[$this->code];
87 3
    }
88
}
89