Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

UserStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 126
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A populateEntity() 0 13 1
A getEntityData() 0 18 3
A getUserById() 0 7 1
A getUserByUserName() 0 7 1
A getUserByEmail() 0 7 1
A getUserByCredentials() 0 7 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Storage\User;
15
16
use WebHemi\DateTime;
17
use WebHemi\Data\EntityInterface;
18
use WebHemi\Data\Entity\User\UserEntity;
19
use WebHemi\Data\Storage\AbstractStorage;
20
21
/**
22
 * Class UserStorage.
23
 */
24
class UserStorage extends AbstractStorage
25
{
26
    /** @var string */
27
    protected $dataGroup = 'webhemi_user';
28
    /** @var string */
29
    protected $idKey = 'id_user';
30
    /** @var string */
31
    private $userName = 'username';
32
    /** @var string */
33
    private $email = 'email';
34
    /** @var string */
35
    private $password = 'password';
36
    /** @var string */
37
    private $hash = 'hash';
38
    /** @var string */
39
    private $isActive = 'is_active';
40
    /** @var string */
41
    private $isEnabled = 'is_enabled';
42
    /** @var string */
43
    private $dateCreated = 'date_created';
44
    /** @var string */
45
    private $dateModified = 'date_modified';
46
47
    /**
48
     * Populates an entity with storage data.
49
     *
50
     * @param EntityInterface $dataEntity
51
     * @param array           $data
52
     * @return void
53
     */
54 5
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
55
    {
56
        /* @var UserEntity $dataEntity */
57 5
        $dataEntity->setUserId((int) $data[$this->idKey])
58 5
            ->setUserName($data[$this->userName])
59 5
            ->setEmail($data[$this->email])
60 5
            ->setPassword($data[$this->password])
61 5
            ->setHash($data[$this->hash])
62 5
            ->setActive((bool) $data[$this->isActive])
63 5
            ->setEnabled((bool) $data[$this->isEnabled])
64 5
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
65 5
            ->setDateModified(new DateTime($data[$this->dateModified] ?? 'now'));
66 5
    }
67
68
    /**
69
     * Get data from an entity.
70
     *
71
     * @param EntityInterface $dataEntity
72
     * @return array
73
     */
74 4
    protected function getEntityData(EntityInterface $dataEntity) : array
75
    {
76
        /** @var UserEntity $dataEntity */
77 4
        $dateCreated = $dataEntity->getDateCreated();
78 4
        $dateModified = $dataEntity->getDateModified();
79
80
        return [
81 4
            $this->idKey => $dataEntity->getKeyData(),
82 4
            $this->userName => $dataEntity->getUserName(),
83 4
            $this->email => $dataEntity->getEmail(),
84 4
            $this->password => $dataEntity->getPassword(),
85 4
            $this->hash => $dataEntity->getHash(),
86 4
            $this->isActive => (int) $dataEntity->getActive(),
87 4
            $this->isEnabled => (int) $dataEntity->getEnabled(),
88 4
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
89 4
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
90
        ];
91
    }
92
93
    /**
94
     * Returns a User entity identified by (unique) ID.
95
     *
96
     * @param int $identifier
97
     * @return null|UserEntity
98
     */
99 1
    public function getUserById(int $identifier) : ? UserEntity
100
    {
101
        /** @var null|UserEntity $dataEntity */
102 1
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
103
104 1
        return $dataEntity;
105
    }
106
107
    /**
108
     * Returns a User entity by user name.
109
     *
110
     * @param string $name
111
     * @return null|UserEntity
112
     */
113 1
    public function getUserByUserName(string $name) : ? UserEntity
114
    {
115
        /** @var null|UserEntity $dataEntity */
116 1
        $dataEntity = $this->getDataEntity([$this->userName => $name]);
117
118 1
        return $dataEntity;
119
    }
120
121
    /**
122
     * Returns a User entity by email.
123
     *
124
     * @param string $email
125
     * @return null|UserEntity
126
     */
127 1
    public function getUserByEmail($email) : ? UserEntity
128
    {
129
        /** @var null|UserEntity $dataEntity */
130 1
        $dataEntity = $this->getDataEntity([$this->email => $email]);
131
132 1
        return $dataEntity;
133
    }
134
135
    /**
136
     * Return a User entity by credentials.
137
     *
138
     * @param string $username
139
     * @param string $password
140
     * @return null|UserEntity
141
     */
142 1
    public function getUserByCredentials(string $username, string $password) : ? UserEntity
143
    {
144
        /** @var null|UserEntity $dataEntity */
145 1
        $dataEntity = $this->getDataEntity([$this->userName => $username, $this->password => $password]);
146
147 1
        return $dataEntity;
148
    }
149
}
150