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

PolicyStorage::getEntityData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 14
cts 14
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 4
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\Data\Storage\AccessManagement;
13
14
use DateTime;
15
use WebHemi\Data\Entity\DataEntityInterface;
16
use WebHemi\Data\Entity\AccessManagement\PolicyEntity;
17
use WebHemi\Data\Storage\AbstractDataStorage;
18
use WebHemi\Data\Storage\Traits\GetEntityListFromDataSetTrait;
19
20
/**
21
 * Class PolicyStorage.
22
 */
23
class PolicyStorage extends AbstractDataStorage
24
{
25
    /** @var string */
26
    protected $dataGroup = 'webhemi_am_policy';
27
    /** @var string */
28
    protected $idKey = 'id_am_policy';
29
    /** @var string */
30
    private $resourceId = 'fk_am_resource';
31
    /** @var string */
32
    private $applicationId = 'fk_application';
33
    /** @var string */
34
    private $name = 'name';
35
    /** @var string */
36
    private $title = 'title';
37
    /** @var string */
38
    private $description = 'description';
39
    /** @var string */
40
    private $isReadOnly = 'is_read_only';
41
    /** @var string */
42
    private $isAllowed = 'is_allowed';
43
    /** @var string */
44
    private $dateCreated = 'date_created';
45
    /** @var string */
46
    private $dateModified = 'date_modified';
47
48
    /** @method bool|array<PolicyEntity> getEntityListFromDataSet(array $dataList) */
49
    use GetEntityListFromDataSetTrait;
50
51
    /**
52
     * Populates an entity with storage data.
53
     *
54
     * @param DataEntityInterface $entity
55
     * @param array               $data
56
     */
57 5
    protected function populateEntity(DataEntityInterface &$entity, array $data)
58
    {
59
        /* @var PolicyEntity $entity */
60 5
        $entity->setPolicyId($data[$this->idKey])
61 5
            ->setResourceId($data[$this->resourceId])
62 5
            ->setApplicationId($data[$this->applicationId])
63 5
            ->setName($data[$this->name])
64 5
            ->setTitle($data[$this->title])
65 5
            ->setDescription($data[$this->description])
66 5
            ->setReadOnly($data[$this->isReadOnly])
67 5
            ->setAllowed($data[$this->isAllowed])
68 5
            ->setDateCreated(new DateTime($data[$this->dateCreated]))
69 5
            ->setDateModified(new DateTime($data[$this->dateModified]));
70 5
    }
71
72
    /**
73
     * Get data from an entity.
74
     *
75
     * @param DataEntityInterface $entity
76
     * @return array
77
     */
78 3 View Code Duplication
    protected function getEntityData(DataEntityInterface $entity)
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...
79
    {
80
        /** @var PolicyEntity $entity */
81 3
        $dateCreated = $entity->getDateCreated();
82 3
        $dateModified = $entity->getDateModified();
83
84
        return [
85 3
            $this->idKey => $entity->getKeyData(),
86 3
            $this->resourceId => $entity->getResourceId(),
87 3
            $this->applicationId => $entity->getApplicationId(),
88 3
            $this->name => $entity->getName(),
89 3
            $this->title => $entity->getTitle(),
90 3
            $this->description => $entity->getDescription(),
91 3
            $this->isReadOnly => (int)$entity->getReadOnly(),
92 3
            $this->isAllowed => (int)$entity->getAllowed(),
93 3
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
94 3
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
95 3
        ];
96
    }
97
98
    /**
99
     * Returns a Policy entity identified by (unique) ID.
100
     *
101
     * @param int $identifier
102
     *
103
     * @return bool|PolicyEntity
104
     */
105 1
    public function getPolicyById($identifier)
106
    {
107 1
        $entity = false;
108 1
        $data = $this->getDataAdapter()->getData($identifier);
109
110 1
        if (!empty($data)) {
111 1
            $entity = $this->createEntity();
112 1
            $this->populateEntity($entity, $data);
113 1
        }
114
115 1
        return $entity;
116
    }
117
118
    /**
119
     * Returns a Policy entity by name.
120
     *
121
     * @param string $name
122
     *
123
     * @return bool|PolicyEntity
124
     */
125 1 View Code Duplication
    public function getPolicyByName($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...
126
    {
127 1
        $entity = false;
128 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->name => $name], 1);
129
130 1
        if (!empty($dataList)) {
131 1
            $entity = $this->createEntity();
132 1
            $this->populateEntity($entity, $dataList[0]);
133 1
        }
134
135 1
        return $entity;
136
    }
137
138
    /**
139
     * Returns a set of Policy entities identified by Resource ID.
140
     *
141
     * @param int $resourceId
142
     *
143
     * @return bool|array<PolicyEntity>
144
     */
145 1
    public function getPoliciesByResourceId($resourceId)
146
    {
147 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->resourceId => $resourceId]);
148
149 1
        return $this->getEntityListFromDataSet($dataList);
150
    }
151
152
    /**
153
     * Returns a set of Policy entities identified by Application ID.
154
     *
155
     * @param int $applicationId
156
     *
157
     * @return bool|array<PolicyEntity>
158
     */
159 1
    public function getPoliciesByApplicationId($applicationId)
160
    {
161 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->applicationId => $applicationId]);
162
163 1
        return $this->getEntityListFromDataSet($dataList);
164
    }
165
166
    /**
167
     * Returns a set of Policy entities identified by both Resource and Application IDs.
168
     *
169
     * @param int $resourceId
170
     * @param int $applicationId
171
     *
172
     * @return bool|array<PolicyEntity>
173
     */
174 1
    public function getPoliciesByResourceAndApplicationIds($resourceId, $applicationId)
175
    {
176 1
        $dataList = $this->getDataAdapter()->getDataSet(
177
            [
178 1
                $this->resourceId => $resourceId,
179 1
                $this->applicationId => $applicationId
180 1
            ]
181 1
        );
182
183 1
        return $this->getEntityListFromDataSet($dataList);
184
    }
185
}
186