Passed
Push — master ( 4b954d...b01812 )
by Gabor
03:38
created

UserGroupStorage::getUserGroupByName()   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 0
Metric Value
dl 12
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\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)
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...
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