Completed
Push — master ( 692e73...54b2f0 )
by Gabor
03:24
created

UserStorage::getUserById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 12
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
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\DataStorage\User;
13
14
use DateTime;
15
use WebHemi\DataEntity\DataEntityInterface;
16
use WebHemi\DataEntity\User\UserEntity;
17
use WebHemi\DataStorage\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 $lastIp = 'last_ip';
38
    /** @var string */
39
    private $registerIp = 'register_ip';
40
    /** @var string */
41
    private $isActive = 'is_active';
42
    /** @var string */
43
    private $isEnabled = 'is_enabled';
44
    /** @var string */
45
    private $timeLogin = 'time_login';
46
    /** @var string */
47
    private $timeRegister = 'time_register';
48
49
    /**
50
     * Populates an entity with storage data.
51
     *
52
     * @param DataEntityInterface $entity
53
     * @param array               $data
54
     */
55 2
    protected function populateEntity(DataEntityInterface &$entity, array $data)
56
    {
57
        /* @var UserEntity $entity */
58 2
        $entity->setUserId($data[$this->idKey])
59 2
            ->setUserName($data[$this->userName])
60 2
            ->setEmail($data[$this->email])
61 2
            ->setPassword($data[$this->password])
62 2
            ->setHash($data[$this->hash])
63 2
            ->setLastIp($data[$this->lastIp])
64 2
            ->setRegisterIp($data[$this->registerIp])
65 2
            ->setActive($data[$this->isActive])
66 2
            ->setEnabled($data[$this->isEnabled])
67 2
            ->setTimeLogin(new DateTime($data[$this->timeLogin]))
68 2
            ->setTimeRegister(new DateTime($data[$this->timeRegister]));
69 2
    }
70
71
    /**
72
     * Returns a User entity identified by (unique) ID.
73
     *
74
     * @param int $identifier
75
     *
76
     * @return bool|UserEntity
77
     */
78 1 View Code Duplication
    public function getUserById($identifier)
79
    {
80 1
        $entity = false;
81 1
        $data = $this->getDataAdapter()->getData($identifier);
82
83 1
        if (!empty($data)) {
84 1
            $entity = $this->createEntity();
85 1
            $this->populateEntity($entity, $data);
86 1
        }
87
88 1
        return $entity;
89
    }
90
91
    /**
92
     * Returns a User entity identified by (unique) Email.
93
     *
94
     * @param $email
95
     *
96
     * @return bool|UserEntity
97
     */
98 1 View Code Duplication
    public function getUserByEmail($email)
99
    {
100 1
        $entity = false;
101 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->email => $email], 1);
102
103 1
        if (!empty($dataList)) {
104 1
            $entity = $this->createEntity();
105 1
            $this->populateEntity($entity, $dataList[0]);
106 1
        }
107
108 1
        return $entity;
109
    }
110
}
111