User::findUserByIdUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\User\Business\Model;
9
10
use Generated\Shared\Transfer\CollectionTransfer;
11
use Generated\Shared\Transfer\UserCollectionResponseTransfer;
12
use Generated\Shared\Transfer\UserCollectionTransfer;
13
use Generated\Shared\Transfer\UserCriteriaTransfer;
14
use Generated\Shared\Transfer\UserTransfer;
15
use Orm\Zed\User\Persistence\Map\SpyUserTableMap;
16
use Orm\Zed\User\Persistence\SpyUser;
17
use Spryker\Client\Session\SessionClientInterface;
18
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
19
use Spryker\Zed\User\Business\Exception\PasswordEncryptionFailedException;
20
use Spryker\Zed\User\Business\Exception\UsernameExistsException;
21
use Spryker\Zed\User\Business\Exception\UserNotFoundException;
22
use Spryker\Zed\User\Persistence\UserQueryContainerInterface;
23
use Spryker\Zed\User\UserConfig;
24
25
class User implements UserInterface
26
{
27
    use TransactionTrait;
28
29
    /**
30
     * @var string
31
     */
32
    public const USER_BUNDLE_SESSION_KEY = 'user';
33
34
    /**
35
     * @var \Spryker\Zed\User\Persistence\UserQueryContainerInterface
36
     */
37
    protected $queryContainer;
38
39
    /**
40
     * @var \Spryker\Client\Session\SessionClientInterface
41
     */
42
    protected $session;
43
44
    /**
45
     * @var \Spryker\Zed\User\UserConfig
46
     */
47
    protected $userConfig;
48
49
    /**
50
     * @var list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPostSavePluginInterface>
51
     */
52
    protected $userPostSavePlugins;
53
54
    /**
55
     * @var list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPreSavePluginInterface>
56
     */
57
    protected $userPreSavePlugins;
58
59
    /**
60
     * @deprecated Use {@link \Spryker\Zed\User\Business\Model\User::$userExpanderPlugins} instead.
61
     *
62
     * @var list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserTransferExpanderPluginInterface>
63
     */
64
    protected $userTransferExpanderPlugins;
65
66
    /**
67
     * @var list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserExpanderPluginInterface>
68
     */
69
    protected array $userExpanderPlugins;
70
71
    /**
72
     * @var list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPostCreatePluginInterface>
73
     */
74
    protected array $userPostCreatePlugins;
75
76
    /**
77
     * @var list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPostUpdatePluginInterface>
78
     */
79
    protected array $userPostUpdatePlugins;
80
81
    /**
82
     * @param \Spryker\Zed\User\Persistence\UserQueryContainerInterface $queryContainer
83
     * @param \Spryker\Client\Session\SessionClientInterface $session
84
     * @param \Spryker\Zed\User\UserConfig $userConfig
85
     * @param list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPostSavePluginInterface> $userPostSavePlugins
86
     * @param list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPreSavePluginInterface> $userPreSavePlugins
87
     * @param list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserTransferExpanderPluginInterface> $userTransferExpanderPlugins
88
     * @param list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserExpanderPluginInterface> $userExpanderPlugins
89
     * @param list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPostCreatePluginInterface> $userPostCreatePlugins
90
     * @param list<\Spryker\Zed\UserExtension\Dependency\Plugin\UserPostUpdatePluginInterface> $userPostUpdatePlugins
91
     */
92
    public function __construct(
93
        UserQueryContainerInterface $queryContainer,
94
        SessionClientInterface $session,
95
        UserConfig $userConfig,
96
        array $userPostSavePlugins = [],
97
        array $userPreSavePlugins = [],
98
        array $userTransferExpanderPlugins = [],
99
        array $userExpanderPlugins = [],
100
        array $userPostCreatePlugins = [],
101
        array $userPostUpdatePlugins = []
102
    ) {
103
        $this->queryContainer = $queryContainer;
104
        $this->session = $session;
105
        $this->userConfig = $userConfig;
106
        $this->userPostSavePlugins = $userPostSavePlugins;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userPostSavePlugins of type array is incompatible with the declared type Spryker\Zed\User\Business\Model\list of property $userPostSavePlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
107
        $this->userPreSavePlugins = $userPreSavePlugins;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userPreSavePlugins of type array is incompatible with the declared type Spryker\Zed\User\Business\Model\list of property $userPreSavePlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
        $this->userTransferExpanderPlugins = $userTransferExpanderPlugins;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userTransferExpanderPlugins of type array is incompatible with the declared type Spryker\Zed\User\Business\Model\list of property $userTransferExpanderPlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
109
        $this->userExpanderPlugins = $userExpanderPlugins;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userExpanderPlugins of type array is incompatible with the declared type Spryker\Zed\User\Business\Model\list of property $userExpanderPlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
        $this->userPostCreatePlugins = $userPostCreatePlugins;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userPostCreatePlugins of type array is incompatible with the declared type Spryker\Zed\User\Business\Model\list of property $userPostCreatePlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
        $this->userPostUpdatePlugins = $userPostUpdatePlugins;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userPostUpdatePlugins of type array is incompatible with the declared type Spryker\Zed\User\Business\Model\list of property $userPostUpdatePlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
112
    }
113
114
    /**
115
     * @param string $firstName
116
     * @param string $lastName
117
     * @param string $username
118
     * @param string $password
119
     *
120
     * @throws \Spryker\Zed\User\Business\Exception\UsernameExistsException
121
     *
122
     * @return \Generated\Shared\Transfer\UserTransfer
123
     */
124
    public function addUser($firstName, $lastName, $username, $password)
125
    {
126
        $userCheck = $this->hasUserByUsername($username);
127
128
        if ($userCheck === true) {
129
            throw new UsernameExistsException();
130
        }
131
132
        $transferUser = new UserTransfer();
133
        $transferUser->setFirstName($firstName);
134
        $transferUser->setLastName($lastName);
135
        $transferUser->setUsername($username);
136
        $transferUser->setPassword($password);
137
138
        return $this->save($transferUser);
139
    }
140
141
    /**
142
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
143
     *
144
     * @throws \Spryker\Zed\User\Business\Exception\UsernameExistsException
145
     *
146
     * @return \Generated\Shared\Transfer\UserTransfer
147
     */
148
    public function createUser(UserTransfer $userTransfer): UserTransfer
149
    {
150
        $userCheck = $this->hasUserByUsername($userTransfer->getUsernameOrFail());
151
152
        if ($userCheck === true) {
153
            throw new UsernameExistsException(
154
                sprintf('Username %s already exist.', $userTransfer->getUsername()),
155
            );
156
        }
157
158
        return $this->handleUserCreateTransaction($userTransfer);
159
    }
160
161
    /**
162
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
163
     *
164
     * @return \Generated\Shared\Transfer\UserTransfer
165
     */
166
    public function updateUser(UserTransfer $userTransfer): UserTransfer
167
    {
168
        return $this->getTransactionHandler()->handleTransaction(function () use ($userTransfer) {
169
            return $this->executeUserUpdateTransaction($userTransfer);
170
        });
171
    }
172
173
    /**
174
     * @param string $password
175
     *
176
     * @return string|false
177
     */
178
    public function encryptPassword($password)
179
    {
180
        return password_hash($password, PASSWORD_BCRYPT);
181
    }
182
183
    /**
184
     * @param string $password
185
     * @param string $hash
186
     *
187
     * @return bool
188
     */
189
    public function validatePassword($password, $hash)
190
    {
191
        return password_verify($password, $hash);
192
    }
193
194
    /**
195
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
196
     *
197
     * @return \Generated\Shared\Transfer\UserTransfer
198
     */
199
    public function save(UserTransfer $userTransfer)
200
    {
201
        return $this->getTransactionHandler()->handleTransaction(function () use ($userTransfer): UserTransfer {
202
            return $this->executeSaveTransaction($userTransfer);
203
        });
204
    }
205
206
    /**
207
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
208
     *
209
     * @throws \Spryker\Zed\User\Business\Exception\PasswordEncryptionFailedException
210
     *
211
     * @return \Generated\Shared\Transfer\UserTransfer
212
     */
213
    protected function executeSaveTransaction(UserTransfer $userTransfer): UserTransfer
214
    {
215
        if ($userTransfer->getIdUser() !== null) {
216
            $userEntity = $this->getEntityUserById($userTransfer->getIdUser());
217
        } else {
218
            $userEntity = new SpyUser();
219
        }
220
221
        $userTransfer = $this->executePreSavePlugins($userTransfer);
222
        $modifiedUser = $userTransfer->modifiedToArray();
223
224
        unset($modifiedUser[UserTransfer::PASSWORD]);
225
226
        $userEntity->fromArray($modifiedUser);
227
228
        $password = $userTransfer->getPassword();
229
        if ($password && $this->isRawPassword($password)) {
230
            $passwordEncrypted = $this->encryptPassword($password);
231
            if ($passwordEncrypted === false) {
0 ignored issues
show
introduced by
The condition $passwordEncrypted === false is always false.
Loading history...
232
                throw new PasswordEncryptionFailedException();
233
            }
234
235
            $userEntity->setPassword($passwordEncrypted);
236
        }
237
238
        $userEntity->save();
239
        $userTransfer = $this->entityToTransfer(
240
            $userEntity,
241
            (new UserTransfer())->fromArray($userTransfer->toArray()),
242
        );
243
        $userTransfer = $this->executePostSavePlugins($userTransfer);
244
245
        return $userTransfer;
246
    }
247
248
    /**
249
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
250
     *
251
     * @return \Generated\Shared\Transfer\UserTransfer
252
     */
253
    protected function executePreSavePlugins(UserTransfer $userTransfer): UserTransfer
254
    {
255
        foreach ($this->userPreSavePlugins as $preSavePlugin) {
256
            $userTransfer = $preSavePlugin->preSave($userTransfer);
257
        }
258
259
        return $userTransfer;
260
    }
261
262
    /**
263
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
264
     *
265
     * @return \Generated\Shared\Transfer\UserTransfer
266
     */
267
    protected function executePostSavePlugins(UserTransfer $userTransfer): UserTransfer
268
    {
269
        foreach ($this->userPostSavePlugins as $postSavePlugin) {
270
            $userTransfer = $postSavePlugin->postSave($userTransfer);
271
        }
272
273
        return $userTransfer;
274
    }
275
276
    /**
277
     * @param int $idUser
278
     *
279
     * @return \Generated\Shared\Transfer\UserTransfer
280
     */
281
    public function removeUser($idUser)
282
    {
283
        $userTransfer = (new UserTransfer())
284
            ->setIdUser($idUser)
285
            ->setStatus('deleted');
286
287
        $userTransfer = $this->save($userTransfer);
288
289
        return $this->executeUserPostUpdatePlugins($userTransfer);
290
    }
291
292
    /**
293
     * @param string $password
294
     *
295
     * @return bool
296
     */
297
    private function isRawPassword($password)
298
    {
299
        $passwordInfo = password_get_info($password);
300
301
        return $passwordInfo['algoName'] === 'unknown';
302
    }
303
304
    /**
305
     * @param string $username
306
     *
307
     * @return bool
308
     */
309
    public function hasUserByUsername($username)
310
    {
311
        $amount = $this->queryContainer->queryUserByUsername($username)->count();
312
313
        return $amount > 0;
314
    }
315
316
    /**
317
     * @param string $username
318
     *
319
     * @return bool
320
     */
321
    public function hasActiveUserByUsername($username)
322
    {
323
        $amount = $this->queryContainer->queryUserByUsername($username)
324
            ->filterByStatus(SpyUserTableMap::COL_STATUS_ACTIVE)->count();
325
326
        return $amount > 0;
327
    }
328
329
    /**
330
     * @param int $idUser
331
     *
332
     * @return bool
333
     */
334
    public function hasUserById($idUser)
335
    {
336
        $amount = $this->queryContainer->queryUserById($idUser)->count();
337
338
        return $amount > 0;
339
    }
340
341
    /**
342
     * @deprecated Use {@link \Spryker\Zed\User\Business\Reader\UserReader::getUserCollection()} instead.
343
     *
344
     * @param string $username
345
     *
346
     * @throws \Spryker\Zed\User\Business\Exception\UserNotFoundException
347
     *
348
     * @return \Generated\Shared\Transfer\UserTransfer
349
     */
350
    public function getUserByUsername($username)
351
    {
352
        $entity = $this->queryContainer->queryUserByUsername($username)->findOne();
353
354
        if ($entity === null) {
355
            throw new UserNotFoundException();
356
        }
357
358
        return $this->entityToTransfer($entity);
359
    }
360
361
    /**
362
     * @deprecated Use {@link \Spryker\Zed\User\Business\Reader\UserReader::getUserCollection()} instead.
363
     *
364
     * @param int $id
365
     *
366
     * @throws \Spryker\Zed\User\Business\Exception\UserNotFoundException
367
     *
368
     * @return \Generated\Shared\Transfer\UserTransfer
369
     */
370
    public function getUserById($id)
371
    {
372
        $entity = $this->queryContainer
373
            ->queryUserById($id)
374
            ->findOne();
375
376
        if ($entity === null) {
377
            throw new UserNotFoundException();
378
        }
379
380
        return $this->entityToTransfer($entity);
381
    }
382
383
    /**
384
     * @deprecated Use {@link \Spryker\Zed\User\Business\Model\User::findUser()} instead.
385
     *
386
     * @param int $id
387
     *
388
     * @return \Generated\Shared\Transfer\UserTransfer|null
389
     */
390
    public function findUserById(int $id): ?UserTransfer
391
    {
392
        return $this->findUserByIdUser($id);
393
    }
394
395
    /**
396
     * @deprecated Use {@link \Spryker\Zed\User\Business\Reader\UserReader::getUserCollection()} instead.
397
     *
398
     * @param \Generated\Shared\Transfer\UserCriteriaTransfer $userCriteriaTransfer
399
     *
400
     * @return \Generated\Shared\Transfer\UserTransfer|null
401
     */
402
    public function findUser(UserCriteriaTransfer $userCriteriaTransfer): ?UserTransfer
403
    {
404
        if ($userCriteriaTransfer->getIdUser() !== null) {
405
            return $this->findUserByIdUser($userCriteriaTransfer->getIdUser());
406
        }
407
408
        if ($userCriteriaTransfer->getEmail() !== null) {
409
            return $this->findUserByEmail($userCriteriaTransfer->getEmail());
410
        }
411
412
        return null;
413
    }
414
415
    /**
416
     * @param int $idUser
417
     *
418
     * @return \Generated\Shared\Transfer\UserTransfer|null
419
     */
420
    protected function findUserByIdUser(int $idUser): ?UserTransfer
421
    {
422
        $userEntity = $this->queryContainer
423
            ->queryUserById($idUser)
424
            ->findOne();
425
426
        if (!$userEntity) {
427
            return null;
428
        }
429
430
        return $this->entityToTransfer($userEntity);
431
    }
432
433
    /**
434
     * @param string $email
435
     *
436
     * @return \Generated\Shared\Transfer\UserTransfer|null
437
     */
438
    protected function findUserByEmail(string $email): ?UserTransfer
439
    {
440
        $userEntity = $this->queryContainer
441
            ->queryUserByUsername($email)
442
            ->findOne();
443
444
        if (!$userEntity) {
445
            return null;
446
        }
447
448
        return $this->entityToTransfer($userEntity);
449
    }
450
451
    /**
452
     * @deprecated Use {@link \Spryker\Zed\User\Business\Reader\UserReader::getUserCollection()} instead.
453
     *
454
     * @param int $id
455
     *
456
     * @throws \Spryker\Zed\User\Business\Exception\UserNotFoundException
457
     *
458
     * @return \Generated\Shared\Transfer\UserTransfer
459
     */
460
    public function getActiveUserById($id)
461
    {
462
        $entity = $this->queryContainer
463
            ->queryUserById($id)
464
            ->filterByStatus(SpyUserTableMap::COL_STATUS_ACTIVE)
465
            ->findOne();
466
467
        if ($entity === null) {
468
            throw new UserNotFoundException();
469
        }
470
471
        return $this->entityToTransfer($entity);
472
    }
473
474
    /**
475
     * @param int $id
476
     *
477
     * @throws \Spryker\Zed\User\Business\Exception\UserNotFoundException
478
     *
479
     * @return \Orm\Zed\User\Persistence\SpyUser
480
     */
481
    public function getEntityUserById($id)
482
    {
483
        $entity = $this->queryContainer->queryUserById($id)->findOne();
484
485
        if ($entity === null) {
486
            throw new UserNotFoundException();
487
        }
488
489
        return $entity;
490
    }
491
492
    /**
493
     * @param \Generated\Shared\Transfer\UserTransfer $user
494
     *
495
     * @return mixed
496
     */
497
    public function setCurrentUser(UserTransfer $user)
498
    {
499
        $key = $this->createUserKey();
500
501
        return $this->session->set($key, clone $user);
502
    }
503
504
    /**
505
     * @return bool
506
     */
507
    public function hasCurrentUser()
508
    {
509
        $user = $this->readUserFromSession();
510
511
        return $user !== null;
512
    }
513
514
    /**
515
     * @return \Generated\Shared\Transfer\UserTransfer|null
516
     */
517
    protected function readUserFromSession()
518
    {
519
        $key = $this->createUserKey();
520
521
        if (!$this->session->has($key)) {
522
            return null;
523
        }
524
525
        return $this->session->get($key);
526
    }
527
528
    /**
529
     * @param \Generated\Shared\Transfer\UserTransfer $user
530
     *
531
     * @return bool
532
     */
533
    public function isSystemUser(UserTransfer $user)
534
    {
535
        $systemUsers = $this->userConfig->getSystemUsers();
536
537
        return in_array($user->getUsername(), $systemUsers);
538
    }
539
540
    /**
541
     * @return \Generated\Shared\Transfer\CollectionTransfer
542
     */
543
    public function getSystemUsers()
544
    {
545
        $systemUser = $this->userConfig->getSystemUsers();
546
        $collection = new CollectionTransfer();
547
548
        foreach ($systemUser as $username) {
549
            $transferUser = new UserTransfer();
550
551
            // TODO why setting the id? why is everything the username?
552
            $transferUser->setIdUser(0);
553
554
            $transferUser->setFirstName($username)
555
                ->setLastName($username)
556
                ->setUsername($username)
557
                ->setPassword($username);
558
559
            $collection->addUser($transferUser);
560
        }
561
562
        return $collection;
563
    }
564
565
    /**
566
     * @throws \Spryker\Zed\User\Business\Exception\UserNotFoundException
567
     *
568
     * @return \Generated\Shared\Transfer\UserTransfer
569
     */
570
    public function getCurrentUser()
571
    {
572
        $user = $this->readUserFromSession();
573
574
        if ($user === null) {
575
            throw new UserNotFoundException();
576
        }
577
578
        return clone $user;
579
    }
580
581
    /**
582
     * @param \Orm\Zed\User\Persistence\SpyUser $userEntity
583
     * @param \Generated\Shared\Transfer\UserTransfer|null $userTransfer
584
     *
585
     * @return \Generated\Shared\Transfer\UserTransfer
586
     */
587
    protected function entityToTransfer(SpyUser $userEntity, ?UserTransfer $userTransfer = null)
588
    {
589
        if ($userTransfer === null) {
590
            $userTransfer = new UserTransfer();
591
        }
592
593
        $userTransfer->fromArray($userEntity->toArray(), true);
594
595
        $userTransfer = $this->executeUserExpanderPlugins($userTransfer);
596
        $userTransfer = $this->executeUserTransferExpanderPlugins($userTransfer);
597
598
        return $userTransfer;
599
    }
600
601
    /**
602
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
603
     *
604
     * @return \Generated\Shared\Transfer\UserTransfer
605
     */
606
    protected function executeUserExpanderPlugins(UserTransfer $userTransfer): UserTransfer
607
    {
608
        $userCollectionTransfer = (new UserCollectionTransfer())->addUser($userTransfer);
609
        foreach ($this->userExpanderPlugins as $userExpanderPlugin) {
610
            $userCollectionTransfer = $userExpanderPlugin->expand($userCollectionTransfer);
611
        }
612
613
        return $userCollectionTransfer->getUsers()->getIterator()->current();
614
    }
615
616
    /**
617
     * @deprecated Use {@link \Spryker\Zed\User\Business\Model\User::executeUserExpanderPlugins()} instead.
618
     *
619
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
620
     *
621
     * @return \Generated\Shared\Transfer\UserTransfer
622
     */
623
    protected function executeUserTransferExpanderPlugins(UserTransfer $userTransfer): UserTransfer
624
    {
625
        foreach ($this->userTransferExpanderPlugins as $userTransferExpanderPlugin) {
626
            $userTransfer = $userTransferExpanderPlugin->expandUserTransfer($userTransfer);
627
        }
628
629
        return $userTransfer;
630
    }
631
632
    /**
633
     * @param int $idUser
634
     *
635
     * @return bool
636
     */
637
    public function activateUser($idUser)
638
    {
639
        return $this->updateUserStatus($idUser, SpyUserTableMap::COL_STATUS_ACTIVE);
640
    }
641
642
    /**
643
     * @param int $idUser
644
     *
645
     * @return bool
646
     */
647
    public function deactivateUser($idUser)
648
    {
649
        return $this->updateUserStatus($idUser, SpyUserTableMap::COL_STATUS_BLOCKED);
650
    }
651
652
    /**
653
     * @param int $idUser
654
     * @param string $status
655
     *
656
     * @return bool
657
     */
658
    protected function updateUserStatus(int $idUser, string $status): bool
659
    {
660
        $userEntity = $this->queryUserById($idUser);
661
        $userEntity->setStatus($status);
662
        $rowsAffected = $userEntity->save();
663
        $userTransfer = $this->entityToTransfer($userEntity);
664
665
        if ($this->userConfig->isPostSavePluginsEnabledAfterUserStatusChange()) {
666
            $this->executePostSavePlugins($userTransfer);
667
        }
668
669
        $this->executeUserPostUpdatePlugins($userTransfer);
670
671
        return $rowsAffected > 0;
672
    }
673
674
    /**
675
     * @param int $idUser
676
     *
677
     * @throws \Spryker\Zed\User\Business\Exception\UserNotFoundException
678
     *
679
     * @return \Orm\Zed\User\Persistence\SpyUser
680
     */
681
    protected function queryUserById(int $idUser): SpyUser
682
    {
683
        $userEntity = $this->queryContainer->queryUserById($idUser)->findOne();
684
        if (!$userEntity) {
685
            throw new UserNotFoundException();
686
        }
687
688
        return $userEntity;
689
    }
690
691
    /**
692
     * @return string
693
     */
694
    protected function createUserKey()
695
    {
696
        return sprintf('%s:currentUser', static::USER_BUNDLE_SESSION_KEY);
697
    }
698
699
    /**
700
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
701
     *
702
     * @return \Generated\Shared\Transfer\UserTransfer
703
     */
704
    protected function handleUserCreateTransaction(UserTransfer $userTransfer): UserTransfer
705
    {
706
        return $this->getTransactionHandler()->handleTransaction(function () use ($userTransfer) {
707
            $userTransfer = $this->executeSaveTransaction($userTransfer);
708
709
            return $this->executePostCreateTransaction($userTransfer);
710
        });
711
    }
712
713
    /**
714
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
715
     *
716
     * @return \Generated\Shared\Transfer\UserTransfer
717
     */
718
    protected function executePostCreateTransaction(UserTransfer $userTransfer): UserTransfer
719
    {
720
        $userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer);
721
        $userCollectionResponseTransfer = $this->executeUserPostCreatePlugins($userCollectionResponseTransfer);
722
723
        return $userCollectionResponseTransfer->getUsers()->offsetGet(0);
724
    }
