Result::getErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12
 * @link          https://cakephp.org CakePHP(tm) Project
13
 * @since         1.0.0
14
 * @license       https://opensource.org/licenses/mit-license.php MIT License
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phauthentic\Authentication\Authenticator;
20
21
use ArrayAccess;
22
use InvalidArgumentException;
23
24
/**
25
 * Authentication result object
26
 */
27
class Result implements ResultInterface
28
{
29
    /**
30
     * Authentication result status
31
     *
32
     * @var string
33
     */
34
    protected string $status;
35
36
    /**
37
     * The identity data used in the authentication attempt
38
     *
39
     * @var null|\ArrayAccess
40
     */
41
    protected ?ArrayAccess $data;
42
43
    /**
44
     * An array of string reasons why the authentication attempt was unsuccessful
45
     *
46
     * If authentication was successful, this should be an empty array.
47
     *
48
     * @var array<int, string>
49
     */
50
    protected array $errors = [];
51
52
    /**
53
     * Sets the result status, identity, and failure messages
54
     *
55
     * @param null|\ArrayAccess $data The identity data
56
     * @param string $status Status constant equivalent.
57 216
     * @param mixed[] $messages Messages.
58
     * @throws \InvalidArgumentException When invalid identity data is passed.
59 216
     */
60 4
    public function __construct(?ArrayAccess $data, string $status, array $messages = [])
61
    {
62
        if ($status === self::SUCCESS && $data === null) {
63 212
            throw new InvalidArgumentException('Identity data can not be empty with status success.');
64 212
        }
65 212
66 212
        $this->status = $status;
67
        $this->data = $data;
68
        $this->errors = $messages;
69
    }
70
71
    /**
72
     * Returns whether the result represents a successful authentication attempt.
73 76
     *
74
     * @return bool
75 76
     */
76
    public function isValid(): bool
77
    {
78
        return $this->status === ResultInterface::SUCCESS;
79
    }
80
81
    /**
82
     * Get the result status for this authentication attempt.
83 116
     *
84
     * @return string
85 116
     */
86
    public function getStatus(): string
87
    {
88
        return $this->status;
89
    }
90
91
    /**
92
     * Returns the identity data used in the authentication attempt.
93 56
     *
94
     * @return \ArrayAccess|null
95 56
     */
96
    public function getData(): ?ArrayAccess
97
    {
98
        return $this->data;
99
    }
100
101
    /**
102
     * Returns an array of string reasons why the authentication attempt was unsuccessful.
103
     *
104
     * If authentication was successful, this method returns an empty array.
105 44
     *
106
     * @return mixed[]
107 44
     */
108
    public function getErrors(): array
109
    {
110
        return $this->errors;
111
    }
112
}
113