Passed
Push — master ( 37b412...8220b9 )
by Gabor
03:10
created

UserStorage::getEntityData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 1
crap 3
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\ConnectorInterface;
18
use WebHemi\Data\EntityInterface;
19
use WebHemi\Data\Entity\User\UserEntity;
20
use WebHemi\Data\Storage\AbstractStorage;
21
22
/**
23
 * Class UserStorage.
24
 */
25
class UserStorage extends AbstractStorage
26
{
27
    /** @var string */
28
    protected $dataGroup = 'webhemi_user';
29
    /** @var string */
30
    protected $idKey = 'id_user';
31
    /** @var string */
32
    private $userName = 'username';
33
    /** @var string */
34
    private $email = 'email';
35
    /** @var string */
36
    private $password = 'password';
37
    /** @var string */
38
    private $hash = 'hash';
39
    /** @var string */
40
    private $isActive = 'is_active';
41
    /** @var string */
42
    private $isEnabled = 'is_enabled';
43
    /** @var string */
44
    private $dateCreated = 'date_created';
45
    /** @var string */
46
    private $dateModified = 'date_modified';
47
48
    /**
49
     * Populates an entity with storage data.
50
     *
51
     * @param EntityInterface $dataEntity
52
     * @param array           $data
53
     * @return void
54
     */
55 6
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
56
    {
57
        /* @var UserEntity $dataEntity */
58 6
        $dataEntity->setUserId((int) $data[$this->idKey])
59 6
            ->setUserName($data[$this->userName])
60 6
            ->setEmail($data[$this->email])
61 6
            ->setPassword($data[$this->password])
62 6
            ->setHash($data[$this->hash] ?? '')
63 6
            ->setActive((bool) $data[$this->isActive])
64 6
            ->setEnabled((bool) $data[$this->isEnabled])
65 6
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
66 6
            ->setDateModified(new DateTime($data[$this->dateModified] ?? 'now'));
67 6
    }
68
69
    /**
70
     * Get data from an entity.
71
     *
72
     * @param EntityInterface $dataEntity
73
     * @return array
74
     */
75 5
    protected function getEntityData(EntityInterface $dataEntity) : array
76
    {
77
        /** @var UserEntity $dataEntity */
78 5
        $dateCreated = $dataEntity->getDateCreated();
79 5
        $dateModified = $dataEntity->getDateModified();
80
81
        return [
82 5
            $this->idKey => $dataEntity->getKeyData(),
83 5
            $this->userName => $dataEntity->getUserName(),
84 5
            $this->email => $dataEntity->getEmail(),
85 5
            $this->password => $dataEntity->getPassword(),
86 5
            $this->hash => $dataEntity->getHash(),
87 5
            $this->isActive => (int) $dataEntity->getActive(),
88 5
            $this->isEnabled => (int) $dataEntity->getEnabled(),
89 5
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
90 5
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
91
        ];
92
    }
93
94
    /**
95
     * Returns a full set of User entities.
96
     *
97
     * @return null|array
98
     */
99 1
    public function getUserList() : ? array
100
    {
101 1
        return $this->getDataEntitySet([], [ConnectorInterface::OPTION_ORDER => 'email']);
102
    }
103
104
    /**
105
     * Returns a User entity identified by (unique) ID.
106
     *
107
     * @param int $identifier
108
     * @return null|UserEntity
109
     */
110 1
    public function getUserById(int $identifier) : ? UserEntity
111
    {
112
        /** @var null|UserEntity $dataEntity */
113 1
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
114
115 1
        return $dataEntity;
116
    }
117
118
    /**
119
     * Returns a User entity by user name.
120
     *
121
     * @param string $name
122
     * @return null|UserEntity
123
     */
124 1
    public function getUserByUserName(string $name) : ? UserEntity
125
    {
126
        /** @var null|UserEntity $dataEntity */
127 1
        $dataEntity = $this->getDataEntity([$this->userName => $name]);
128
129 1
        return $dataEntity;
130
    }
131
132
    /**
133
     * Returns a User entity by email.
134
     *
135
     * @param string $email
136
     * @return null|UserEntity
137
     */
138 1
    public function getUserByEmail($email) : ? UserEntity
139
    {
140
        /** @var null|UserEntity $dataEntity */
141 1
        $dataEntity = $this->getDataEntity([$this->email => $email]);
142
143 1
        return $dataEntity;
144
    }
145
146
    /**
147
     * Return a User entity by credentials.
148
     *
149
     * @param string $username
150
     * @param string $password
151
     * @return null|UserEntity
152
     */
153 1
    public function getUserByCredentials(string $username, string $password) : ? UserEntity
154
    {
155
        /** @var null|UserEntity $dataEntity */
156 1
        $dataEntity = $this->getDataEntity([$this->userName => $username, $this->password => $password]);
157
158 1
        return $dataEntity;
159
    }
160
}
161