• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / docker-symfony-api / #74

pending completion
#74

push

DKravtsov
Php 8.2, symfony 6.2, updated RabbitMQ, updated composer dependencies, refactoring.

51 of 51 new or added lines in 44 files covered. (100.0%)

1479 of 2668 relevant lines covered (55.43%)

23.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.25
/src/User/Transport/EventListener/UserEntityEventListener.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\User\Transport\EventListener;
6

7
use App\User\Application\Security\SecurityUser;
8
use App\User\Domain\Entity\User;
9
use Doctrine\Persistence\Event\LifecycleEventArgs;
10
use LengthException;
11
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
12

13
use function strlen;
14

15
/**
16
 * Class UserEntityEventListener
17
 *
18
 * @package App\User
19
 */
20
class UserEntityEventListener
21
{
22
    public function __construct(
23
        private readonly UserPasswordHasherInterface $userPasswordHasher,
24
    ) {
25
    }
141✔
26

27
    /**
28
     * Doctrine lifecycle event for 'prePersist' event.
29
     */
30
    public function prePersist(LifecycleEventArgs $event): void
31
    {
32
        $this->process($event);
141✔
33
    }
34

35
    /**
36
     * Doctrine lifecycle event for 'preUpdate' event.
37
     */
38
    public function preUpdate(LifecycleEventArgs $event): void
39
    {
40
        $this->process($event);
14✔
41
    }
42

43
    /**
44
     * @throws LengthException
45
     */
46
    private function process(LifecycleEventArgs $event): void
47
    {
48
        // Get user entity object
49
        $user = $event->getObject();
141✔
50

51
        // Valid user so lets change password
52
        if ($user instanceof User) {
141✔
53
            $this->changePassword($user);
10✔
54
        }
55
    }
56

57
    /**
58
     * Method to change user password whenever it's needed.
59
     *
60
     * @throws LengthException
61
     */
62
    private function changePassword(User $user): void
63
    {
64
        // Get plain password from user entity
65
        $plainPassword = $user->getPlainPassword();
10✔
66

67
        // Yeah, we have new plain password set, so we need to encode it
68
        if ($plainPassword !== '') {
10✔
69
            if (strlen($plainPassword) < User::PASSWORD_MIN_LENGTH) {
4✔
70
                throw new LengthException(
×
71
                    'Too short password, should be at least ' . User::PASSWORD_MIN_LENGTH . ' symbols'
×
72
                );
×
73
            }
74

75
            // Password hash callback
76
            $callback = fn (string $plainPassword): string => $this->userPasswordHasher
4✔
77
                ->hashPassword(new SecurityUser($user, []), $plainPassword);
4✔
78
            // Set new password and encode it with user encoder
79
            $user->setPassword($callback, $plainPassword);
4✔
80
            // And clean up plain password from entity
81
            $user->eraseCredentials();
4✔
82
        }
83
    }
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc