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

ApplicationStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 100
loc 100
rs 10
ccs 36
cts 36
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A populateEntity() 11 11 1
A getEntityData() 16 16 3
A getApplicationById() 12 12 2
A getApplicationByName() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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