Completed
Push — test-EZP-26707-issearchable-fu... ( 0d4c60 )
by
unknown
30:03 queued 14:08
created

ExceptionConversion   C

Complexity

Total Complexity 61

Size/Duplication

Total Lines 386
Duplicated Lines 7.77 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 30
loc 386
rs 6.018
c 0
b 0
f 0
wmc 61
lcom 1
cbo 1

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadLocationEntries() 0 10 3
A isRootEntry() 0 10 3
A cleanupAfterPublish() 10 10 3
A historizeBeforeSwap() 0 10 3
A historizeId() 0 10 3
A reparent() 0 10 3
A updateRow() 10 10 3
A insertRow() 0 10 3
A loadRow() 0 10 3
A loadAutogeneratedEntry() 0 10 3
A remove() 10 10 3
A listGlobalEntries() 0 10 3
A removeCustomAlias() 0 10 3
A loadUrlAliasData() 0 10 3
A loadPathData() 0 10 3
A loadPathDataByHierarchy() 0 10 3
A loadAutogeneratedEntries() 0 10 3
A getNextId() 0 10 3
A setTable() 0 10 3
A getLocationContentMainLanguageId() 0 10 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ExceptionConversion often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ExceptionConversion, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * File containing the Section Gateway 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\UrlAlias\Gateway;
12
13
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway;
14
use Doctrine\DBAL\DBALException;
15
use PDOException;
16
17
/**
18
 * UrlAlias Handler.
19
 */
20
class ExceptionConversion extends Gateway
21
{
22
    /**
23
     * The wrapped gateway.
24
     *
25
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway
26
     */
27
    protected $innerGateway;
28
29
    /**
30
     * Creates a new exception conversion gateway around $innerGateway.
31
     *
32
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $innerGateway
33
     */
34
    public function __construct(Gateway $innerGateway)
35
    {
36
        $this->innerGateway = $innerGateway;
37
    }
38
39
    public function setTable($name)
40
    {
41
        try {
42
            return $this->innerGateway->setTable($name);
43
        } catch (DBALException $e) {
44
            throw new \RuntimeException('Database error', 0, $e);
45
        } catch (PDOException $e) {
46
            throw new \RuntimeException('Database error', 0, $e);
47
        }
48
    }
49
50
    /**
51
     * Loads list of aliases by given $locationId.
52
     *
53
     * @param mixed $locationId
54
     * @param bool $custom
55
     * @param mixed $languageId
56
     *
57
     * @return array
58
     */
59
    public function loadLocationEntries($locationId, $custom = false, $languageId = false)
60
    {
61
        try {
62
            return $this->innerGateway->loadLocationEntries($locationId, $custom);
63
        } catch (DBALException $e) {
64
            throw new \RuntimeException('Database error', 0, $e);
65
        } catch (PDOException $e) {
66
            throw new \RuntimeException('Database error', 0, $e);
67
        }
68
    }
69
70
    /**
71
     * Returns boolean indicating if the row with given $id is special root entry.
72
     *
73
     * Special root entry entry will have parentId=0 and text=''.
74
     * In standard installation this entry will point to location with id=2.
75
     *
76
     * @param mixed $id
77
     *
78
     * @return bool
79
     */
80
    public function isRootEntry($id)
81
    {
82
        try {
83
            return $this->innerGateway->isRootEntry($id);
84
        } catch (DBALException $e) {
85
            throw new \RuntimeException('Database error', 0, $e);
86
        } catch (PDOException $e) {
87
            throw new \RuntimeException('Database error', 0, $e);
88
        }
89
    }
90
91
    /**
92
     * Downgrades autogenerated entry matched by given $action and $languageId and negatively matched by
93
     * composite primary key.
94
     *
95
     * If language mask of the found entry is composite (meaning it consists of multiple language ids) given
96
     * $languageId will be removed from mask. Otherwise entry will be marked as history.
97
     *
98
     * @param string $action
99
     * @param mixed $languageId
100
     * @param mixed $newId
101
     * @param mixed $parentId
102
     * @param string $textMD5
103
     */
104 View Code Duplication
    public function cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5)
105
    {
106
        try {
107
            $this->innerGateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5);
108
        } catch (DBALException $e) {
109
            throw new \RuntimeException('Database error', 0, $e);
110
        } catch (PDOException $e) {
111
            throw new \RuntimeException('Database error', 0, $e);
112
        }
113
    }
114
115
    public function historizeBeforeSwap($action, $languageMask)
116
    {
117
        try {
118
            $this->innerGateway->historizeBeforeSwap($action, $languageMask);
119
        } catch (DBALException $e) {
120
            throw new \RuntimeException('Database error', 0, $e);
121
        } catch (PDOException $e) {
122
            throw new \RuntimeException('Database error', 0, $e);
123
        }
124
    }
