Passed
Push — master ( 64ab61...b96172 )
by Gabor
09:36
created

UserStorage::getUserById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
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\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 2
    protected function populateEntity(DataEntityInterface &$entity, array $data)
52
    {
53
        /* @var UserEntity $entity */
54 2
        $entity->setUserId($data[$this->idKey])
55 2
            ->setUserName($data[$this->userName])
56 2
            ->setEmail($data[$this->email])
57 2
            ->setPassword($data[$this->password])
58 2
            ->setHash($data[$this->hash])
59 2
            ->setActive($data[$this->isActive])
60 2
            ->setEnabled($data[$this->isEnabled])
61 2
            ->setDateCreated(new DateTime($data[$this->dateCreated]))
62 2
            ->setDateModified(new DateTime($data[$this->dateModified]));
63 2
    }
64
65
    /**
66
     * Returns a User entity identified by (unique) ID.
67
     *
68
     * @param int $identifier
69
     *
70
     * @return bool|UserEntity
71
     */
72 1
    public function getUserById($identifier)
73
    {
74 1
        $entity = false;
75 1
        $data = $this->getDataAdapter()->getData($identifier);
76
77 1
        if (!empty($data)) {
78 1
            $entity = $this->createEntity();
79 1
            $this->populateEntity($entity, $data);
80 1
        }
81
82 1
        return $entity;
83
    }
84
85
    /**
86
     * Returns a User entity identified by (unique) Email.
87
     *
88
     * @param $email
89
     *
90
     * @return bool|UserEntity
91
     */
92 1
    public function getUserByEmail($email)
93
    {
94 1
        $entity = false;
95 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->email => $email], 1);
96
97 1
        if (!empty($dataList)) {
98 1
            $entity = $this->createEntity();
99 1
            $this->populateEntity($entity, $dataList[0]);
100 1
        }
101
102 1
        return $entity;
103
    }
104
}
105