EmailUpdateListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 10
ccs 0
cts 6
cp 0
crap 2
1
<?php
2
3
namespace Azine\EmailUpdateConfirmationBundle\Doctrine;
4
5
use Azine\EmailUpdateConfirmationBundle\Mailer\EmailUpdateConfirmationMailerInterface;
6
use Azine\EmailUpdateConfirmationBundle\Services\EmailUpdateConfirmation;
7
use Doctrine\ORM\Event\PreUpdateEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PreUpdateEventArgs 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...
8
use FOS\UserBundle\Model\UserInterface;
9
use FOS\UserBundle\Util\CanonicalFieldsUpdater;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
12
/**
13
 * Class EmailUpdateListener.
14
 */
15
class EmailUpdateListener
16
{
17
    /**
18
     * @var EmailUpdateConfirmation
19
     */
20
    private $emailUpdateConfirmation;
21
    /**
22
     * @var RequestStack
23
     */
24
    private $requestStack;
25
    /**
26
     * @var CanonicalFieldsUpdater
27
     */
28
    private $canonicalFieldsUpdater;
29
    /**
30
     * @var EmailUpdateConfirmationMailerInterface
31
     */
32
    private $mailer;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param EmailUpdateConfirmation                $emailUpdateConfirmation
38
     * @param RequestStack                           $requestStack
39
     * @param CanonicalFieldsUpdater                 $canonicalFieldsUpdater
40
     * @param EmailUpdateConfirmationMailerInterface $mailer
41
     */
42
    public function __construct(EmailUpdateConfirmation $emailUpdateConfirmation, RequestStack $requestStack, CanonicalFieldsUpdater $canonicalFieldsUpdater, EmailUpdateConfirmationMailerInterface $mailer)
43
    {
44
        $this->emailUpdateConfirmation = $emailUpdateConfirmation;
45
        $this->requestStack = $requestStack;
46
        $this->canonicalFieldsUpdater = $canonicalFieldsUpdater;
47
        $this->mailer = $mailer;
48
    }
49
50
    /**
51
     * Pre update listener based on doctrine common.
52
     *
53
     * @param PreUpdateEventArgs $args
54
     */
55
    public function preUpdate(PreUpdateEventArgs $args)
56
    {
57
        $object = $args->getObject();
58
        if ($object instanceof UserInterface) {
59
            $user = $object;
60
            if ($user->getConfirmationToken() != $this->emailUpdateConfirmation->getEmailConfirmedToken() && isset($args->getEntityChangeSet()['email'])) {
61
                $oldEmail = $args->getEntityChangeSet()['email'][0];
62
                $newEmail = $args->getEntityChangeSet()['email'][1];
63
                $user->setEmail($oldEmail);
64
                $user->setEmailCanonical($this->canonicalFieldsUpdater->canonicalizeEmail($oldEmail));
65
66
                $this->mailer->sendUpdateEmailConfirmation(
67
                    $user,
68
                    $this->emailUpdateConfirmation->generateConfirmationLink($this->requestStack->getCurrentRequest(), $user, $newEmail),
0 ignored issues
show
Bug introduced by
It seems like $this->requestStack->getCurrentRequest() can also be of type null; however, parameter $request of Azine\EmailUpdateConfirm...erateConfirmationLink() does only seem to accept Symfony\Component\HttpFoundation\Request, maybe add an additional type check? ( Ignorable by Annotation )

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

68
                    $this->emailUpdateConfirmation->generateConfirmationLink(/** @scrutinizer ignore-type */ $this->requestStack->getCurrentRequest(), $user, $newEmail),
Loading history...
69
                    $newEmail
70
                );
71
            }
72
73
            if ($user->getConfirmationToken() == $this->emailUpdateConfirmation->getEmailConfirmedToken()) {
74
                $user->setConfirmationToken(null);
75
            }
76
        }
77
    }
78
}
79