Completed
Push — ezp-30616 ( 0a36b6 )
by
unknown
21:15 queued 06:15
created

UserService::updateUserGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 23
loc 23
rs 9.552
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\Event;
10
11
use eZ\Publish\SPI\Repository\Decorator\UserServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\UserService as UserServiceInterface;
14
use eZ\Publish\API\Repository\Values\User\User;
15
use eZ\Publish\API\Repository\Values\User\UserCreateStruct;
16
use eZ\Publish\API\Repository\Values\User\UserGroup;
17
use eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct;
18
use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct;
19
use eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct;
20
use eZ\Publish\API\Repository\Values\User\UserUpdateStruct;
21
use eZ\Publish\Core\Event\User\AssignUserToUserGroupEvent;
22
use eZ\Publish\Core\Event\User\BeforeAssignUserToUserGroupEvent;
23
use eZ\Publish\Core\Event\User\BeforeCreateUserEvent;
24
use eZ\Publish\Core\Event\User\BeforeCreateUserGroupEvent;
25
use eZ\Publish\Core\Event\User\BeforeDeleteUserEvent;
26
use eZ\Publish\Core\Event\User\BeforeDeleteUserGroupEvent;
27
use eZ\Publish\Core\Event\User\BeforeMoveUserGroupEvent;
28
use eZ\Publish\Core\Event\User\BeforeUnAssignUserFromUserGroupEvent;
29
use eZ\Publish\Core\Event\User\BeforeUpdateUserEvent;
30
use eZ\Publish\Core\Event\User\BeforeUpdateUserGroupEvent;
31
use eZ\Publish\Core\Event\User\BeforeUpdateUserTokenEvent;
32
use eZ\Publish\Core\Event\User\CreateUserEvent;
33
use eZ\Publish\Core\Event\User\CreateUserGroupEvent;
34
use eZ\Publish\Core\Event\User\DeleteUserEvent;
35
use eZ\Publish\Core\Event\User\DeleteUserGroupEvent;
36
use eZ\Publish\Core\Event\User\MoveUserGroupEvent;
37
use eZ\Publish\Core\Event\User\UnAssignUserFromUserGroupEvent;
38
use eZ\Publish\Core\Event\User\UpdateUserEvent;
39
use eZ\Publish\Core\Event\User\UpdateUserGroupEvent;
40
use eZ\Publish\Core\Event\User\UpdateUserTokenEvent;
41
use eZ\Publish\Core\Event\User\UserEvents;
42
43
class UserService extends UserServiceDecorator implements UserServiceInterface
44
{
45
    /**
46
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
47
     */
48
    protected $eventDispatcher;
49
50
    public function __construct(
51
        UserServiceInterface $innerService,
52
        EventDispatcherInterface $eventDispatcher
53
    ) {
54
        parent::__construct($innerService);
55
56
        $this->eventDispatcher = $eventDispatcher;
57
    }
58
59 View Code Duplication
    public function createUserGroup(
60
        UserGroupCreateStruct $userGroupCreateStruct,
61
        UserGroup $parentGroup
62
    ) {
63
        $eventData = [
64
            $userGroupCreateStruct,
65
            $parentGroup,
66
        ];
67
68
        $beforeEvent = new BeforeCreateUserGroupEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateUserGroupEvent::__construct() misses a required argument $parentGroup.

This check looks for function calls that miss required arguments.

Loading history...
69
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_CREATE_USER_GROUP, $beforeEvent)->isPropagationStopped()) {
70
            return $beforeEvent->getReturnValue();
71
        } else {
72
            $userGroup = parent::createUserGroup($userGroupCreateStruct, $parentGroup);
73
        }
74
75
        $this->eventDispatcher->dispatch(
76
            UserEvents::CREATE_USER_GROUP,
77
            new CreateUserGroupEvent($userGroup, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateUserGroupEvent::__construct() misses a required argument $parentGroup.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $userGroup defined by parent::createUserGroup(...teStruct, $parentGroup) on line 72 can be null; however, eZ\Publish\Core\Event\Us...oupEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
78
        );
79
80
        return $userGroup;
81
    }
82
83 View Code Duplication
    public function deleteUserGroup(UserGroup $userGroup)
84
    {
85
        $eventData = [$userGroup];
86
87
        $beforeEvent = new BeforeDeleteUserGroupEvent(...$eventData);
88
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_DELETE_USER_GROUP, $beforeEvent)->isPropagationStopped()) {
89
            return $beforeEvent->getReturnValue();
90
        } else {
91
            $locations = parent::deleteUserGroup($userGroup);
92
        }
