Passed
Push — master ( 22a454...7e0f83 )
by Gabor
02:59
created

ApplicationStorage::getApplicationByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
ccs 8
cts 8
cp 1
cc 2
eloc 7
nc 2
nop 1
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;
13
14
use WebHemi\DateTime;
15
use WebHemi\Data\Entity\DataEntityInterface;
16
use WebHemi\Data\Entity\ApplicationEntity;
17
18
/**
19
 * Class ApplicationStorage.
20
 */
21 View Code Duplication
class ApplicationStorage extends AbstractDataStorage
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23
    /** @var string */
24
    protected $dataGroup = 'webhemi_application';
25
    /** @var string */
26
    protected $idKey = 'id_application';
27
    /** @var string */
28
    private $name = 'name';
29
    /** @var string */
30
    private $title = 'title';
31
    /** @var string */
32
    private $description = 'description';
33
    /** @var string */
34
    private $isReadOnly = 'is_read_only';
35
    /** @var string */
36
    private $dateCreated = 'date_created';
37
    /** @var string */
38
    private $dateModified = 'date_modified';
39
40
    /**
41
     * Populates an entity with storage data.
42
     *
43
     * @param DataEntityInterface $entity
44
     * @param array               $data
45
     */
46 2
    protected function populateEntity(DataEntityInterface &$entity, array $data)
47
    {
48
        /* @var ApplicationEntity $entity */
49 2
        $entity->setApplicationId($data[$this->idKey])
50 2
            ->setName($data[$this->name])
51 2
            ->setTitle($data[$this->title])
52 2
            ->setDescription($data[$this->description])
53 2
            ->setReadOnly($data[$this->isReadOnly])
54 2
            ->setDateCreated(new DateTime($data[$this->dateCreated]))
55 2
            ->setDateModified(new DateTime($data[$this->dateModified]));
56 2
    }
57
58
    /**
59
     * Get data from an entity.
60
     *
61
     * @param DataEntityInterface $entity
62
     * @return array
63
     */
64 2
    protected function getEntityData(DataEntityInterface $entity)
65
    {
66
        /** @var ApplicationEntity $entity */
67 2
        $dateCreated = $entity->getDateCreated();
68 2
        $dateModified = $entity->getDateModified();
69
70
        return [
71 2
            $this->idKey => $entity->getKeyData(),
72 2
            $this->name => $entity->getName(),
73 2
            $this->title => $entity->getTitle(),
74 2
            $this->description => $entity->getDescription(),
75 2
            $this->isReadOnly => (int)$entity->getReadOnly(),
76 2
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
77 2
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
78 2
        ];
79
    }
80
81
    /**
82
     * Returns a Application entity identified by (unique) ID.
83
     *
84
     * @param int $identifier
85
     *
86
     * @return bool|ApplicationEntity
87
     */
88 1
    public function getApplicationById($identifier)
89
    {
90 1
        $entity = false;
91 1
        $data = $this->getDataAdapter()->getData($identifier);
92
93 1
        if (!empty($data)) {
94 1
            $entity = $this->createEntity();
95 1
            $this->populateEntity($entity, $data);
96 1
        }
97
98 1
        return $entity;
99
    }
100
101
    /**
102
     * Returns an Application entity by name.
103
     *
104
     * @param string $name
105
     *
106
     * @return bool|ApplicationEntity
107
     */
108 1
    public function getApplicationByName($name)
109
    {
110 1
        $entity = false;
111 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->name => $name], 1);
112
113 1
        if (!empty($dataList)) {
114 1
            $entity = $this->createEntity();
115 1
            $this->populateEntity($entity, $dataList[0]);
116 1
        }
117
118 1
        return $entity;
119
    }
120
}
121