Completed
Push — ezp-29724 ( 495423...21f3e8 )
by
unknown
46:15 queued 21:42
created

Handler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setUserPreference() 0 6 1
A getUserPreferenceByUserIdAndName() 0 12 2
A countUserPreferences() 0 4 1
A loadUserPreferences() 0 6 1
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\Legacy\UserPreference;
10
11
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
12
use eZ\Publish\SPI\Persistence\UserPreference\UserPreferenceSetStruct;
13
use eZ\Publish\SPI\Persistence\UserPreference\Handler as HandlerInterface;
14
use eZ\Publish\SPI\Persistence\UserPreference\UserPreference;
15
16
class Handler implements HandlerInterface
17
{
18
    /**
19
     * @var \eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway
20
     */
21
    protected $gateway;
22
23
    /**
24
     * @var \eZ\Publish\Core\Persistence\Legacy\UserPreference\Mapper
25
     */
26
    protected $mapper;
27
28
    /**
29
     * @param \eZ\Publish\Core\Persistence\Legacy\UserPreference\Gateway $gateway
30
     * @param \eZ\Publish\Core\Persistence\Legacy\UserPreference\Mapper $mapper
31
     */
32
    public function __construct(Gateway $gateway, Mapper $mapper)
33
    {
34
        $this->gateway = $gateway;
35
        $this->mapper = $mapper;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
42
     */
43
    public function setUserPreference(UserPreferenceSetStruct $setStruct): UserPreference
44
    {
45
        $this->gateway->setUserPreference($setStruct);
46
47
        return $this->getUserPreferenceByUserIdAndName($setStruct->userId, $setStruct->name);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
54
     */
55
    public function getUserPreferenceByUserIdAndName(int $userId, string $name): UserPreference
56
    {
57
        $userPreference = $this->mapper->extractUserPreferencesFromRows(
58
            $this->gateway->getUserPreferenceByUserIdAndName($userId, $name)
59
        );
60
61
        if (count($userPreference) < 1) {
62
            throw new NotFoundException('User Preference', $userId . ',' . $name);
63
        }
64
65
        return reset($userPreference);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function countUserPreferences(int $userId): int
72
    {
73
        return $this->gateway->countUserPreferences($userId);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function loadUserPreferences(int $userId, int $offset, int $limit): array
80
    {
81
        return $this->mapper->extractUserPreferencesFromRows(
82
            $this->gateway->loadUserPreferences($userId, $offset, $limit)
83
        );
84
    }
85
}
86