125
126
    /**
127
     * Marks all entries with given $id as history entries.
128
     *
129
     * This method is used by Handler::locationMoved(). Each row is separately historized
130
     * because future publishing needs to be able to take over history entries safely.
131
     *
132
     * @param mixed $id
133
     * @param mixed $link
134
     */
135
    public function historizeId($id, $link)
136
    {
137
        try {
138
            $this->innerGateway->historizeId($id, $link);
139
        } catch (DBALException $e) {
140
            throw new \RuntimeException('Database error', 0, $e);
141
        } catch (PDOException $e) {
142
            throw new \RuntimeException('Database error', 0, $e);
143
        }
144
    }
145
146
    /**
147
     * Updates parent id of autogenerated entries.
148
     *
149
     * Update includes history entries.
150
     *
151
     * @param mixed $oldParentId
152
     * @param mixed $newParentId
153
     */
154
    public function reparent($oldParentId, $newParentId)
155
    {
156
        try {
157
            $this->innerGateway->reparent($oldParentId, $newParentId);
158
        } catch (DBALException $e) {
159
            throw new \RuntimeException('Database error', 0, $e);
160
        } catch (PDOException $e) {
161
            throw new \RuntimeException('Database error', 0, $e);
162
        }
163
    }
164
165
    /**
166
     * Updates single row data matched by composite primary key.
167
     *
168
     * Use optional parameter $languageMaskMatch to additionally limit the query match with languages
169
     *
170
     * @param mixed $parentId
171
     * @param string $textMD5
172
     * @param array $values associative array with column names as keys and column values as values
173
     */
174 View Code Duplication
    public function updateRow($parentId, $textMD5, array $values)
175
    {
176
        try {
177
            $this->innerGateway->updateRow($parentId, $textMD5, $values);
178
        } catch (DBALException $e) {
179
            throw new \RuntimeException('Database error', 0, $e);
180
        } catch (PDOException $e) {
181
            throw new \RuntimeException('Database error', 0, $e);
182
        }
183
    }
184
185
    /**
186
     * Inserts new row in urlalias_ml table.
187
     *
188
     * @param array $values
189
     *
190
     * @return mixed
191
     */
192
    public function insertRow(array $values)
193
    {
194
        try {
195
            return $this->innerGateway->insertRow($values);
196
        } catch (DBALException $e) {
197
            throw new \RuntimeException('Database error', 0, $e);
198
        } catch (PDOException $e) {
199
            throw new \RuntimeException('Database error', 0, $e);
200
        }
201
    }
202
203
    /**
204
     * Loads single row data matched by composite primary key.
205
     *
206
     * @param mixed $parentId
207
     * @param string $textMD5
208
     *
209
     * @return array
210
     */
211
    public function loadRow($parentId, $textMD5)
212
    {
213
        try {
214
            return $this->innerGateway->loadRow($parentId, $textMD5);
215
        } catch (DBALException $e) {
216
            throw new \RuntimeException('Database error', 0, $e);
217
        } catch (PDOException $e) {
218
            throw new \RuntimeException('Database error', 0, $e);
219
        }
220
    }
221
222
    /**
223
     * Loads autogenerated entry id by given $action and optionally $parentId.
224
     *
225
     * @param string $action
226
     * @param mixed|null $parentId
227
     *
228
     * @return array
229
     */
230
    public function loadAutogeneratedEntry($action, $parentId = null)
231
    {
232
        try {
233
            return $this->innerGateway->loadAutogeneratedEntry($action, $parentId);
234
        } catch (DBALException $e) {
235
            throw new \RuntimeException('Database error', 0, $e);
236
        } catch (PDOException $e) {
237
            throw new \RuntimeException('Database error', 0, $e);
238
        }
239
    }
240
241
    /**
242
     * Deletes all rows with given $action and optionally $id.
243
     *
244
     * If $id is set only autogenerated entries will be removed.
245
     *
246
     * @param string $action
247
     * @param mixed|null $id
248
     */
249 View Code Duplication
    public function remove($action, $id = null)
250
    {
251
        try {
252
            $this->innerGateway->remove($action, $id);
253
        } catch (DBALException $e) {
254
            throw new \RuntimeException('Database error', 0, $e);
255
        } catch (PDOException $e) {
256
            throw new \RuntimeException('Database error', 0, $e);
257
        }
258
    }
259
260
    /**
261
     * Loads paged list of global aliases.
262
     *
263
     * @param string|null $languageCode
264
     * @param int $offset
265
     * @param int $limit
266
     *
267
     * @return array
268
     */
