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

PolicyStorage::populateEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
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\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
19
/**
20
 * Class PolicyStorage.
21
 */
22
class PolicyStorage extends AbstractDataStorage
23
{
24
    /** @var string */
25
    protected $dataGroup = 'webhemi_am_policy';
26
    /** @var string */
27
    protected $idKey = 'id_am_policy';
28
    /** @var string */
29
    private $resourceId = 'fk_am_resource';
30
    /** @var string */
31
    private $applicationId = 'fk_application';
32
    /** @var string */
33
    private $title = 'title';
34
    /** @var string */
35
    private $description = 'description';
36
    /** @var string */
37
    private $isReadOnly = 'is_read_only';
38
    /** @var string */
39
    private $isAllowed = 'is_allowed';
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
    protected function populateEntity(DataEntityInterface &$entity, array $data)
52
    {
53
        /* @var PolicyEntity $entity */
54
        $entity->setPolicyId($data[$this->idKey])
55
            ->setResourceId($data[$this->resourceId])
56
            ->setApplicationId($data[$this->applicationId])
57
            ->setTitle($data[$this->title])
58
            ->setDescription($data[$this->description])
59
            ->setReadOnly($data[$this->isReadOnly])
60
            ->setAllowed($data[$this->isAllowed])
61
            ->setDateCreated(new DateTime($data[$this->dateCreated]))
62
            ->setDateModified(new DateTime($data[$this->dateModified]));
63
    }
64
65
    /**
66
     * Returns a Policy entity identified by (unique) ID.
67
     *
68
     * @param int $identifier
69
     *
70
     * @return bool|PolicyEntity
71
     */
72
    public function getPolicyById($identifier)
73
    {
74
        $entity = false;
75
        $data = $this->getDataAdapter()->getData($identifier);
76
77
        if (!empty($data)) {
78
            $entity = $this->createEntity();
79
            $this->populateEntity($entity, $data);
80
        }
81
82
        return $entity;
83
    }
84
}
85