725
726
    /**
727
     * @param \Generated\Shared\Transfer\UserCollectionResponseTransfer $userCollectionResponseTransfer
728
     *
729
     * @return \Generated\Shared\Transfer\UserCollectionResponseTransfer
730
     */
731
    protected function executeUserPostCreatePlugins(
732
        UserCollectionResponseTransfer $userCollectionResponseTransfer
733
    ): UserCollectionResponseTransfer {
734
        foreach ($this->userPostCreatePlugins as $userPostCreatePlugin) {
735
            $userCollectionResponseTransfer = $userPostCreatePlugin->postCreate($userCollectionResponseTransfer);
736
        }
737
738
        return $userCollectionResponseTransfer;
739
    }
740
741
    /**
742
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
743
     *
744
     * @return \Generated\Shared\Transfer\UserTransfer
745
     */
746
    protected function executeUserUpdateTransaction(UserTransfer $userTransfer): UserTransfer
747
    {
748
        $userTransfer = $this->executeSaveTransaction($userTransfer);
749
750
        return $this->executeUserPostUpdatePlugins($userTransfer);
751
    }
752
753
    /**
754
     * @param \Generated\Shared\Transfer\UserTransfer $userTransfer
755
     *
756
     * @return \Generated\Shared\Transfer\UserTransfer
757
     */
758
    protected function executeUserPostUpdatePlugins(UserTransfer $userTransfer): UserTransfer
759
    {
760
        $userCollectionResponseTransfer = (new UserCollectionResponseTransfer())->addUser($userTransfer);
761
762
        foreach ($this->userPostUpdatePlugins as $userPostUpdatePlugin) {
763
            $userCollectionResponseTransfer = $userPostUpdatePlugin->postUpdate($userCollectionResponseTransfer);
764
        }
765
766
        return $userCollectionResponseTransfer->getUsers()->getIterator()->current();
767
    }
768
}
769