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

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