269
    public function listGlobalEntries($languageCode = null, $offset = 0, $limit = -1)
270
    {
271
        try {
272
            return $this->innerGateway->listGlobalEntries($languageCode, $offset, $limit);
273
        } catch (DBALException $e) {
274
            throw new \RuntimeException('Database error', 0, $e);
275
        } catch (PDOException $e) {
276
            throw new \RuntimeException('Database error', 0, $e);
277
        }
278
    }
279
280
    /**
281
     * Deletes single custom alias row matched by composite primary key.
282
     *
283
     * If $id is set only autogenerated entries will be removed.
284
     *
285
     * @param mixed $parentId
286
     * @param string $textMD5
287
     *
288
     * @return bool
289
     */
290
    public function removeCustomAlias($parentId, $textMD5)
291
    {
292
        try {
293
            return $this->innerGateway->removeCustomAlias($parentId, $textMD5);
294
        } catch (DBALException $e) {
295
            throw new \RuntimeException('Database error', 0, $e);
296
        } catch (PDOException $e) {
297
            throw new \RuntimeException('Database error', 0, $e);
298
        }
299
    }
300
301
    /**
302
     * Loads complete URL alias data by given array of path hashes.
303
     *
304
     * @param string[] $urlHashes URL string hashes
305
     *
306
     * @return array
307
     */
308
    public function loadUrlAliasData(array $urlHashes)
309
    {
310
        try {
311
            return $this->innerGateway->loadUrlAliasData($urlHashes);
312
        } catch (DBALException $e) {
313
            throw new \RuntimeException('Database error', 0, $e);
314
        } catch (PDOException $e) {
315
            throw new \RuntimeException('Database error', 0, $e);
316
        }
317
    }
318
319
    /**
320
     * Loads all data for the path identified by given $id.
321
     *
322
     * @param mixed $id
323
     *
324
     * @return array
325
     */
326
    public function loadPathData($id)
327
    {
328
        try {
329
            return $this->innerGateway->loadPathData($id);
330
        } catch (DBALException $e) {
331
            throw new \RuntimeException('Database error', 0, $e);
332
        } catch (PDOException $e) {
333
            throw new \RuntimeException('Database error', 0, $e);
334
        }
335
    }
336
337
    /**
338
     * Loads path data identified by given ordered array of hierarchy data.
339
     *
340
     * The first entry in $hierarchyData corresponds to the top-most path element in the path, the second entry the
341
     * child of the first path element and so on.
342
     * This method is faster than self::getPath() since it can fetch all elements using only one query, but can be used
343
     * only for autogenerated paths.
344
     *
345
     * @param array $hierarchyData
346
     *
347
     * @return array
348
     */
349
    public function loadPathDataByHierarchy(array $hierarchyData)
350
    {
351
        try {
352
            return $this->innerGateway->loadPathDataByHierarchy($hierarchyData);
353
        } catch (DBALException $e) {
354
            throw new \RuntimeException('Database error', 0, $e);
355
        } catch (PDOException $e) {
356
            throw new \RuntimeException('Database error', 0, $e);
357
        }
358
    }
359
360
    /**
361
     * Loads all autogenerated entries with given $parentId with optionally included history entries.
362
     *
363
     * @param mixed $parentId
364
     * @param bool $includeHistory
365
     *
366
     * @return array
367
     */
368
    public function loadAutogeneratedEntries($parentId, $includeHistory = false)
369
    {
370
        try {
371
            return $this->innerGateway->loadAutogeneratedEntries($parentId, $includeHistory);
372
        } catch (DBALException $e) {
373
            throw new \RuntimeException('Database error', 0, $e);
374
        } catch (PDOException $e) {
375
            throw new \RuntimeException('Database error', 0, $e);
376
        }
377
    }
378
379
    /**
380
     * Returns next value for "id" column.
381
     *
382
     * @return mixed
383
     */
384
    public function getNextId()
385
    {
386
        try {
387
            return $this->innerGateway->getNextId();
388
        } catch (DBALException $e) {
389
            throw new \RuntimeException('Database error', 0, $e);
390
        } catch (PDOException $e) {
391
            throw new \RuntimeException('Database error', 0, $e);
392
        }
393
    }
394
395
    public function getLocationContentMainLanguageId($locationId)
396
    {
397
        try {
398
            return $this->innerGateway->getLocationContentMainLanguageId($locationId);
399
        } catch (DBALException $e) {
400
            throw new \RuntimeException('Database error', 0, $e);
401
        } catch (PDOException $e) {
402
            throw new \RuntimeException('Database error', 0, $e);
403
        }
404
    }
405
}
406