Passed
Push — master ( 5bf459...a14261 )
by Gabor
27:12
created

ApplicationStorage::getApplicationById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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;
15
16
use WebHemi\DateTime;
17
use WebHemi\Data\EntityInterface;
18
use WebHemi\Data\Entity\ApplicationEntity;
19
20
/**
21
 * Class ApplicationStorage.
22
 */
23
class ApplicationStorage extends AbstractStorage
24
{
25
    /** @var string */
26
    protected $dataGroup = 'webhemi_application';
27
    /** @var string */
28
    protected $idKey = 'id_application';
29
    /** @var string */
30
    private $name = 'name';
31
    /** @var string */
32
    private $title = 'title';
33
    /** @var string */
34
    private $description = 'description';
35
    /** @var string */
36
    private $isReadOnly = 'is_read_only';
37
    /** @var string */
38
    private $isEnabled = 'is_enabled';
39
    /** @var string */
40
    private $dateCreated = 'date_created';
41
    /** @var string */
42
    private $dateModified = 'date_modified';
43
44
    /**
45
     * Populates an entity with storage data.
46
     *
47
     * @param EntityInterface $dataEntity
48
     * @param array           $data
49
     * @return void
50
     */
51 3
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
52
    {
53
        /* @var ApplicationEntity $dataEntity */
54 3
        $dataEntity->setApplicationId((int) $data[$this->idKey])
55 3
            ->setName($data[$this->name])
56 3
            ->setTitle($data[$this->title])
57 3
            ->setDescription($data[$this->description])
58 3
            ->setReadOnly((bool) $data[$this->isReadOnly])
59 3
            ->setEnabled((bool) $data[$this->isEnabled])
60 3
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
61 3
            ->setDateModified(new DateTime($data[$this->dateModified] ?? 'now'));
62 3
    }
63
64
    /**
65
     * Get data from an entity.
66
     *
67
     * @param EntityInterface $dataEntity
68
     * @return array
69
     */
70 3
    protected function getEntityData(EntityInterface $dataEntity) : array
71
    {
72
        /** @var ApplicationEntity $dataEntity */
73 3
        $dateCreated = $dataEntity->getDateCreated();
74 3
        $dateModified = $dataEntity->getDateModified();
75
76
        return [
77 3
            $this->idKey => $dataEntity->getKeyData(),
78 3
            $this->name => $dataEntity->getName(),
79 3
            $this->title => $dataEntity->getTitle(),
80 3
            $this->description => $dataEntity->getDescription(),
81 3
            $this->isReadOnly => (int) $dataEntity->getReadOnly(),
82 3
            $this->isEnabled => (int) $dataEntity->getEnabled(),
83 3
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
84 3
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
85
        ];
86
    }
87
88
    /**
89
     * Returns every Application entity.
90
     *
91
     * @return array|ApplicationEntity[]
92
     */
93 1
    public function getApplications()
94
    {
95
        /** @var ApplicationEntity[] $entityList */
96 1
        $entityList = $this->getDataEntitySet([]);
97
98 1
        return $entityList;
99
    }
100
101
    /**
102
     * Returns a Application entity identified by (unique) ID.
103
     *
104
     * @param int $identifier
105
     * @return null|ApplicationEntity
106
     */
107 1
    public function getApplicationById($identifier) : ? ApplicationEntity
108
    {
109
        /** @var null|ApplicationEntity $dataEntity */
110 1
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
111
112 1
        return $dataEntity;
113
    }
114
115
    /**
116
     * Returns an Application entity by name.
117
     *
118
     * @param string $name
119
     * @return null|ApplicationEntity
120
     */
121 1
    public function getApplicationByName($name) : ? ApplicationEntity
122
    {
123
        /** @var null|ApplicationEntity $dataEntity */
124 1
        $dataEntity = $this->getDataEntity([$this->name => $name]);
125
126 1
        return $dataEntity;
127
    }
128
}
129