Completed
Push — master ( 93b50d...02fb34 )
by Dominik
10:08 queued 03:07
created

EmailUpdateListener::preUpdate()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 21
cp 0
rs 9.2408
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
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;
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 be null; however, generateConfirmationLink() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69
                    $newEmail
70
                );
71
            }
72
73
            if ($user->getConfirmationToken() == $this->emailUpdateConfirmation->getEmailConfirmedToken()) {
74
                $user->setConfirmationToken(null);
75
            }
76
        }
77
    }
78
}
79