Passed
Push — master ( 5c25b8...502eee )
by Gabor
05:16
created

ResourceStorage::getResources()   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\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 $type = 'type';
36
    /** @var string */
37
    private $description = 'description';
38
    /** @var string */
39
    private $isReadOnly = 'is_read_only';
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 EntityInterface $dataEntity
49
     * @param array           $data
50
     * @return void
51
     */
52 3
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
53
    {
54
        /* @var ResourceEntity $dataEntity */
55 3
        $dataEntity->setResourceId((int) $data[$this->idKey])
56 3
            ->setName($data[$this->name])
57 3
            ->setTitle($data[$this->title])
58 3
            ->setType($data[$this->type])
59 3
            ->setDescription($data[$this->description])
60 3
            ->setReadOnly((bool) $data[$this->isReadOnly])
61 3
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
62 3
            ->setDateModified(new DateTime($data[$this->dateModified] ?? 'now'));
63 3
    }
64
65
    /**
66
     * Get data from an entity.
67
     *
68
     * @param EntityInterface $dataEntity
69
     * @return array
70
     */
71 3
    protected function getEntityData(EntityInterface $dataEntity) : array
72
    {
73
        /** @var ResourceEntity $dataEntity */
74 3
        $dateCreated = $dataEntity->getDateCreated();
75 3
        $dateModified = $dataEntity->getDateModified();
76
77
        return [
78 3
            $this->idKey => $dataEntity->getKeyData(),
79 3
            $this->name => $dataEntity->getName(),
80 3
            $this->title => $dataEntity->getTitle(),
81 3
            $this->type => $dataEntity->getType(),
82 3
            $this->description => $dataEntity->getDescription(),
83 3
            $this->isReadOnly => (int) $dataEntity->getReadOnly(),
84 3
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
85 3
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
86
        ];
87
    }
88
89
    /**
90
     * Returns a full set of Resource entities.
91
     *
92
     * @return null|array
93
     */
94 1
    public function getResources() : ? array
95
    {
96 1
        return $this->getDataEntitySet([]);
97
    }
98
99
    /**
100
     * Returns a Resource entity identified by (unique) ID.
101
     *
102
     * @param int $identifier
103
     * @return null|ResourceEntity
104
     */
105 1
    public function getResourceById(int $identifier) : ? ResourceEntity
106
    {
107
        /** @var null|ResourceEntity $dataEntity */
108 1
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
109
110 1
        return $dataEntity;
111
    }
112
113
    /**
114
     * Returns an Resource entity by name.
115
     *
116
     * @param string $name
117
     * @return null|ResourceEntity
118
     */
119 1
    public function getResourceByName(string $name) : ? ResourceEntity
120
    {
121
        /** @var null|ResourceEntity $dataEntity */
122 1
        $dataEntity = $this->getDataEntity([$this->name => $name]);
123
124 1
        return $dataEntity;
125
    }
126
}
127