Completed
Push — 7.0_master_link_manager_merge ( e446e3 )
by André
77:34 queued 60:56
created

URLHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 106
Duplicated Lines 24.53 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 26
loc 106
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A find() 0 8 1
A loadById() 13 13 2
A loadByUrl() 0 4 1
A findUsages() 13 13 2
A updateUrl() 0 14 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
namespace eZ\Publish\Core\Persistence\Cache;
8
9
use eZ\Publish\API\Repository\Values\URL\URLQuery;
10
use eZ\Publish\SPI\Persistence\URL\Handler as URLHandlerInterface;
11
use eZ\Publish\SPI\Persistence\URL\URLUpdateStruct;
12
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
13
14
/**
15
 * SPI cache for URL Handler.
16
 *
17
 * @see \eZ\Publish\SPI\Persistence\URL\Handler
18
 */
19
class URLHandler implements URLHandlerInterface
20
{
21
    /**
22
     * @var \eZ\Publish\Core\Persistence\Cache\CacheServiceDecorator
23
     */
24
    protected $cache;
25
26
    /**
27
     * @var \eZ\Publish\SPI\Persistence\Handler
28
     */
29
    protected $persistenceHandler;
30
31
    /**
32
     * @var \eZ\Publish\Core\Persistence\Cache\PersistenceLogger
33
     */
34
    protected $logger;
35
36
    /**
37
     * Setups current handler with everything needed.
38
     *
39
     * @param \eZ\Publish\Core\Persistence\Cache\CacheServiceDecorator $cache
40
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
41
     * @param \eZ\Publish\Core\Persistence\Cache\PersistenceLogger $logger
42
     */
43
    public function __construct(
44
        CacheServiceDecorator $cache,
45
        PersistenceHandler $persistenceHandler,
46
        PersistenceLogger $logger)
47
    {
48
        $this->cache = $cache;
49
        $this->persistenceHandler = $persistenceHandler;
50
        $this->logger = $logger;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function updateUrl($id, URLUpdateStruct $struct)
57
    {
58
        $this->logger->logCall(__METHOD__, [
59
            'url' => $id,
60
            'struct' => $struct,
61
        ]);
62
63
        $url = $this->persistenceHandler->urlHandler()->updateUrl($id, $struct);
64
65
        $this->cache->clear('url', $id);
66
        $this->cache->clear('content');
67
68
        return $url;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function find(URLQuery $query)
75
    {
76
        $this->logger->logCall(__METHOD__, [
77
            'query' => $query,
78
        ]);
79
80
        return $this->persistenceHandler->urlHandler()->find($query);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 View Code Duplication
    public function loadById($id)
87
    {
88
        $cache = $this->cache->getItem('url', $id);
89
90
        $url = $cache->get();
91
        if ($cache->isMiss()) {
92
            $this->logger->logCall(__METHOD__, ['url' => $id]);
93
            $url = $this->persistenceHandler->urlHandler()->loadById($id);
94
            $cache->set($url)->save();
95
        }
96
97
        return $url;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function loadByUrl($url)
104
    {
105
        return $this->persistenceHandler->urlHandler()->loadByUrl($url);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 View Code Duplication
    public function findUsages($id)
112
    {
113
        $cache = $this->cache->getItem('url', $id, 'usages');
114
115
        $usages = $cache->get();
116
        if ($cache->isMiss()) {
117
            $this->logger->logCall(__METHOD__, ['url' => $id]);
118
            $usages = $this->persistenceHandler->urlHandler()->findUsages($id);
119
            $cache->set($usages)->save();
120
        }
121
122
        return $usages;
123
    }
124
}
125