Completed
Push — master ( 2c0170...b4f3d9 )
by Łukasz
23:04
created

BookmarkHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 9.9 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 10
loc 101
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A delete() 10 10 1
B loadByUserIdAndLocationId() 0 29 2
A loadUserBookmarks() 0 10 1
A countUserBookmarks() 0 8 1
A locationSwapped() 0 10 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
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use eZ\Publish\SPI\Persistence\Bookmark\Bookmark;
12
use eZ\Publish\SPI\Persistence\Bookmark\CreateStruct;
13
use eZ\Publish\SPI\Persistence\Bookmark\Handler as BookmarkHandlerInterface;
14
15
/**
16
 * @see \eZ\Publish\SPI\Persistence\Bookmark\Handler
17
 */
18
class BookmarkHandler extends AbstractHandler implements BookmarkHandlerInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function create(CreateStruct $createStruct): Bookmark
24
    {
25
        $this->logger->logCall(__METHOD__, [
26
            'createStruct' => $createStruct,
27
        ]);
28
29
        return $this->persistenceHandler->bookmarkHandler()->create($createStruct);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 View Code Duplication
    public function delete(int $bookmarkId): void
36
    {
37
        $this->logger->logCall(__METHOD__, [
38
            'id' => $bookmarkId,
39
        ]);
40
41
        $this->persistenceHandler->bookmarkHandler()->delete($bookmarkId);
42
43
        $this->cache->invalidateTags(['bookmark-' . $bookmarkId]);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function loadByUserIdAndLocationId(int $userId, array $locationIds): array
50
    {
51
        return $this->getMultipleCacheItems(
52
            $locationIds,
53
            'ez-bookmark-' . $userId . '-',
54
            function (array $missingIds) use ($userId) {
55
                $this->logger->logCall(__METHOD__, [
56
                    'userId' => $userId,
57
                    'locationIds' => $missingIds,
58
                ]);
59
60
                return $this->persistenceHandler->bookmarkHandler()->loadByUserIdAndLocationId($userId, $missingIds);
61
            },
62
            function (Bookmark $bookmark) {
63
                $tags = [
64
                    'bookmark-' . $bookmark->id,
65
                    'location-' . $bookmark->locationId,
66
                    'user-' . $bookmark->userId,
67
                ];
68
69
                $location = $this->persistenceHandler->locationHandler()->load($bookmark->locationId);
70
                foreach (explode('/', trim($location->pathString, '/')) as $locationId) {
71
                    $tags[] = 'location-path-' . $locationId;
72
                }
73
74
                return $tags;
75
            }
76
        );
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
83
    {
84
        $this->logger->logCall(__METHOD__, [
85
            'userId' => $userId,
86
            'offset' => $offset,
87
            'limit' => $limit,
88
        ]);
89
90
        return $this->persistenceHandler->bookmarkHandler()->loadUserBookmarks($userId, $offset, $limit);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function countUserBookmarks(int $userId): int
97
    {
98
        $this->logger->logCall(__METHOD__, [
99
            'userId' => $userId,
100
        ]);
101
102
        return $this->persistenceHandler->bookmarkHandler()->countUserBookmarks($userId);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function locationSwapped(int $location1Id, int $location2Id): void
109
    {
110
        $this->logger->logCall(__METHOD__, [
111
            'location1Id' => $location1Id,
112
            'location2Id' => $location2Id,
113
        ]);
114
115
        // Cache clearing is already done by locationHandler
116
        $this->persistenceHandler->bookmarkHandler()->locationSwapped($location1Id, $location2Id);
117
    }
118
}
119