Passed
Push — master ( 394926...dbe3b4 )
by Daniel
05:14
created

UserSubcriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\EventSubscriber\EntitySubscriber;
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Event\LifecycleEventArgs;
9
use Doctrine\ORM\Event\PreUpdateEventArgs;
10
use Doctrine\ORM\Events;
11
use Silverback\ApiComponentBundle\Entity\User\User;
12
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
13
14
/**
15
 * @author Daniel West <[email protected]>
16
 */
17
class UserSubcriber implements EntitySubscriberInterface
18
{
19
    private $passwordEncoder;
20
21
    public function __construct(
22
        UserPasswordEncoderInterface $passwordEncoder
23
    ) {
24
        $this->passwordEncoder = $passwordEncoder;
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function getSubscribedEvents(): array
31
    {
32
        return [
33
            Events::prePersist => 'prePersist',
34
            Events::preUpdate => 'preUpdate'
35
        ];
36
    }
37
38
    public function supportsEntity($entity = null): bool
39
    {
40
        return $entity instanceof User;
41
    }
42
43
    public function prePersist(LifecycleEventArgs $eventArgs, User $entity): void
44
    {
45
        $this->prePersistUpdate($eventArgs->getEntityManager(), $entity);
46
    }
47
48
    public function preUpdate(PreUpdateEventArgs $eventArgs, User $entity): void
49
    {
50
        $this->prePersistUpdate($eventArgs->getEntityManager(), $entity);
51
    }
52
53
    public function prePersistUpdate(EntityManager $em, User $entity): void
0 ignored issues
show
Unused Code introduced by
The parameter $em is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function prePersistUpdate(/** @scrutinizer ignore-unused */ EntityManager $em, User $entity): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        if ($entity->getPlainPassword()) {
56
            $password = $this->passwordEncoder->encodePassword($entity, $entity->getPlainPassword());
57
            $entity->setPassword($password);
58
        }
59
    }
60
}
61