Accounts::updateAccount()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 14
cts 14
cp 1
crap 3
rs 9.8333
1
<?php
2
3
namespace kalanis\kw_auth_sources\Sources\Memory;
4
5
6
use kalanis\kw_accounts\AccountsException;
7
use kalanis\kw_accounts\Interfaces as acc_interfaces;
8
use kalanis\kw_auth_sources\AuthSourcesException;
9
use kalanis\kw_auth_sources\Interfaces;
10
use kalanis\kw_auth_sources\Traits\TLang;
11
12
13
/**
14
 * Class Accounts
15
 * @package kalanis\kw_auth_sources\Sources\Memory
16
 * Authenticate class in memory
17
 */
18
class Accounts implements acc_interfaces\IAuth, acc_interfaces\IProcessAccounts
19
{
20
    use TLang;
21
22
    protected Interfaces\IHashes $mode;
23
    /** @var acc_interfaces\IUser[] */
24
    protected array $local = [];
25
    /** @var array<string, string> */
26
    protected array $pass = [];
27
28
    /**
29
     * @param Interfaces\IHashes $mode
30
     * @param acc_interfaces\IUser[] $initial
31
     * @param Interfaces\IKAusTranslations|null $lang
32
     */
33 16
    public function __construct(Interfaces\IHashes $mode, array $initial = [], ?Interfaces\IKAusTranslations $lang = null)
34
    {
35 16
        $this->setAusLang($lang);
36 16
        $this->mode = $mode;
37 16
        $this->local = $initial;
38 16
        foreach ($this->local as $item) {
39 11
            $this->pass[$item->getAuthId()] = 'undefined now';
40
        }
41 16
    }
42
43 5
    public function authenticate(string $userName, array $params = []): ?acc_interfaces\IUser
44
    {
45 5
        if (!isset($params['password'])) {
46 1
            throw new AccountsException($this->getAusLang()->kauPassMustBeSet());
47
        }
48
49
        try {
50 4
            $user = $this->getDataOnly($userName);
51 4
            if ($user && isset($params['password']) && isset($this->pass[$user->getAuthId()])) {
52 3
                if ($this->mode->checkHash(strval($params['password']), $this->pass[$user->getAuthId()])) {
53 2
                    return clone $user;
54
                }
55
            }
56 3
            return null;
57
58 1
        } catch (AuthSourcesException $ex) {
59 1
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
60
        }
61
    }
62
63 11
    public function getDataOnly(string $userName): ?acc_interfaces\IUser
64
    {
65 11
        foreach ($this->local as $local) {
66 8
            if ($local->getAuthName() == $userName) {
67 8
                return clone $local;
68
            }
69
        }
70
71 6
        return null;
72
    }
73
74 4
    public function createAccount(acc_interfaces\IUser $user, string $password): bool
75
    {
76 4
        foreach ($this->local as $local) {
77 3
            if ($local->getAuthName() == $user->getAuthName()) {
78 1
                return false;
79
            }
80 3
            if ($local->getAuthId() == $user->getAuthId()) {
81 1
                return false;
82
            }
83
        }
84
85
        try {
86 3
            $this->local[] = $user;
87 3
            $this->pass[$user->getAuthId()] = $this->mode->createHash($password);
88 2
            return true;
89
90 1
        } catch (AuthSourcesException $ex) {
91 1
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
92
        }
93
    }
94
95 1
    public function readAccounts(): array
96
    {
97 1
        return $this->local;
98
    }
99
100 2
    public function updateAccount(acc_interfaces\IUser $user): bool
101
    {
102 2
        foreach ($this->local as $local) {
103 1
            if ($local->getAuthId() == $user->getAuthId()) {
104 1
                $local->setUserData(
105 1
                    $local->getAuthId(),
106 1
                    $user->getAuthName(),
107 1
                    $user->getGroup(),
108 1
                    $user->getClass(),
109 1
                    $user->getStatus(),
110 1
                    $user->getDisplayName(),
111 1
                    $user->getDir(),
112 1
                    $user->getExtra()
113
                );
114 1
                return true;
115
            }
116
        }
117 1
        return false;
118
    }
119
120 3
    public function updatePassword(string $userName, string $passWord): bool
121
    {
122
        try {
123 3
            $user = $this->getDataOnly($userName);
124 3
            if ($user && isset($this->pass[$user->getAuthId()])) {
125 2
                $this->pass[$user->getAuthId()] = $this->mode->createHash($passWord);
126 1
                return true;
127
            }
128 1
            return false;
129
130 1
        } catch (AuthSourcesException $ex) {
131 1
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
132
        }
133
    }
134
135 2
    public function deleteAccount(string $userName): bool
136
    {
137 2
        $willDelete = null;
138 2
        $use = [];
139 2
        foreach ($this->local as $local) {
140 1
            if ($local->getAuthName() == $userName) {
141 1
                $willDelete = $local;
142
            } else {
143 1
                $use[] = $local;
144
            }
145
        }
146 2
        $this->local = $use;
147
148 2
        if ($willDelete && isset($this->pass[$willDelete->getAuthId()])) {
149 1
            unset($this->pass[$willDelete->getAuthId()]);
150
        }
151
152 2
        return !empty($willDelete);
153
    }
154
}
155