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

ResourceStorage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 99
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A populateEntity() 0 11 1
A getEntityData() 0 16 3
A getResources() 0 4 1
A getResourceById() 0 7 1
A getResourceByName() 0 7 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\ResourceEntity;
19
use WebHemi\Data\Storage\AbstractStorage;
20
21
/**
22
 * Class ResourceStorage.
23
 */
24
class ResourceStorage extends AbstractStorage
25
{
26
    /** @var string */
27
    protected $dataGroup = 'webhemi_am_resource';
28
    /** @var string */
29
    protected $idKey = 'id_am_resource';
30
    /** @var string */
31
    private $name = 'name';
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 $dateCreated = 'date_created';
40
    /** @var string */
41
    private $dateModified = 'date_modified';
42
43
    /**
44
     * Populates an entity with storage data.
45
     *
46
     * @param EntityInterface $dataEntity
47
     * @param array           $data
48
     * @return void
49
     */
50 3
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
51
    {
52
        /* @var ResourceEntity $dataEntity */
53 3
        $dataEntity->setResourceId((int) $data[$this->idKey])
54 3
            ->setName($data[$this->name])
55 3
            ->setTitle($data[$this->title])
56 3
            ->setDescription($data[$this->description])
57 3
            ->setReadOnly((bool) $data[$this->isReadOnly])
58 3
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
59 3
            ->setDateModified(new DateTime($data[$this->dateModified] ?? 'now'));
60 3
    }
61
62
    /**
63
     * Get data from an entity.
64
     *
65
     * @param EntityInterface $dataEntity
66
     * @return array
67
     */
68 3
    protected function getEntityData(EntityInterface $dataEntity) : array
69
    {
70
        /** @var ResourceEntity $dataEntity */
71 3
        $dateCreated = $dataEntity->getDateCreated();
72 3
        $dateModified = $dataEntity->getDateModified();
73
74
        return [
75 3
            $this->idKey => $dataEntity->getKeyData(),
76 3
            $this->name => $dataEntity->getName(),
77 3
            $this->title => $dataEntity->getTitle(),
78 3
            $this->description => $dataEntity->getDescription(),
79 3
            $this->isReadOnly => (int) $dataEntity->getReadOnly(),
80 3
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
81 3
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
82
        ];
83
    }
84
85
    /**
86
     * Returns a full set of Resource entities.
87
     *
88
     * @return null|array
89
     */
90 1
    public function getResources() : ? array
91
    {
92 1
        return $this->getDataEntitySet([]);
93
    }
94
95
    /**
96
     * Returns a Resource entity identified by (unique) ID.
97
     *
98
     * @param int $identifier
99
     * @return null|ResourceEntity
100
     */
101 1
    public function getResourceById($identifier) : ? ResourceEntity
102
    {
103
        /** @var null|ResourceEntity $dataEntity */
104 1
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
105
106 1
        return $dataEntity;
107
    }
108
109
    /**
110
     * Returns an Resource entity by name.
111
     *
112
     * @param string $name
113
     * @return null|ResourceEntity
114
     */
115 1
    public function getResourceByName($name) : ? ResourceEntity
116
    {
117
        /** @var null|ResourceEntity $dataEntity */
118 1
        $dataEntity = $this->getDataEntity([$this->name => $name]);
119
120 1
        return $dataEntity;
121
    }
122
}
123