93
94
        $this->eventDispatcher->dispatch(
95
            UserEvents::DELETE_USER_GROUP,
96
            new DeleteUserGroupEvent($locations, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $locations defined by parent::deleteUserGroup($userGroup) on line 91 can also be of type null; however, eZ\Publish\Core\Event\Us...oupEvent::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97
        );
98
99
        return $locations;
100
    }
101
102
    public function moveUserGroup(
103
        UserGroup $userGroup,
104
        UserGroup $newParent
105
    ) {
106
        $eventData = [
107
            $userGroup,
108
            $newParent,
109
        ];
110
111
        $beforeEvent = new BeforeMoveUserGroupEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeMoveUserGroupEvent::__construct() misses a required argument $newParent.

This check looks for function calls that miss required arguments.

Loading history...
112
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_MOVE_USER_GROUP, $beforeEvent)->isPropagationStopped()) {
113
            return;
114
        } else {
115
            parent::moveUserGroup($userGroup, $newParent);
116
        }
117
118
        $this->eventDispatcher->dispatch(
119
            UserEvents::MOVE_USER_GROUP,
120
            new MoveUserGroupEvent(...$eventData)
0 ignored issues
show
Bug introduced by
The call to MoveUserGroupEvent::__construct() misses a required argument $newParent.

This check looks for function calls that miss required arguments.

Loading history...
121
        );
122
    }
123
124 View Code Duplication
    public function updateUserGroup(
125
        UserGroup $userGroup,
126
        UserGroupUpdateStruct $userGroupUpdateStruct
127
    ) {
128
        $eventData = [
129
            $userGroup,
130
            $userGroupUpdateStruct,
131
        ];
132
133
        $beforeEvent = new BeforeUpdateUserGroupEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateUserGroupEvent::__construct() misses a required argument $userGroupUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
134
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_UPDATE_USER_GROUP, $beforeEvent)->isPropagationStopped()) {
135
            return $beforeEvent->getReturnValue();
136
        } else {
137
            $updatedUserGroup = parent::updateUserGroup($userGroup, $userGroupUpdateStruct);
138
        }
139
140
        $this->eventDispatcher->dispatch(
141
            UserEvents::UPDATE_USER_GROUP,
142
            new UpdateUserGroupEvent($updatedUserGroup, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to UpdateUserGroupEvent::__construct() misses a required argument $userGroupUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $updatedUserGroup defined by parent::updateUserGroup(...$userGroupUpdateStruct) on line 137 can be null; however, eZ\Publish\Core\Event\Us...oupEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
143
        );
144
145
        return $updatedUserGroup;
146
    }
147
148 View Code Duplication
    public function createUser(
149
        UserCreateStruct $userCreateStruct,
150
        array $parentGroups
151
    ) {
152
        $eventData = [
153
            $userCreateStruct,
154
            $parentGroups,
155
        ];
156
157
        $beforeEvent = new BeforeCreateUserEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateUserEvent::__construct() misses a required argument $parentGroups.

This check looks for function calls that miss required arguments.

Loading history...
158
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_CREATE_USER, $beforeEvent)->isPropagationStopped()) {
159
            return $beforeEvent->getReturnValue();
160
        } else {
161
            $user = parent::createUser($userCreateStruct, $parentGroups);
162
        }
163
164
        $this->eventDispatcher->dispatch(
165
            UserEvents::CREATE_USER,
166
            new CreateUserEvent($user, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateUserEvent::__construct() misses a required argument $parentGroups.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $user defined by parent::createUser($user...eStruct, $parentGroups) on line 161 can be null; however, eZ\Publish\Core\Event\Us...serEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
167
        );
168
169
        return $user;
170
    }
171
172 View Code Duplication
    public function deleteUser(User $user)
173
    {
174
        $eventData = [$user];
175
176
        $beforeEvent = new BeforeDeleteUserEvent(...$eventData);
177
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_DELETE_USER, $beforeEvent)->isPropagationStopped()) {
178
            return $beforeEvent->getReturnValue();
179
        } else {
180
            $locations = parent::deleteUser($user);
181
        }
182
183
        $this->eventDispatcher->dispatch(
184
            UserEvents::DELETE_USER,
185
            new DeleteUserEvent($locations, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $locations defined by parent::deleteUser($user) on line 180 can also be of type null; however, eZ\Publish\Core\Event\Us...serEvent::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
186
        );
187
188
        return $locations;
189
    }
