|
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\UserGroupEntity; |
|
17
|
|
|
use WebHemi\Data\Storage\AbstractDataStorage; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class UserGroupStorage. |
|
21
|
|
|
*/ |
|
22
|
|
|
class UserGroupStorage extends AbstractDataStorage |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $dataGroup = 'webhemi_user_group'; |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $idKey = 'id_user_group'; |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $name = 'name'; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $title = 'title'; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $description = 'description'; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $isReadOnly = 'is_read_only'; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $dateCreated = 'date_created'; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
private $dateModified = 'date_modified'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Populates an entity with storage data. |
|
43
|
|
|
* |
|
44
|
|
|
* @param DataEntityInterface $entity |
|
45
|
|
|
* @param array $data |
|
46
|
|
|
*/ |
|
47
|
2 |
|
protected function populateEntity(DataEntityInterface &$entity, array $data) |
|
48
|
|
|
{ |
|
49
|
|
|
/* @var UserGroupEntity $entity */ |
|
50
|
2 |
|
$entity->setUserGroupId($data[$this->idKey]) |
|
51
|
2 |
|
->setName($data[$this->name]) |
|
52
|
2 |
|
->setTitle($data[$this->title]) |
|
53
|
2 |
|
->setDescription($data[$this->description]) |
|
54
|
2 |
|
->setReadOnly($data[$this->isReadOnly]) |
|
55
|
2 |
|
->setDateCreated(new DateTime($data[$this->dateCreated])) |
|
56
|
2 |
|
->setDateModified(new DateTime($data[$this->dateModified])); |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get data from an entity. |
|
61
|
|
|
* |
|
62
|
|
|
* @param DataEntityInterface $entity |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
1 |
|
protected function getEntityData(DataEntityInterface $entity) |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var UserGroupEntity $entity */ |
|
68
|
1 |
|
$dateCreated = $entity->getDateCreated(); |
|
69
|
1 |
|
$dateModified = $entity->getDateModified(); |
|
70
|
|
|
|
|
71
|
|
|
return [ |
|
72
|
1 |
|
$this->idKey => $entity->getKeyData(), |
|
73
|
1 |
|
$this->name => $entity->getName(), |
|
74
|
1 |
|
$this->title => $entity->getTitle(), |
|
75
|
1 |
|
$this->description => $entity->getDescription(), |
|
76
|
1 |
|
$this->isReadOnly => (int)$entity->getReadOnly(), |
|
77
|
1 |
|
$this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null, |
|
78
|
1 |
|
$this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null |
|
79
|
1 |
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns a User Group entity identified by (unique) ID. |
|
84
|
|
|
* |
|
85
|
|
|
* @param int $identifier |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool|UserGroupEntity |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getUserGroupById($identifier) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$entity = false; |
|
92
|
1 |
|
$data = $this->getDataAdapter()->getData($identifier); |
|
93
|
|
|
|
|
94
|
1 |
|
if (!empty($data)) { |
|
95
|
1 |
|
$entity = $this->createEntity(); |
|
96
|
1 |
|
$this->populateEntity($entity, $data); |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $entity; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns a User Group entity by name. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool|UserGroupEntity |
|
108
|
|
|
*/ |
|
109
|
1 |
View Code Duplication |
public function getUserGroupByName($name) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
1 |
|
$entity = false; |
|
112
|
1 |
|
$dataList = $this->getDataAdapter()->getDataSet([$this->name => $name], 1); |
|
113
|
|
|
|
|
114
|
1 |
|
if (!empty($dataList)) { |
|
115
|
1 |
|
$entity = $this->createEntity(); |
|
116
|
1 |
|
$this->populateEntity($entity, $dataList[0]); |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
return $entity; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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.