Passed
Push — master ( bf3918...698edc )
by Gabor
12:58 queued 06:03
created

ApplicationStorage::getEntityData()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 20
cts 20
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 21
nc 4
nop 1
crap 3
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
 * @SuppressWarnings(PHPMD.TooManyFields)
24
 */
25
class ApplicationStorage extends AbstractStorage
26
{
27
    /** @var string */
28
    protected $dataGroup = 'webhemi_application';
29
    /** @var string */
30
    protected $idKey = 'id_application';
31
    /** @var string */
32
    private $name = 'name';
33
    /** @var string */
34
    private $title = 'title';
35
    /** @var string */
36
    private $introduction = 'introduction';
37
    /** @var string */
38
    private $subject = 'subject';
39
    /** @var string */
40
    private $description = 'description';
41
    /** @var string */
42
    private $keywords = 'keywords';
43
    /** @var string */
44
    private $copyright = 'copyright';
45
    /** @var string */
46
    private $path = 'path';
47
    /** @var string */
48
    private $theme = 'theme';
49
    /** @var string */
50
    private $type = 'type';
51
    /** @var string */
52
    private $locale = 'locale';
53
    /** @var string */
54
    private $timeZone = 'timezone';
55
    /** @var string */
56
    private $isReadOnly = 'is_read_only';
57
    /** @var string */
58
    private $isEnabled = 'is_enabled';
59
    /** @var string */
60
    private $dateCreated = 'date_created';
61
    /** @var string */
62
    private $dateModified = 'date_modified';
63
64
    /**
65
     * Populates an entity with storage data.
66
     *
67
     * @param EntityInterface $dataEntity
68
     * @param array           $data
69
     * @return void
70
     */
71 3
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
72
    {
73
        /* @var ApplicationEntity $dataEntity */
74 3
        $dataEntity->setApplicationId((int) $data[$this->idKey])
75 3
            ->setName($data[$this->name])
76 3
            ->setTitle($data[$this->title])
77 3
            ->setIntroduction($data[$this->introduction] ?? null)
78 3
            ->setSubject($data[$this->subject] ?? null)
79 3
            ->setDescription($data[$this->description] ?? null)
80 3
            ->setKeywords($data[$this->keywords] ?? null)
81 3
            ->setCopyright($data[$this->copyright] ?? null)
82 3
            ->setPath($data[$this->path] ?? null)
83 3
            ->setTheme($data[$this->theme] ?? null)
84 3
            ->setType($data[$this->type] ?? null)
85 3
            ->setLocale($data[$this->locale] ?? null)
86 3
            ->setTimeZone($data[$this->timeZone] ?? null)
87 3
            ->setReadOnly((bool) $data[$this->isReadOnly])
88 3
            ->setEnabled((bool) $data[$this->isEnabled])
89 3
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
90 3
            ->setDateModified(!empty($data[$this->dateModified]) ? new DateTime($data[$this->dateModified]) : null);
91 3
    }
92
93
    /**
94
     * Get data from an entity.
95
     *
96
     * @param EntityInterface $dataEntity
97
     * @return array
98
     */
99 3
    protected function getEntityData(EntityInterface $dataEntity) : array
100
    {
101
        /** @var ApplicationEntity $dataEntity */
102 3
        $dateCreated = $dataEntity->getDateCreated();
103 3
        $dateModified = $dataEntity->getDateModified();
104
105
        return [
106 3
            $this->idKey => $dataEntity->getKeyData(),
107 3
            $this->name => $dataEntity->getName(),
108 3
            $this->title => $dataEntity->getTitle(),
109 3
            $this->introduction => $dataEntity->getIntroduction(),
110 3
            $this->subject => $dataEntity->getSubject(),
111 3
            $this->description => $dataEntity->getDescription(),
112 3
            $this->keywords => $dataEntity->getKeywords(),
113 3
            $this->copyright => $dataEntity->getCopyright(),
114 3
            $this->path => $dataEntity->getPath(),
115 3
            $this->theme => $dataEntity->getTheme(),
116 3
            $this->type => $dataEntity->getType(),
117 3
            $this->locale => $dataEntity->getLocale(),
118 3
            $this->timeZone => $dataEntity->getTimeZone(),
119 3
            $this->isReadOnly => (int) $dataEntity->getReadOnly(),
120 3
            $this->isEnabled => (int) $dataEntity->getEnabled(),
121 3
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
122 3
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
123
        ];
124
    }
125
126
    /**
127
     * Returns every Application entity.
128
     *
129
     * @return array|ApplicationEntity[]
130
     */
131 1
    public function getApplications()
132
    {
133
        /** @var ApplicationEntity[] $entityList */
134 1
        $entityList = $this->getDataEntitySet([]);
135
136 1
        return $entityList;
137
    }
138
139
    /**
140
     * Returns a Application entity identified by (unique) ID.
141
     *
142
     * @param int $identifier
143
     * @return null|ApplicationEntity
144
     */
145 1
    public function getApplicationById($identifier) : ? ApplicationEntity
146
    {
147
        /** @var null|ApplicationEntity $dataEntity */
148 1
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
149
150 1
        return $dataEntity;
151
    }
152
153
    /**
154
     * Returns an Application entity by name.
155
     *
156
     * @param string $name
157
     * @return null|ApplicationEntity
158
     */
159 1
    public function getApplicationByName($name) : ? ApplicationEntity
160
    {
161
        /** @var null|ApplicationEntity $dataEntity */
162 1
        $dataEntity = $this->getDataEntity([$this->name => $name]);
163
164 1
        return $dataEntity;
165
    }
166
}
167