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

Handler::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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