AccountListener   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 60
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 7 1
A prePersist() 0 4 1
A preUpdate() 0 4 1
A handleEvent() 0 16 3
A fixEntity() 0 4 1
1
<?php
2
3
namespace BWC\Share\Symfony\Security\User;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\Events;
7
use Doctrine\ORM\Event\LifecycleEventArgs;
8
use Doctrine\ORM\Event\PreUpdateEventArgs;
9
10
/**
11
 * Encodes account password before saving
12
 * Must be tagged in service definition with
13
 *    - { name: doctrine.event_subscriber }
14
 */
15
class AccountListener implements EventSubscriber
16
{
17
    /** @var AccountPasswordEncoderInterface  */
18
    private $passwordEncoder;
19
20
21
    public function __construct(AccountPasswordEncoderInterface $passwordEncoder)
22
    {
23
        $this->passwordEncoder = $passwordEncoder;
24
    }
25
26
    /**
27
     * Returns an array of events this subscriber wants to listen to.
28
     *
29
     * @return array
30
     */
31
    public function getSubscribedEvents()
32
    {
33
        return array(
34
            Events::prePersist,
35
            Events::preUpdate,
36
        );
37
    }
38
39
    public function prePersist(LifecycleEventArgs $args)
40
    {
41
        $this->handleEvent($args);
42
    }
43
44
    public function preUpdate(PreUpdateEventArgs $args)
45
    {
46
        $this->handleEvent($args);
47
    }
48
49
50
51
    protected function handleEvent(LifecycleEventArgs $args)
52
    {
53
        $entity = $args->getEntity();
54
        if ($entity instanceof AdvancedUserAccountInterface) {
55
            $this->fixEntity($entity);
56
            $this->passwordEncoder->encodePassword($entity);
57
            if ($args instanceof PreUpdateEventArgs) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\Event\PreUpdateEventArgs does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
                // We are doing a update, so we must force Doctrine to update the
59
                // changeset in case we changed something above
60
                $em   = $args->getEntityManager();
61
                $uow  = $em->getUnitOfWork();
62
                $meta = $em->getClassMetadata(get_class($entity));
63
                $uow->recomputeSingleEntityChangeSet($meta, $entity);
64
            }
65
        }
66
    }
67
68
69
    protected function fixEntity($entity)
70
    {
71
72
    }
73
74
}