Completed
Push — master ( 597a5a...27b735 )
by Park Jong-Hun
29s
created

DatabaseAccountManager::getAccountById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Auth\AccountManager;
4
5
use App\Auth\AccountManagerInterface;
6
use App\Auth\HashManager;
7
use App\Auth\Model\Account;
8
use App\Entity\User;
9
use Core\Utils\EntityUtils\EntitySelect;
10
11
class DatabaseAccountManager implements AccountManagerInterface
12
{
13
    public function __construct(array $settings = [])
14
    {
15
    }
16
17
    public function isExistAccountId($accountId)
18
    {
19
        return $this->getUserEntity($accountId) !== null;
20
    }
21
22
    public function isEqualPassword($accountId, $password)
23
    {
24
        if ($this->isExistAccountId($accountId) === false) {
25
            return false;
26
        }
27
28
        return HashManager::getProvider()->isEqualValueAndHash($password, $this->getUserEntity($accountId)->getPassword());
29
    }
30
31
    /**
32
     * @param string $accountId
33
     * @return Account|null
34
     */
35
    public function getAccountById($accountId)
36
    {
37
        $user = $this->getUserEntity($accountId);
38
39
        if($user === null) {
40
            return null;
41
        }
42
43
        $account = new Account();
44
        $account->setAccountId($user->getAccountId());
45
        $account->setPassword($user->getPassword());
46
        $account->setName($user->getAccountId());
47
48
        return $account;
49
    }
50
51
    public function getRole($accountId)
52
    {
53
        if ($this->isExistAccountId($accountId) === false) {
54
            return null;
55
        }
56
57
        $roles = $this->getUserEntity($accountId)->getRoles();
58
        $accountRole = [];
59
60
        foreach ($roles as $item) {
61
            $accountRole[] = $item->getName();
62
        }
63
64
        return $accountRole;
65
    }
66
67
    /**
68
     * @param  string $accountId
69
     * @return User
70
     */
71
    private function getUserEntity($accountId)
72
    {
73
        return EntitySelect::select(User::class)
74
            ->criteria(['accountId' => $accountId])
75
            ->findOne();
76
    }
77
}
78