ProfileController::sendConfirmationMessage()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 16
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 1
crap 12
1
<?php
2
3
use BZIon\Form\Creator\ProfileFormCreator;
4
use Symfony\Component\HttpFoundation\RedirectResponse;
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
8
class ProfileController extends HTMLController
9 1
{
10
    public function setup()
11 1
    {
12
        $this->requireLogin();
13
    }
14
15
    /**
16
     * Edit a profile
17
     *
18
     * @param  Player  $me      The player's profile to edit
19
     * @param  Request $request
20
     * @param  bool    $self    Whether a player is editing their own profile,
21
     *                          instead of an admin editing another player's
22
     *                          profile
23
     *
24
     * @return string|Response
25
     */
26
    public function editAction(Player $me, Request $request, $self = true)
27
    {
28
        $creator = new ProfileFormCreator($me);
29
        $creator->setEditingSelf($self);
30
        $form = $creator->create()->handleRequest($request);
31
32
        if ($form->isValid()) {
33
            if (!$self && $form->has('verify_email') && $form->get('verify_email')->isClicked()) {
34
                // An admin is editing a form and has chosen to verify a
35
                // player's e-mail address
36
                $me->setVerified(true);
37
38
                // Reset the form so that the "verify email" button gets hidden
39
                $form = $creator->create()->handleRequest($request);
40
            } else {
41
                $creator->update($form, $me);
42
43
                $email = $form->get('email')->getData();
44
                if ($email !== $me->getEmailAddress()) {
45
                    // User has changed their address, send a confirmation mail
46
                    $me->setEmailAddress($email);
47
48
                    if ($self) {
49
                        $this->sendConfirmationMessage($me);
50
                    } else {
51
                        // Admins can set users' e-mail addresses at will, without
52
                        // having to send them confirmation messages
53
                        $me->setVerified(true);
54
                    }
55
                }
56
            }
57
58
            $message = ($self) ? "Your profile has been updated." : $me->getUsername() . "'s profile has been updated.";
59
            $this->getFlashBag()->add("success", $message);
60
61
            if ($form->get('enter')->isClicked()) {
62
                return (new RedirectResponse($me->getURL()));
63
            }
64
        }
65
66
        return $this->render('Profile/edit.html.twig', array(
67
            "editingSelf" => $self,
68
            "player"      => $me,
69
            "form"        => $form->createView()
70
        ));
71
    }
72
73
    /**
74
     * @todo Expire verification codes
75
     */
76
    public function confirmAction(Player $me, $token)
77
    {
78
        if (!$me->getEmailAddress()) {
79
            throw new ForbiddenException("You need to have an e-mail address to confirm!");
80
        }
81
82
        if ($me->isVerified()) {
83
            throw new ForbiddenException("You have already been verified");
84
        }
85
86
        if (!$me->isCorrectConfirmCode($token)) {
87
            throw new ForbiddenException("Invalid verification code");
88
        }
89
90
        $me->setVerified(true);
91
92
        $this->getFlashBag()->add('success', "Your e-mail address has been successfully verified");
93
94
        return new RedirectResponse($me->getUrl());
95
    }
96
97
    public function showAction(Player $me)
98
    {
99
        return new RedirectResponse($me->getUrl());
100
    }
101
102
    /**
103
     * Send a confirmation e-mail to a player
104
     * @param Player $player The receiving player
105
     */
106
    private function sendConfirmationMessage($player)
107
    {
108
        if ($player->getConfirmCode() === null) {
109
            // The player has no confirmation code, don't send them a message
110
            return;
111
        }
112
113
        $from = $this->container->getParameter('bzion.email.from');
114
        $title = $this->container->getParameter('bzion.site.name');
115
116
        if (!$from) {
117
            $this->getLogger()->addError('Unable to send verification e-mail message to player due to the "From:" address not being specified', array(
118
                'player' => array('id' => $player->getId(), 'username' => $player->getUsername())
119
            ));
120
            return;
121
        }
122
123
        $message = Swift_Message::newInstance()
124
            ->setSubject($title . ' Email Confirmation')
125
            ->setFrom(array($from => $title))
126
            ->setTo($player->getEmailAddress())
127
            ->setBody($this->render('Email/confirm.txt.twig',  array('player' => $player)))
128
            ->addPart($this->render('Email/confirm.html.twig', array('player' => $player)), 'text/html');
129
130
        $this->container->get('mailer')->send($message);
131
132
        $this->getFlashBag()->add('info',
133
            'Please check your inbox in order to confirm your email address.');
134
    }
135
}
136