190
191 View Code Duplication
    public function updateUser(
192
        User $user,
193
        UserUpdateStruct $userUpdateStruct
194
    ) {
195
        $eventData = [
196
            $user,
197
            $userUpdateStruct,
198
        ];
199
200
        $beforeEvent = new BeforeUpdateUserEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateUserEvent::__construct() misses a required argument $userUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
201
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_UPDATE_USER, $beforeEvent)->isPropagationStopped()) {
202
            return $beforeEvent->getReturnValue();
203
        } else {
204
            $updatedUser = parent::updateUser($user, $userUpdateStruct);
205
        }
206
207
        $this->eventDispatcher->dispatch(
208
            UserEvents::UPDATE_USER,
209
            new UpdateUserEvent($updatedUser, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to UpdateUserEvent::__construct() misses a required argument $userUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $updatedUser defined by parent::updateUser($user, $userUpdateStruct) on line 204 can be null; however, eZ\Publish\Core\Event\Us...serEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
210
        );
211
212
        return $updatedUser;
213
    }
214
215 View Code Duplication
    public function updateUserToken(
216
        User $user,
217
        UserTokenUpdateStruct $userTokenUpdateStruct
218
    ) {
219
        $eventData = [
220
            $user,
221
            $userTokenUpdateStruct,
222
        ];
223
224
        $beforeEvent = new BeforeUpdateUserTokenEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateUserTokenEvent::__construct() misses a required argument $userTokenUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
225
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_UPDATE_USER_TOKEN, $beforeEvent)->isPropagationStopped()) {
226
            return $beforeEvent->getReturnValue();
227
        } else {
228
            $updatedUser = parent::updateUserToken($user, $userTokenUpdateStruct);
229
        }
230
231
        $this->eventDispatcher->dispatch(
232
            UserEvents::UPDATE_USER_TOKEN,
233
            new UpdateUserTokenEvent($updatedUser, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to UpdateUserTokenEvent::__construct() misses a required argument $userTokenUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $updatedUser defined by parent::updateUserToken(...$userTokenUpdateStruct) on line 228 can be null; however, eZ\Publish\Core\Event\Us...kenEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
234
        );
235
236
        return $updatedUser;
237
    }
238
239
    public function assignUserToUserGroup(
240
        User $user,
241
        UserGroup $userGroup
242
    ) {
243
        $eventData = [
244
            $user,
245
            $userGroup,
246
        ];
247
248
        $beforeEvent = new BeforeAssignUserToUserGroupEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeAssignUserToUserGroupEvent::__construct() misses a required argument $userGroup.

This check looks for function calls that miss required arguments.

Loading history...
249
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_ASSIGN_USER_TO_USER_GROUP, $beforeEvent)->isPropagationStopped()) {
250
            return;
251
        } else {
252
            parent::assignUserToUserGroup($user, $userGroup);
253
        }
254
255
        $this->eventDispatcher->dispatch(
256
            UserEvents::ASSIGN_USER_TO_USER_GROUP,
257
            new AssignUserToUserGroupEvent(...$eventData)
0 ignored issues
show
Bug introduced by
The call to AssignUserToUserGroupEvent::__construct() misses a required argument $userGroup.

This check looks for function calls that miss required arguments.

Loading history...
258
        );
259
    }
260
261
    public function unAssignUserFromUserGroup(
262
        User $user,
263
        UserGroup $userGroup
264
    ) {
265
        $eventData = [
266
            $user,
267
            $userGroup,
268
        ];
269
270
        $beforeEvent = new BeforeUnAssignUserFromUserGroupEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUnAssignUserFromUs...oupEvent::__construct() misses a required argument $userGroup.

This check looks for function calls that miss required arguments.

Loading history...
271
        if ($this->eventDispatcher->dispatch(UserEvents::BEFORE_UN_ASSIGN_USER_FROM_USER_GROUP, $beforeEvent)->isPropagationStopped()) {
272
            return;
273
        } else {
274
            parent::unAssignUserFromUserGroup($user, $userGroup);
275
        }
276
277
        $this->eventDispatcher->dispatch(
278
            UserEvents::UN_ASSIGN_USER_FROM_USER_GROUP,
279
            new UnAssignUserFromUserGroupEvent(...$eventData)
0 ignored issues
show
Bug introduced by
The call to UnAssignUserFromUserGroupEvent::__construct() misses a required argument $userGroup.

This check looks for function calls that miss required arguments.

Loading history...
280
        );
281
    }
282
}
283