Completed
Push — ezp-23733_disable_section_dele... ( a742c6...6e64a4 )
by
unknown
35:02
created

Handler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 215
Duplicated Lines 19.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 42
loc 215
wmc 17
lcom 1
cbo 3
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 11 11 1
A update() 11 11 1
A load() 10 10 2
A loadAll() 0 6 1
A loadByIdentifier() 10 10 2
A createSectionFromArray() 0 10 1
A createSectionsFromArray() 0 9 2
A delete() 0 11 2
A assign() 0 4 1
A assignmentsCount() 0 4 1
A policiesCount() 0 4 1
A countRoleAssignmentsUsingSection() 0 4 1

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
/**
4
 * File containing the Section Handler class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Legacy\Content\Section;
12
13
use eZ\Publish\SPI\Persistence\Content\Section\Handler as BaseSectionHandler;
14
use eZ\Publish\SPI\Persistence\Content\Section;
15
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
16
use RuntimeException;
17
18
/**
19
 * Section Handler.
20
 */
21
class Handler implements BaseSectionHandler
22
{
23
    /**
24
     * Section Gateway.
25
     *
26
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Section\Gateway
27
     */
28
    protected $sectionGateway;
29
30
    /**
31
     * Creates a new Section Handler.
32
     *
33
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Section\Gateway $sectionGateway
34
     */
35
    public function __construct(Gateway $sectionGateway)
36
    {
37
        $this->sectionGateway = $sectionGateway;
38
    }
39
40
    /**
41
     * Create a new section.
42
     *
43
     * @param string $name
44
     * @param string $identifier
45
     *
46
     * @return \eZ\Publish\SPI\Persistence\Content\Section
47
     */
48 View Code Duplication
    public function create($name, $identifier)
49
    {
50
        $section = new Section();
51
52
        $section->name = $name;
53
        $section->identifier = $identifier;
54
55
        $section->id = $this->sectionGateway->insertSection($name, $identifier);
56
57
        return $section;
58
    }
59
60
    /**
61
     * Update name and identifier of a section.
62
     *
63
     * @param mixed $id
64
     * @param string $name
65
     * @param string $identifier
66
     *
67
     * @return \eZ\Publish\SPI\Persistence\Content\Section
68
     */
69 View Code Duplication
    public function update($id, $name, $identifier)
70
    {
71
        $this->sectionGateway->updateSection($id, $name, $identifier);
72
73
        $section = new Section();
74
        $section->id = $id;
75
        $section->name = $name;
76
        $section->identifier = $identifier;
77
78
        return $section;
79
    }
80
81
    /**
82
     * Get section data.
83
     *
84
     * @param mixed $id
85
     *
86
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If section is not found
87
     *
88
     * @return \eZ\Publish\SPI\Persistence\Content\Section
89
     */
90 View Code Duplication
    public function load($id)
91
    {
92
        $rows = $this->sectionGateway->loadSectionData($id);
93
94
        if (empty($rows)) {
95
            throw new NotFound('Section', $id);
96
        }
97
98
        return $this->createSectionFromArray(reset($rows));
0 ignored issues
show
Security Bug introduced by
It seems like reset($rows) targeting reset() can also be of type false; however, eZ\Publish\Core\Persiste...reateSectionFromArray() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
99
    }
100
101
    /**
102
     * Get all section data.
103
     *
104
     * @return \eZ\Publish\SPI\Persistence\Content\Section[]
105
     */
106
    public function loadAll()
107
    {
108
        $rows = $this->sectionGateway->loadAllSectionData();
109
110
        return $this->createSectionsFromArray($rows);
111
    }
112
113
    /**
114
     * Get section data by identifier.
115
     *
116
     * @param string $identifier
117
     *
118
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If section is not found
119
     *
120
     * @return \eZ\Publish\SPI\Persistence\Content\Section
121
     */
122 View Code Duplication
    public function loadByIdentifier($identifier)
123
    {
124
        $rows = $this->sectionGateway->loadSectionDataByIdentifier($identifier);
125
126
        if (empty($rows)) {
127
            throw new NotFound('Section', $identifier);
128
        }
129
130
        return $this->createSectionFromArray(reset($rows));
0 ignored issues
show
Security Bug introduced by
It seems like reset($rows) targeting reset() can also be of type false; however, eZ\Publish\Core\Persiste...reateSectionFromArray() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
131
    }
132
133
    /**
134
     * Creates a Section from the given $data.
135
     *
136
     * @param array $data
137
     *
138
     * @return \eZ\Publish\SPI\Persistence\Content\Section
139
     */
140
    protected function createSectionFromArray(array $data)
141
    {
142
        $section = new Section();
143
144
        $section->id = (int)$data['id'];
145
        $section->name = $data['name'];
146
        $section->identifier = $data['identifier'];
147
148
        return $section;
149
    }
150
151
    /**
152
     * Creates a Section from the given $data.
153
     *
154
     * @param array $data
155
     *
156
     * @return \eZ\Publish\SPI\Persistence\Content\Section[]
157
     */
158
    protected function createSectionsFromArray(array $data)
159
    {
160
        $sections = array();
161
        foreach ($data as $sectionData) {
162
            $sections[] = $this->createSectionFromArray($sectionData);
163
        }
164
165
        return $sections;
166
    }
167
168
    /**
169
     * Delete a section.
170
     *
171
     * Might throw an exception if the section is still associated with some
172
     * content objects. Make sure that no content objects are associated with
173
     * the section any more *before* calling this method.
174
     *
175
     * @param mixed $id
176
     */
177
    public function delete($id)
178
    {
179
        $contentCount = $this->sectionGateway->countContentObjectsInSection($id);
180
181
        if ($contentCount > 0) {
182
            throw new RuntimeException(
183
                "Section with ID '{$id}' still has content assigned."
184
            );
185
        }
186
        $this->sectionGateway->deleteSection($id);
187
    }
188
189
    /**
190
     * Assigns section to single content object.
191
     *
192
     * @param mixed $sectionId
193
     * @param mixed $contentId
194
     */
195
    public function assign($sectionId, $contentId)
196
    {
197
        $this->sectionGateway->assignSectionToContent($sectionId, $contentId);
198
    }
199
200
    /**
201
     * Number of content assignments a Section has.
202
     *
203
     * @param mixed $sectionId
204
     *
205
     * @return int
206
     */
207
    public function assignmentsCount($sectionId)
208
    {
209
        return $this->sectionGateway->countContentObjectsInSection($sectionId);
210
    }
211
212
    /**
213
     * Number of role policies using a Section in limitations.
214
     *
215
     * @param mixed $sectionId
216
     *
217
     * @return int
218
     */
219
    public function policiesCount($sectionId)
220
    {
221
        return $this->sectionGateway->countPoliciesUsingSection($sectionId);
222
    }
223
224
    /**
225
     * Counts the number of role assignments using section with $sectionId in their limitations.
226
     *
227
     * @param int $sectionId
228
     *
229
     * @return int
230
     */
231
    public function countRoleAssignmentsUsingSection($sectionId)
232
    {
233
        return $this->sectionGateway->countRoleAssignmentsUsingSection($sectionId);
234
    }
235
}
236