Passed
Branch master (41990d)
by Gabor
03:31
created

PolicyStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 37.59 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 53
loc 141
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A populateEntity() 0 13 1
A getPolicyById() 0 12 2
A getPoliciesByResourceId() 16 16 3
A getPoliciesByApplicationId() 16 16 3
A getPoliciesByResourceAndApplicationIds() 21 21 3

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 DateTime;
15
use WebHemi\Data\Entity\DataEntityInterface;
16
use WebHemi\Data\Entity\AccessManagement\PolicyEntity;
17
use WebHemi\Data\Storage\AbstractDataStorage;
18
19
/**
20
 * Class PolicyStorage.
21
 */
22
class PolicyStorage extends AbstractDataStorage
23
{
24
    /** @var string */
25
    protected $dataGroup = 'webhemi_am_policy';
26
    /** @var string */
27
    protected $idKey = 'id_am_policy';
28
    /** @var string */
29
    private $resourceId = 'fk_am_resource';
30
    /** @var string */
31
    private $applicationId = 'fk_application';
32
    /** @var string */
33
    private $title = 'title';
34
    /** @var string */
35
    private $description = 'description';
36
    /** @var string */
37
    private $isReadOnly = 'is_read_only';
38
    /** @var string */
39
    private $isAllowed = 'is_allowed';
40
    /** @var string */
41
    private $dateCreated = 'date_created';
42
    /** @var string */
43
    private $dateModified = 'date_modified';
44
45
    /**
46
     * Populates an entity with storage data.
47
     *
48
     * @param DataEntityInterface $entity
49
     * @param array               $data
50
     */
51 4
    protected function populateEntity(DataEntityInterface &$entity, array $data)
52
    {
53
        /* @var PolicyEntity $entity */
54 4
        $entity->setPolicyId($data[$this->idKey])
55 4
            ->setResourceId($data[$this->resourceId])
56 4
            ->setApplicationId($data[$this->applicationId])
57 4
            ->setTitle($data[$this->title])
58 4
            ->setDescription($data[$this->description])
59 4
            ->setReadOnly($data[$this->isReadOnly])
60 4
            ->setAllowed($data[$this->isAllowed])
61 4
            ->setDateCreated(new DateTime($data[$this->dateCreated]))
62 4
            ->setDateModified(new DateTime($data[$this->dateModified]));
63 4
    }
64
65
    /**
66
     * Returns a Policy entity identified by (unique) ID.
67
     *
68
     * @param int $identifier
69
     *
70
     * @return bool|PolicyEntity
71
     */
72 1
    public function getPolicyById($identifier)
73
    {
74 1
        $entity = false;
75 1
        $data = $this->getDataAdapter()->getData($identifier);
76
77 1
        if (!empty($data)) {
78 1
            $entity = $this->createEntity();
79 1
            $this->populateEntity($entity, $data);
80 1
        }
81
82 1
        return $entity;
83
    }
84
85
    /**
86
     * Returns a set of Policy entities identified by Resource ID.
87
     *
88
     * @param int $resourceId
89
     *
90
     * @return bool|array<PolicyEntity>
91
     */
92 1 View Code Duplication
    public function getPoliciesByResourceId($resourceId)
0 ignored issues
show
Duplication introduced by
This method 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...
93
    {
94 1
        $entityList = false;
95 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->resourceId => $resourceId]);
96
97 1
        if (!empty($dataList)) {
98 1
            foreach ($dataList as $policy) {
99
                /** @var PolicyEntity $entity */
100 1
                $entity = $this->createEntity();
101 1
                $this->populateEntity($entity, $policy);
102 1
                $entityList[] = $entity;
103 1
            }
104 1
        }
105
106 1
        return $entityList;
107
    }
108
109
    /**
110
     * Returns a set of Policy entities identified by Application ID.
111
     *
112
     * @param int $applicationId
113
     *
114
     * @return bool|array<PolicyEntity>
115
     */
116 1 View Code Duplication
    public function getPoliciesByApplicationId($applicationId)
0 ignored issues
show
Duplication introduced by
This method 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...
117
    {
118 1
        $entityList = false;
119 1
        $dataList = $this->getDataAdapter()->getDataSet([$this->applicationId => $applicationId]);
120
121 1
        if (!empty($dataList)) {
122 1
            foreach ($dataList as $policy) {
123
                /** @var PolicyEntity $entity */
124 1
                $entity = $this->createEntity();
125 1
                $this->populateEntity($entity, $policy);
126 1
                $entityList[] = $entity;
127 1
            }
128 1
        }
129
130 1
        return $entityList;
131
    }
132
133
    /**
134
     * Returns a set of Policy entities identified by both Resource and Application IDs.
135
     *
136
     * @param int $resourceId
137
     * @param int $applicationId
138
     *
139
     * @return bool|array<PolicyEntity>
140
     */
141 1 View Code Duplication
    public function getPoliciesByResourceAndApplicationIds($resourceId, $applicationId)
0 ignored issues
show
Duplication introduced by
This method 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...
142
    {
143 1
        $entityList = false;
144 1
        $dataList = $this->getDataAdapter()->getDataSet(
145
            [
146 1
                $this->resourceId => $resourceId,
147 1
                $this->applicationId => $applicationId
148 1
            ]
149 1
        );
150
151 1
        if (!empty($dataList)) {
152 1
            foreach ($dataList as $policy) {
153
                /** @var PolicyEntity $entity */
154 1
                $entity = $this->createEntity();
155 1
                $this->populateEntity($entity, $policy);
156 1
                $entityList[] = $entity;
157 1
            }
158 1
        }
159
160 1
        return $entityList;
161
    }
162
}
163