PhpArray::getIdentity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace T4web\Authentication\Adapter;
4
5
use Zend\Authentication\Adapter\ValidatableAdapterInterface;
6
use Zend\Authentication\Result;
7
8
class PhpArray implements ValidatableAdapterInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    private $accounts;
14
15
    /**
16
     * @var string
17
     */
18
    private $identity;
19
20
    /**
21
     * @var string
22
     */
23
    private $credential;
24
25
    /**
26
     * @param array $accounts
27
     */
28
    public function __construct(array $accounts)
29
    {
30
        $this->accounts = $accounts;
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function getIdentity()
37
    {
38
        return $this->identity;
39
    }
40
41
    /**
42
     * @param string $identity
43
     * @return ValidatableAdapterInterface
44
     */
45
    public function setIdentity($identity)
46
    {
47
        $this->identity = $identity;
48
        return $this;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getCredential()
55
    {
56
        return $this->credential;
57
    }
58
59
    /**
60
     * @param mixed $credential
61
     * @return ValidatableAdapterInterface
62
     */
63
    public function setCredential($credential)
64
    {
65
        $this->credential = $credential;
66
        return $this;
67
    }
68
69
    /**
70
     * @return Result
71
     */
72
    public function authenticate()
73
    {
74
        if (!isset($this->accounts[$this->identity])) {
75
            return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['Login does not exists']);
76
        }
77
78
        if ($this->accounts[$this->identity] !== $this->credential) {
79
            return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, ['Invalid credentials']);
80
        }
81
82
        return new Result(Result::SUCCESS, $this->identity);
83
    }
84
}
85