Completed
Push — 7.0 ( 66fc99...d11e6e )
by André
14:26
created

Handler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 25.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 24
loc 95
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A updateUrl() 0 11 1
A find() 0 14 1
A loadById() 12 12 2
A loadByUrl() 12 12 2
A findUsages() 0 6 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\Legacy\URL;
8
9
use eZ\Publish\API\Repository\Values\URL\URLQuery;
10
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
11
use eZ\Publish\SPI\Persistence\URL\Handler as HandlerInterface;
12
use eZ\Publish\SPI\Persistence\URL\URLUpdateStruct;
13
14
/**
15
 * Storage Engine handler for URLs.
16
 */
17
class Handler implements HandlerInterface
18
{
19
    /** @var \eZ\Publish\Core\Persistence\Legacy\URL\Gateway */
20
    private $urlGateway;
21
22
    /** @var \eZ\Publish\Core\Persistence\Legacy\URL\Mapper */
23
    private $urlMapper;
24
25
    /**
26
     * Handler constructor.
27
     *
28
     * @param \eZ\Publish\Core\Persistence\Legacy\URL\Gateway $gateway
29
     * @param \eZ\Publish\Core\Persistence\Legacy\URL\Mapper $mapper
30
     */
31
    public function __construct(Gateway $gateway, Mapper $mapper)
32
    {
33
        $this->urlGateway = $gateway;
34
        $this->urlMapper = $mapper;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function updateUrl($id, URLUpdateStruct $urlUpdateStruct)
41
    {
42
        $url = $this->urlMapper->createURLFromUpdateStruct(
43
            $urlUpdateStruct
44
        );
45
        $url->id = $id;
46
47
        $this->urlGateway->updateUrl($url);
48
49
        return $url;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function find(URLQuery $query)
56
    {
57
        $results = $this->urlGateway->find(
58
            $query->filter,
59
            $query->offset,
60
            $query->limit,
61
            $query->sortClauses
62
        );
63
64
        return [
65
            'count' => $results['count'],
66
            'items' => $this->urlMapper->extractURLsFromRows($results['rows']),
67
        ];
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 View Code Duplication
    public function loadById($id)
74
    {
75
        $url = $this->urlMapper->extractURLsFromRows(
76
            $this->urlGateway->loadUrlData($id)
77
        );
78
79
        if (count($url) < 1) {
80
            throw new NotFoundException('URL', $id);
81
        }
82
83
        return reset($url);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($url); of type eZ\Publish\SPI\Persistence\URL\URL|false adds false to the return on line 83 which is incompatible with the return type declared by the interface eZ\Publish\SPI\Persistence\URL\Handler::loadById of type eZ\Publish\SPI\Persistence\URL\URL. It seems like you forgot to handle an error condition.
Loading history...
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 View Code Duplication
    public function loadByUrl($url)
90
    {
91
        $urls = $this->urlMapper->extractURLsFromRows(
92
            $this->urlGateway->loadUrlDataByUrl($url)
93
        );
94
95
        if (count($urls) < 1) {
96
            throw new NotFoundException('URL', $url);
97
        }
98
99
        return reset($urls);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($urls); of type eZ\Publish\SPI\Persistence\URL\URL|false adds false to the return on line 99 which is incompatible with the return type declared by the interface eZ\Publish\SPI\Persistence\URL\Handler::loadByUrl of type eZ\Publish\SPI\Persistence\URL\URL. It seems like you forgot to handle an error condition.
Loading history...
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function findUsages($id)
106
    {
107
        $ids = $this->urlGateway->findUsages($id);
108
109
        return $ids;
110
    }
111
}
112