Completed
Pull Request — master (#2)
by Eugene
02:26
created

EmailUpdateListener::preUpdate()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
ccs 0
cts 24
cp 0
rs 9.1928
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
                // Configure email confirmation
66
                $this->emailUpdateConfirmation->setUser($user);
67
                $this->emailUpdateConfirmation->setEmail($newEmail);
68
                $this->emailUpdateConfirmation->setConfirmationRoute('user_update_email_confirm');
69
                $this->mailer->sendUpdateEmailConfirmation(
70
                    $user,
71
                    $this->emailUpdateConfirmation->generateConfirmationLink($this->requestStack->getCurrentRequest()),
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...
72
                    $newEmail
73
                );
74
            }
75
76
            if ($user->getConfirmationToken() == $this->emailUpdateConfirmation->getEmailConfirmedToken()) {
77
                $user->setConfirmationToken(null);
78
            }
79
        }
80
    }
81
}
82