Passed
Push — master ( 37b412...8220b9 )
by Gabor
03:10
created

PolicyStorage::getPolicies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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