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

DatabaseAccountManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 67
rs 10
c 1
b 0
f 0
wmc 10
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isExistAccountId() 0 4 1
A isEqualPassword() 0 8 2
A getAccountById() 0 15 2
A getRole() 0 15 3
A getUserEntity() 0 6 1
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