Completed
Push — ezp-28439 ( b6528d )
by
unknown
19:13
created

ExceptionConversion::loadTrashByContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
rs 9.4285
1
<?php
2
3
/**
4
 * File containing the Location 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
namespace eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway;
10
11
use eZ\Publish\API\Repository\Values\Content\Query;
12
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
13
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway;
14
use eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct;
15
use eZ\Publish\SPI\Persistence\Content\Location\CreateStruct;
16
use Doctrine\DBAL\DBALException;
17
use PDOException;
18
use RuntimeException;
19
20
/**
21
 * Base class for location gateways.
22
 */
23
class ExceptionConversion extends Gateway
24
{
25
    /**
26
     * The wrapped gateway.
27
     *
28
     * @var Gateway
29
     */
30
    protected $innerGateway;
31
32
    /**
33
     * Creates a new exception conversion gateway around $innerGateway.
34
     *
35
     * @param Gateway $innerGateway
36
     */
37
    public function __construct(Gateway $innerGateway)
38
    {
39
        $this->innerGateway = $innerGateway;
40
    }
41
42
    /**
43
     * Returns an array with basic node data.
44
     *
45
     * We might want to cache this, since this method is used by about every
46
     * method in the location handler.
47
     *
48
     * @todo optimize
49
     *
50
     * @param mixed $nodeId
51
     *
52
     * @return array
53
     */
54
    public function getBasicNodeData($nodeId)
55
    {
56
        try {
57
            return $this->innerGateway->getBasicNodeData($nodeId);
58
        } catch (DBALException $e) {
59
            throw new RuntimeException('Database error', 0, $e);
60
        } catch (PDOException $e) {
61
            throw new RuntimeException('Database error', 0, $e);
62
        }
63
    }
64
65
    /**
66
     * Returns an array with basic node data for the node with $remoteId.
67
     *
68
     * @todo optimize
69
     *
70
     * @param mixed $remoteId
71
     *
72
     * @return array
73
     */
74
    public function getBasicNodeDataByRemoteId($remoteId)
75
    {
76
        try {
77
            return $this->innerGateway->getBasicNodeDataByRemoteId($remoteId);
78
        } catch (DBALException $e) {
79
            throw new RuntimeException('Database error', 0, $e);
80
        } catch (PDOException $e) {
81
            throw new RuntimeException('Database error', 0, $e);
82
        }
83
    }
84
85
    /**
86
     * Returns total count and data for all Locations satisfying the parameters.
87
     *
88
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
89
     * @param int $offset
90
     * @param int|null $limit
91
     * @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause[] $sortClauses
92
     *
93
     * @return mixed[][]
94
     */
95 View Code Duplication
    public function find(Criterion $criterion, $offset = 0, $limit = null, array $sortClauses = null)
96
    {
97
        try {
98
            return $this->innerGateway->find($criterion, $offset, $limit, $sortClauses);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eZ\Publish\Core\Persiste...ontent\Location\Gateway as the method find() does only exist in the following sub-classes of eZ\Publish\Core\Persiste...ontent\Location\Gateway: eZ\Publish\Core\Persiste...way\ExceptionConversion. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
99
        } catch (DBALException $e) {
100
            throw new RuntimeException('Database error', 0, $e);
101
        } catch (PDOException $e) {
102
            throw new RuntimeException('Database error', 0, $e);
103
        }
104
    }
105
106
    /**
107
     * Loads data for all Locations for $contentId, optionally only in the
108
     * subtree starting at $rootLocationId.
109
     *
110
     * @param int $contentId
111
     * @param int $rootLocationId
112
     *
113
     * @return array
114
     */
115
    public function loadLocationDataByContent($contentId, $rootLocationId = null)
116
    {
117
        try {
118
            return $this->innerGateway->loadLocationDataByContent($contentId, $rootLocationId);
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
     * @see \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway::loadParentLocationsDataForDraftContent
128
     */
129
    public function loadParentLocationsDataForDraftContent($contentId)
130
    {
131
        try {
132
            return $this->innerGateway->loadParentLocationsDataForDraftContent($contentId);
133
        } catch (DBALException $e) {
134
            throw new RuntimeException('Database error', 0, $e);
135
        } catch (PDOException $e) {
136
            throw new RuntimeException('Database error', 0, $e);
137
        }
138
    }
139
140
    /**
141
     * Find all content in the given subtree.
142
     *
143
     * @param mixed $sourceId
144
     * @param bool $onlyIds
145
     *
146
     * @return array
147
     */
148
    public function getSubtreeContent($sourceId, $onlyIds = false)
149
    {
150
        try {
151
            return $this->innerGateway->getSubtreeContent($sourceId, $onlyIds);
152
        } catch (DBALException $e) {
153
            throw new RuntimeException('Database error', 0, $e);
154
        } catch (PDOException $e) {
155
            throw new RuntimeException('Database error', 0, $e);
156
        }
157
    }
158
159
    /**
160
     * Returns data for the first level children of the location identified by given $locationId.
161
     *
162
     * @param mixed $locationId
163
     *
164
     * @return array
165
     */
166
    public function getChildren($locationId)
167
    {
168
        try {
169
            return $this->innerGateway->getChildren($locationId);
170
        } catch (DBALException $e) {
171
            throw new RuntimeException('Database error', 0, $e);
172
        } catch (PDOException $e) {
173
            throw new RuntimeException('Database error', 0, $e);
174
        }
175
    }
176
177
    /**
178
     * Update path strings to move nodes in the ezcontentobject_tree table.
179
     *
180
     * This query can likely be optimized to use some more advanced string
181
     * operations, which then depend on the respective database.
182
     *
183
     * @todo optimize
184
     *
185
     * @param array $fromPathString
186
     * @param array $toPathString
187
     */
188
    public function moveSubtreeNodes(array $fromPathString, array $toPathString)
189
    {
190
        try {
191
            return $this->innerGateway->moveSubtreeNodes($fromPathString, $toPathString);
192
        } catch (DBALException $e) {
193
            throw new RuntimeException('Database error', 0, $e);
194
        } catch (PDOException $e) {
195
            throw new RuntimeException('Database error', 0, $e);
196
        }
197
    }
198
199
    /**
200
     * Updated subtree modification time for all nodes on path.
201
     *
202
     * @param string $pathString
203
     * @param int|null $timestamp
204
     */
205
    public function updateSubtreeModificationTime($pathString, $timestamp = null)
206
    {
207
        try {
208
            return $this->innerGateway->updateSubtreeModificationTime($pathString, $timestamp);
209
        } catch (DBALException $e) {
210
            throw new RuntimeException('Database error', 0, $e);
211
        } catch (PDOException $e) {
212
            throw new RuntimeException('Database error', 0, $e);
213
        }
214
    }
215
216
    /**
217
     * Update node assignment table.
218
     *
219
     * @param int $contentObjectId
220
     * @param int $oldParent
221
     * @param int $newParent
222
     * @param int $opcode
223
     */
224
    public function updateNodeAssignment($contentObjectId, $oldParent, $newParent, $opcode)
225
    {
226
        try {
227
            return $this->innerGateway->updateNodeAssignment($contentObjectId, $oldParent, $newParent, $opcode);
228
        } catch (DBALException $e) {
229
            throw new RuntimeException('Database error', 0, $e);
230
        } catch (PDOException $e) {
231
            throw new RuntimeException('Database error', 0, $e);
232
        }
233
    }
234
235
    /**
236
     * Create locations from node assignments.
237
     *
238
     * Convert existing node assignments into real locations.
239
     *
240
     * @param mixed $contentId
241
     * @param mixed $versionNo
242
     */
243
    public function createLocationsFromNodeAssignments($contentId, $versionNo)
244
    {
245
        try {
246
            return $this->innerGateway->createLocationsFromNodeAssignments($contentId, $versionNo);
247
        } catch (DBALException $e) {
248
            throw new RuntimeException('Database error', 0, $e);
249
        } catch (PDOException $e) {
250
            throw new RuntimeException('Database error', 0, $e);
251
        }
252
    }
253
254
    /**
255
     * Updates all Locations of content identified with $contentId with $versionNo.
256
     *
257
     * @param mixed $contentId
258
     * @param mixed $versionNo
259
     */
260
    public function updateLocationsContentVersionNo($contentId, $versionNo)
261
    {
262
        try {
263
            return $this->innerGateway->updateLocationsContentVersionNo($contentId, $versionNo);
264
        } catch (DBALException $e) {
265
            throw new RuntimeException('Database error', 0, $e);
266
        } catch (PDOException $e) {
267
            throw new RuntimeException('Database error', 0, $e);
268
        }
269
    }
270
271
    /**
272
     * Sets a location to be hidden, and it self + all children to invisible.
273
     *
274
     * @param string $pathString
275
     */
276
    public function hideSubtree($pathString)
277
    {
278
        try {
279
            return $this->innerGateway->hideSubtree($pathString);
280
        } catch (DBALException $e) {
281
            throw new RuntimeException('Database error', 0, $e);
282
        } catch (PDOException $e) {
283
            throw new RuntimeException('Database error', 0, $e);
284
        }
285
    }
286
287
    /**
288
     * Sets a location to be unhidden, and self + children to visible unless a parent is hiding the tree.
289
     * If not make sure only children down to first hidden node is marked visible.
290
     *
291
     * @param string $pathString
292
     */
293
    public function unHideSubtree($pathString)
294
    {
295
        try {
296
            return $this->innerGateway->unHideSubtree($pathString);
297
        } catch (DBALException $e) {
298
            throw new RuntimeException('Database error', 0, $e);
299
        } catch (PDOException $e) {
300
            throw new RuntimeException('Database error', 0, $e);
301
        }
302
    }
303
304
    /**
305
     * Swaps the content object being pointed to by a location object.
306
     *
307
     * Make the location identified by $locationId1 refer to the Content
308
     * referred to by $locationId2 and vice versa.
309
     *
310
     * @param mixed $locationId1
311
     * @param mixed $locationId2
312
     *
313
     * @return bool
314
     */
315
    public function swap($locationId1, $locationId2)
316
    {
317
        try {
318
            return $this->innerGateway->swap($locationId1, $locationId2);
319
        } catch (DBALException $e) {
320
            throw new RuntimeException('Database error', 0, $e);
321
        } catch (PDOException $e) {
322
            throw new RuntimeException('Database error', 0, $e);
323
        }
324
    }
325
326
    /**
327
     * Creates a new location in given $parentNode.
328
     *
329
     * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct
330
     * @param array $parentNode
331
     *
332
     * @return \eZ\Publish\SPI\Persistence\Content\Location
333
     */
334
    public function create(CreateStruct $createStruct, array $parentNode)
335
    {
336
        try {
337
            return $this->innerGateway->create($createStruct, $parentNode);
338
        } catch (DBALException $e) {
339
            throw new RuntimeException('Database error', 0, $e);
340
        } catch (PDOException $e) {
341
            throw new RuntimeException('Database error', 0, $e);
342
        }
343
    }
344
345
    /**
346
     * Create an entry in the node assignment table.
347
     *
348
     * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct
349
     * @param mixed $parentNodeId
350
     * @param int $type
351
     */
352
    public function createNodeAssignment(CreateStruct $createStruct, $parentNodeId, $type = self::NODE_ASSIGNMENT_OP_CODE_CREATE_NOP)
353
    {
354
        try {
355
            return $this->innerGateway->createNodeAssignment($createStruct, $parentNodeId, $type);
356
        } catch (DBALException $e) {
357
            throw new RuntimeException('Database error', 0, $e);
358
        } catch (PDOException $e) {
359
            throw new RuntimeException('Database error', 0, $e);
360
        }
361
    }
362
363
    /**
364
     * Deletes node assignment for given $contentId and $versionNo.
365
     *
366
     * @param int $contentId
367
     * @param int $versionNo
368
     */
369
    public function deleteNodeAssignment($contentId, $versionNo = null)
370
    {
371
        try {
372
            return $this->innerGateway->deleteNodeAssignment($contentId, $versionNo);
373
        } catch (DBALException $e) {
374
            throw new RuntimeException('Database error', 0, $e);
375
        } catch (PDOException $e) {
376
            throw new RuntimeException('Database error', 0, $e);
377
        }
378
    }
379
380
    /**
381
     * Updates an existing location.
382
     *
383
     * Will not throw anything if location id is invalid or no entries are affected.
384
     *
385
     * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location
386
     * @param int $locationId
387
     */
388
    public function update(UpdateStruct $location, $locationId)
389
    {
390
        try {
391
            return $this->innerGateway->update($location, $locationId);
392
        } catch (DBALException $e) {
393
            throw new RuntimeException('Database error', 0, $e);
394
        } catch (PDOException $e) {
395
            throw new RuntimeException('Database error', 0, $e);
396
        }
397
    }
398
399
    /**
400
     * Updates path identification string for given $locationId.
401
     *
402
     * @param mixed $locationId
403
     * @param mixed $parentLocationId
404
     * @param string $text
405
     */
406
    public function updatePathIdentificationString($locationId, $parentLocationId, $text)
407
    {
408
        try {
409
            return $this->innerGateway->updatePathIdentificationString($locationId, $parentLocationId, $text);
410
        } catch (DBALException $e) {
411
            throw new RuntimeException('Database error', 0, $e);
412
        } catch (PDOException $e) {
413
            throw new RuntimeException('Database error', 0, $e);
414
        }
415
    }
416
417
    /**
418
     * Deletes ezcontentobject_tree row for given $locationId (node_id).
419
     *
420
     * @param mixed $locationId
421
     */
422
    public function removeLocation($locationId)
423
    {
424
        try {
425
            return $this->innerGateway->removeLocation($locationId);
426
        } catch (DBALException $e) {
427
            throw new RuntimeException('Database error', 0, $e);
428
        } catch (PDOException $e) {
429
            throw new RuntimeException('Database error', 0, $e);
430
        }
431
    }
432
433
    /**
434
     * Returns id of the next in line node to be set as a new main node.
435
     *
436
     * This returns lowest node id for content identified by $contentId, and not of
437
     * the node identified by given $locationId (current main node).
438
     * Assumes that content has more than one location.
439
     *
440
     * @param mixed $contentId
441
     * @param mixed $locationId
442
     *
443
     * @return array
444
     */
445
    public function getFallbackMainNodeData($contentId, $locationId)
446
    {
447
        try {
448
            return $this->innerGateway->getFallbackMainNodeData($contentId, $locationId);
449
        } catch (DBALException $e) {
450
            throw new RuntimeException('Database error', 0, $e);
451
        } catch (PDOException $e) {
452
            throw new RuntimeException('Database error', 0, $e);
453
        }
454
    }
455
456
    /**
457
     * Sends a single location identified by given $locationId to the trash.
458
     *
459
     * The associated content object is left untouched.
460
     *
461
     * @param mixed $locationId
462
     *
463
     * @return bool
464
     */
465
    public function trashLocation($locationId)
466
    {
467
        try {
468
            return $this->innerGateway->trashLocation($locationId);
469
        } catch (DBALException $e) {
470
            throw new RuntimeException('Database error', 0, $e);
471
        } catch (PDOException $e) {
472
            throw new RuntimeException('Database error', 0, $e);
473
        }
474
    }
475
476
    /**
477
     * Returns a trashed location to normal state.
478
     *
479
     * Recreates the originally trashed location in the new position. If no new
480
     * position has been specified, it will be tried to re-create the location
481
     * at the old position. If this is not possible ( because the old location
482
     * does not exist any more) and exception is thrown.
483
     *
484
     * @param mixed $locationId
485
     * @param mixed $newParentId
486
     *
487
     * @return \eZ\Publish\SPI\Persistence\Content\Location
488
     */
489
    public function untrashLocation($locationId, $newParentId = null)
490
    {
491
        try {
492
            return $this->innerGateway->untrashLocation($locationId, $newParentId);
493
        } catch (DBALException $e) {
494
            throw new RuntimeException('Database error', 0, $e);
495
        } catch (PDOException $e) {
496
            throw new RuntimeException('Database error', 0, $e);
497
        }
498
    }
499
500
    /**
501
     * Loads trash data specified by location ID.
502
     *
503
     * @param mixed $locationId
504
     *
505
     * @return array
506
     */
507
    public function loadTrashByLocation($locationId)
508
    {
509
        try {
510
            return $this->innerGateway->loadTrashByLocation($locationId);
511
        } catch (DBALException $e) {
512
            throw new RuntimeException('Database error', 0, $e);
513
        } catch (PDOException $e) {
514
            throw new RuntimeException('Database error', 0, $e);
515
        }
516
    }
517
518
    /**
519
     * Loads trash data specified by content ID.
520
     *
521
     * @param mixed $contentId
522
     *
523
     * @return array
524
     */
525 View Code Duplication
    public function loadTrashByContent($contentId)
526
    {
527
        try {
528
            return $this->innerGateway->loadTrashByContent($contentId);
529
        } catch (DBALException $e) {
530
            throw new RuntimeException('Database error', 0, $e);
531
        } catch (PDOException $e) {
532
            throw new RuntimeException('Database error', 0, $e);
533
        }
534
    }
535
536
    /**
537
     * Removes every entries in the trash.
538
     * Will NOT remove associated content objects nor attributes.
539
     *
540
     * Basically truncates ezcontentobject_trash table.
541
     */
542
    public function cleanupTrash()
543
    {
544
        try {
545
            return $this->innerGateway->cleanupTrash();
546
        } catch (DBALException $e) {
547
            throw new RuntimeException('Database error', 0, $e);
548
        } catch (PDOException $e) {
549
            throw new RuntimeException('Database error', 0, $e);
550
        }
551
    }
552
553
    /**
554
     * Lists trashed items.
555
     * Returns entries from ezcontentobject_trash.
556
     *
557
     * @param int $offset
558
     * @param int $limit
559
     * @param array $sort
560
     *
561
     * @return array
562
     */
563 View Code Duplication
    public function listTrashed($offset, $limit, array $sort = null)
564
    {
565
        try {
566
            return $this->innerGateway->listTrashed($offset, $limit, $sort);
567
        } catch (DBALException $e) {
568
            throw new RuntimeException('Database error', 0, $e);
569
        } catch (PDOException $e) {
570
            throw new RuntimeException('Database error', 0, $e);
571
        }
572
    }
573
574
    /**
575
     * Removes trashed element identified by $id from trash.
576
     * Will NOT remove associated content object nor attributes.
577
     *
578
     * @param int $id The trashed location Id
579
     */
580
    public function removeElementFromTrash($id)
581
    {
582
        try {
583
            return $this->innerGateway->removeElementFromTrash($id);
584
        } catch (DBALException $e) {
585
            throw new RuntimeException('Database error', 0, $e);
586
        } catch (PDOException $e) {
587
            throw new RuntimeException('Database error', 0, $e);
588
        }
589
    }
590
591
    /**
592
     * Set section on all content objects in the subtree.
593
     *
594
     * @param mixed $pathString
595
     * @param mixed $sectionId
596
     *
597
     * @return bool
598
     */
599
    public function setSectionForSubtree($pathString, $sectionId)
600
    {
601
        try {
602
            return $this->innerGateway->setSectionForSubtree($pathString, $sectionId);
603
        } catch (DBALException $e) {
604
            throw new RuntimeException('Database error', 0, $e);
605
        } catch (PDOException $e) {
606
            throw new RuntimeException('Database error', 0, $e);
607
        }
608
    }
609
610
    /**
611
     * Returns how many locations given content object identified by $contentId has.
612
     *
613
     * @param int $contentId
614
     *
615
     * @return int
616
     */
617
    public function countLocationsByContentId($contentId)
618
    {
619
        try {
620
            return $this->innerGateway->countLocationsByContentId($contentId);
621
        } catch (DBALException $e) {
622
            throw new RuntimeException('Database error', 0, $e);
623
        } catch (PDOException $e) {
624
            throw new RuntimeException('Database error', 0, $e);
625
        }
626
    }
627
628
    /**
629
     * Changes main location of content identified by given $contentId to location identified by given $locationId.
630
     *
631
     * Updates ezcontentobject_tree table for the given $contentId and eznode_assignment table for the given
632
     * $contentId, $parentLocationId and $versionNo
633
     *
634
     * @param mixed $contentId
635
     * @param mixed $locationId
636
     * @param mixed $versionNo version number, needed to update eznode_assignment table
637
     * @param mixed $parentLocationId parent location of location identified by $locationId, needed to update
638
     *        eznode_assignment table
639
     */
640
    public function changeMainLocation($contentId, $locationId, $versionNo, $parentLocationId)
641
    {
642
        try {
643
            return $this->innerGateway->changeMainLocation($contentId, $locationId, $versionNo, $parentLocationId);
644
        } catch (DBALException $e) {
645
            throw new RuntimeException('Database error', 0, $e);
646
        } catch (PDOException $e) {
647
            throw new RuntimeException('Database error', 0, $e);
648
        }
649
    }
650
}
651