UserListener   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 76
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updateUserFields() 0 4 1
A recomputeChangeSet() 0 12 3
A getSubscribedEvents() 0 5 1
A prePersist() 0 5 2
A __construct() 0 4 1
A preUpdate() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the DoyoUserBundle project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\UserBundle\Bridge\ORM;
15
16
use Doctrine\Common\EventSubscriber;
17
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use Doctrine\ODM\MongoDB\DocumentManager;
1 ignored issue
show
Bug introduced by
The type Doctrine\ODM\MongoDB\DocumentManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Doctrine\ORM\EntityManagerInterface;
21
use Doctrine\ORM\Events;
22
use Doyo\UserBundle\Model\UserInterface;
23
use Doyo\UserBundle\Util\CanonicalFieldsUpdaterInterface;
24
use Doyo\UserBundle\Util\PasswordUpdaterInterface;
25
26
class UserListener implements EventSubscriber
27
{
28
    /**
29
     * @var PasswordUpdaterInterface
30
     */
31
    private $passwordUpdater;
32
33
    /**
34
     * @var CanonicalFieldsUpdaterInterface
35
     */
36
    private $canonicalFieldsUpdater;
37
38
    /**
39
     * UserListener constructor.
40
     */
41 21
    public function __construct(PasswordUpdaterInterface $passwordUpdater, CanonicalFieldsUpdaterInterface $canonicalFieldsUpdater)
42
    {
43 21
        $this->passwordUpdater        = $passwordUpdater;
44 21
        $this->canonicalFieldsUpdater = $canonicalFieldsUpdater;
45
    }
46
47 16
    public function getSubscribedEvents()
48
    {
49
        return [
50 16
            Events::prePersist,
51 16
            Events::preUpdate,
52
        ];
53
    }
54
55
    /**
56
     * Pre persist listener based on doctrine common.
57
     */
58 11
    public function prePersist(LifecycleEventArgs $args)
59
    {
60 11
        $object = $args->getObject();
61 11
        if ($object instanceof UserInterface) {
62 7
            $this->updateUserFields($object);
63
        }
64
    }
65
66
    /**
67
     * Pre update listener based on doctrine common.
68
     */
69 13
    public function preUpdate(LifecycleEventArgs $args)
70
    {
71 13
        $object = $args->getObject();
72 13
        if ($object instanceof UserInterface) {
73 13
            $this->updateUserFields($object);
74 13
            $this->recomputeChangeSet($args->getObjectManager(), $object);
75
        }
76
    }
77
78
    /**
79
     * Updates the user properties.
80
     */
81 14
    private function updateUserFields(UserInterface $user)
82
    {
83 14
        $this->canonicalFieldsUpdater->updateCanonicalFields($user);
84 14
        $this->passwordUpdater->hashPassword($user);
85
    }
86
87
    /**
88
     * Recomputes change set for Doctrine implementations not doing it automatically after the event.
89
     */
90 13
    private function recomputeChangeSet(ObjectManager $om, UserInterface $user)
91
    {
92 13
        $meta = $om->getClassMetadata(\get_class($user));
93
94 13
        if ($om instanceof EntityManagerInterface) {
95 11
            $om->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $user);
96
97 11
            return;
98
        }
99
100 2
        if ($om instanceof DocumentManager) {
101
            $om->getUnitOfWork()->recomputeSingleDocumentChangeSet($meta, $user);
1 ignored issue
show
Bug introduced by
The method getUnitOfWork() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

101
            $om->/** @scrutinizer ignore-call */ 
102
                 getUnitOfWork()->recomputeSingleDocumentChangeSet($meta, $user);
Loading history...
102
        }
103
    }
104
}
105