Completed
Push — location_multi_load ( e5e305...41e541 )
by André
51:38 queued 33:21
created

UserPreferenceService::setUserPreference()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 1
dl 0
loc 34
rs 8.7537
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
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository;
10
11
use Exception;
12
use eZ\Publish\API\Repository\UserPreferenceService as UserPreferenceServiceInterface;
13
use eZ\Publish\API\Repository\Values\UserPreference\UserPreference as APIUserPreference;
14
use eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceList;
15
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
16
use eZ\Publish\SPI\Persistence\UserPreference\UserPreferenceSetStruct;
17
use eZ\Publish\SPI\Persistence\UserPreference\Handler as UserPreferenceHandler;
18
use eZ\Publish\SPI\Persistence\UserPreference\UserPreference;
19
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
20
21
class UserPreferenceService implements UserPreferenceServiceInterface
22
{
23
    /**
24
     * @var \eZ\Publish\API\Repository\Repository
25
     */
26
    private $repository;
27
28
    /**
29
     * @var \eZ\Publish\SPI\Persistence\UserPreference\Handler
30
     */
31
    private $userPreferenceHandler;
32
33
    /**
34
     * @param \eZ\Publish\API\Repository\Repository $repository
35
     * @param \eZ\Publish\SPI\Persistence\UserPreference\Handler $userPreferenceHandler
36
     */
37
    public function __construct(RepositoryInterface $repository, UserPreferenceHandler $userPreferenceHandler)
38
    {
39
        $this->repository = $repository;
40
        $this->userPreferenceHandler = $userPreferenceHandler;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 View Code Duplication
    public function loadUserPreferences(int $offset = 0, int $limit = 25): UserPreferenceList
47
    {
48
        $currentUserId = $this->getCurrentUserId();
49
50
        $list = new UserPreferenceList();
51
52
        $list->totalCount = $this->userPreferenceHandler->countUserPreferences($currentUserId);
53
        if ($list->totalCount > 0) {
54
            $list->items = array_map(function (UserPreference $spiUserPreference) {
55
                return $this->buildDomainObject($spiUserPreference);
56
            }, $this->userPreferenceHandler->loadUserPreferences($currentUserId, $offset, $limit));
57
        }
58
59
        return $list;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function setUserPreference(array $userPreferenceSetStructs): void
66
    {
67
        $spiSetStructs = [];
68
        foreach ($userPreferenceSetStructs as $key => $userPreferenceSetStruct) {
69
            $spiSetStruct = new UserPreferenceSetStruct();
70
            $spiSetStruct->userId = $this->getCurrentUserId();
71
72
            if (empty($userPreferenceSetStruct->name)) {
73
                throw new InvalidArgumentException('name', $userPreferenceSetStruct->name . ' at index ' . $key);
74
            }
75
76
            $spiSetStruct->name = $userPreferenceSetStruct->name;
77
78
            try {
79
                $value = (string)$userPreferenceSetStruct->value;
80
            } catch (\Exception $exception) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $excep...g at index ' . $key); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
81
                throw new InvalidArgumentException('value', 'Can not convert value to string at index ' . $key);
82
            }
83
84
            $spiSetStruct->value = $value;
85
            $spiSetStructs[] = $spiSetStruct;
86
        }
87
88
        $this->repository->beginTransaction();
89
        try {
90
            foreach ($spiSetStructs as $spiSetStruct) {
91
                $this->userPreferenceHandler->setUserPreference($spiSetStruct);
92
            }
93
            $this->repository->commit();
94
        } catch (Exception $e) {
95
            $this->repository->rollback();
96
            throw $e;
97
        }
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getUserPreference(string $userPreferenceName): APIUserPreference
104
    {
105
        $currentUserId = $this->getCurrentUserId();
106
107
        $userPreference = $this->userPreferenceHandler->getUserPreferenceByUserIdAndName(
108
            $currentUserId,
109
            $userPreferenceName
110
        );
111
112
        return $this->buildDomainObject($userPreference);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getUserPreferenceCount(): int
119
    {
120
        return $this->userPreferenceHandler->countUserPreferences(
121
            $this->getCurrentUserId()
122
        );
123
    }
124
125
    /**
126
     * Builds UserPreference domain object from ValueObject returned by Persistence API.
127
     *
128
     * @param \eZ\Publish\SPI\Persistence\UserPreference\UserPreference $spiUserPreference
129
     *
130
     * @return \eZ\Publish\API\Repository\Values\UserPreference\UserPreference
131
     */
132
    protected function buildDomainObject(UserPreference $spiUserPreference): APIUserPreference
133
    {
134
        return new APIUserPreference([
135
            'name' => $spiUserPreference->name,
136
            'value' => $spiUserPreference->value,
137
        ]);
138
    }
139
140
    private function getCurrentUserId(): int
141
    {
142
        return $this->repository
143
            ->getPermissionResolver()
144
            ->getCurrentUserReference()
145
            ->getUserId();
146
    }
147
}
148