|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 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
|
|
|
namespace WebHemi\Data\Storage\User; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use WebHemi\Data\Entity\DataEntityInterface; |
|
16
|
|
|
use WebHemi\Data\Entity\User\UserEntity; |
|
17
|
|
|
use WebHemi\Data\Storage\AbstractDataStorage; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class UserStorage. |
|
21
|
|
|
*/ |
|
22
|
|
|
class UserStorage extends AbstractDataStorage |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $dataGroup = 'webhemi_user'; |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $idKey = 'id_user'; |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $userName = 'username'; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $email = 'email'; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $password = 'password'; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $hash = 'hash'; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $isActive = 'is_active'; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
private $isEnabled = 'is_enabled'; |
|
40
|
|
|
/** @var string */ |
|
41
|
|
|
private $dateCreated = 'date_created'; |
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
private $dateModified = 'date_modified'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Populates an entity with storage data. |
|
47
|
|
|
* |
|
48
|
|
|
* @param DataEntityInterface $entity |
|
49
|
|
|
* @param array $data |
|
50
|
|
|
*/ |
|
51
|
4 |
|
protected function populateEntity(DataEntityInterface &$entity, array $data) |
|
52
|
|
|
{ |
|
53
|
|
|
/* @var UserEntity $entity */ |
|
54
|
4 |
|
$entity->setUserId($data[$this->idKey]) |
|
55
|
4 |
|
->setUserName($data[$this->userName]) |
|
56
|
4 |
|
->setEmail($data[$this->email]) |
|
57
|
4 |
|
->setPassword($data[$this->password]) |
|
58
|
4 |
|
->setHash($data[$this->hash]) |
|
59
|
4 |
|
->setActive($data[$this->isActive]) |
|
60
|
4 |
|
->setEnabled($data[$this->isEnabled]) |
|
61
|
4 |
|
->setDateCreated(new DateTime($data[$this->dateCreated])) |
|
62
|
4 |
|
->setDateModified(new DateTime($data[$this->dateModified])); |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get data from an entity. |
|
67
|
|
|
* |
|
68
|
|
|
* @param DataEntityInterface $entity |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
3 |
View Code Duplication |
protected function getEntityData(DataEntityInterface $entity) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
/** @var UserEntity $entity */ |
|
74
|
3 |
|
$dateCreated = $entity->getDateCreated(); |
|
75
|
3 |
|
$dateModified = $entity->getDateModified(); |
|
76
|
|
|
|
|
77
|
|
|
return [ |
|
78
|
3 |
|
$this->idKey => $entity->getKeyData(), |
|
79
|
3 |
|
$this->userName => $entity->getUserName(), |
|
80
|
3 |
|
$this->email => $entity->getEmail(), |
|
81
|
3 |
|
$this->password => $entity->getPassword(), |
|
82
|
3 |
|
$this->hash => $entity->getHash(), |
|
83
|
3 |
|
$this->isActive => (int)$entity->getActive(), |
|
84
|
3 |
|
$this->isEnabled => (int)$entity->getEnabled(), |
|
85
|
3 |
|
$this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null, |
|
86
|
3 |
|
$this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null |
|
87
|
3 |
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns a User entity identified by (unique) ID. |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $identifier |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool|UserEntity |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getUserById($identifier) |
|
98
|
|
|
{ |
|
99
|
1 |
|
$entity = false; |
|
100
|
1 |
|
$data = $this->getDataAdapter()->getData($identifier); |
|
101
|
|
|
|
|
102
|
1 |
|
if (!empty($data)) { |
|
103
|
1 |
|
$entity = $this->createEntity(); |
|
104
|
1 |
|
$this->populateEntity($entity, $data); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return $entity; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns a User entity by user name. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $name |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool|UserEntity |
|
116
|
|
|
*/ |
|
117
|
1 |
View Code Duplication |
public function getUserByUserName($name) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
1 |
|
$entity = false; |
|
120
|
1 |
|
$dataList = $this->getDataAdapter()->getDataSet([$this->userName => $name], 1); |
|
121
|
|
|
|
|
122
|
1 |
|
if (!empty($dataList)) { |
|
123
|
1 |
|
$entity = $this->createEntity(); |
|
124
|
1 |
|
$this->populateEntity($entity, $dataList[0]); |
|
125
|
1 |
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
return $entity; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns a User entity by email. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $email |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool|UserEntity |
|
136
|
|
|
*/ |
|
137
|
1 |
View Code Duplication |
public function getUserByEmail($email) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
1 |
|
$entity = false; |
|
140
|
1 |
|
$dataList = $this->getDataAdapter()->getDataSet([$this->email => $email], 1); |
|
141
|
|
|
|
|
142
|
1 |
|
if (!empty($dataList)) { |
|
143
|
1 |
|
$entity = $this->createEntity(); |
|
144
|
1 |
|
$this->populateEntity($entity, $dataList[0]); |
|
145
|
1 |
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
return $entity; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.