Passed
Push — master ( d26101...1eb4af )
by Gabor
03:11
created

UserMetaStorage::getUserMetaForUserId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 9
nc 2
nop 1
crap 3
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 WebHemi\DataEntity\DataEntityInterface;
15
use WebHemi\DataEntity\User\UserMetaEntity;
16
use WebHemi\DataStorage\AbstractDataStorage;
17
18
/**
19
 * Class UserMetaStorage.
20
 */
21
class UserMetaStorage extends AbstractDataStorage
22
{
23
    /** @var string */
24
    protected $dataGroup = 'webhemi_user_meta';
25
    /** @var string */
26
    protected $idKey = 'id_user_meta';
27
    /** @var string */
28
    private $userId = 'fk_user';
29
    /** @var string */
30
    private $metaKey = 'meta_key';
31
    /** @var string */
32
    private $metaData = 'meta_data';
33
34
    /**
35
     * Populates an entity with storage data.
36
     *
37
     * @param DataEntityInterface $entity
38
     * @param array               $data
39
     */
40 2
    protected function populateEntity(DataEntityInterface &$entity, array $data)
41
    {
42
        /* @var UserMetaEntity $entity */
43 2
        $entity->setUserMetaId($data[$this->idKey])
44 2
            ->setUserId($data[$this->userId])
45 2
            ->setMetaKey($data[$this->metaKey])
46 2
            ->setMetaData($data[$this->metaData]);
47 2
    }
48
49
    /**
50
     * Returns a User entity identified by (unique) ID.
51
     *
52
     * @param int $identifier
53
     *
54
     * @return bool|UserMetaEntity
55
     */
56 1 View Code Duplication
    public function getUserMetaById($identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
57
    {
58 1
        $entity = false;
59 1
        $data = $this->getDataAdapter()->getData($identifier);
60
61 1
        if (!empty($data)) {
62 1
            $entity = $this->createEntity();
63 1
            $this->populateEntity($entity, $data);
64 1
        }
65
66 1
        return $entity;
67
    }
68
69
    /**
70
     * Returns a User entity identified by (unique) Email.
71
     *
72
     * @param mixed $userId
73
     *
74
     * @return array<UserMetaEntity>
75
     */
76 1
    public function getUserMetaForUserId($userId)
77
    {
78 1
        $entityList = false;
79 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->userId => $userId]);
80
81 1
        if (!empty($dataList)) {
82 1
            foreach ($dataList as $metaData) {
83
                /** @var UserMetaEntity $entity */
84 1
                $entity = $this->createEntity();
85 1
                $this->populateEntity($entity, $metaData);
86 1
                $entityList[] = $entity;
87 1
            }
88 1
        }
89
90 1
        return $entityList;
91
    }
92
}
93