Confirmation::getVerifyModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace DoS\UserBundle\Confirmation\Email;
4
5
use DoS\UserBundle\Confirmation\ConfirmationAbstract;
6
use DoS\UserBundle\Confirmation\ConfirmationSubjectInterface;
7
use DoS\UserBundle\Confirmation\Exception\NotFoundChannelException;
8
use DoS\UserBundle\Confirmation\Model\EmailResend;
9
use DoS\UserBundle\Confirmation\Model\EmailVerification;
10
use DoS\UserBundle\Confirmation\Model\VerificationInterface;
11
use Symfony\Component\Form\FormError;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
class Confirmation extends ConfirmationAbstract
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function sendToken(ConfirmationSubjectInterface $subject, $token)
21
    {
22
        $email = $subject->getConfirmationChannel($this->options['channel_path']);
23
24
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
25
            throw new NotFoundChannelException();
26
        }
27
28
        $this->sender->send(
29
            $this->options['token_send_template'],
30
            array($email),
31
            array('subject' => $subject, 'token' => $token)
32
        );
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function verify(Request $request, $token)
39
    {
40
        $form = $this->createVerifyForm();
41
        /** @var VerificationInterface $data */
42
        $data = $form->getData();
43
        $data->setToken($token);
44
        $form->submit($request->query->all());
45
46
        if (!$form->isValid()) {
47
            return $form;
48
        }
49
50
        if (!$this->validateTimeAware($subject = $data->getSubject())) {
51
            $form->addError(new FormError('ui.trans.user.confirmation.verify.invalid_time'));
52
        }
53
54
        if (!$form->getErrors(true)->count()) {
55
            $this->successVerify($subject);
56
        }
57
58
        return $form;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function createVerifyForm()
65
    {
66
        return $this->formFactory->create(
67
            $this->options['token_verify_form'],
68
            $this->getVerifyModel(),
69
            array('csrf_protection' => false)
70
        );
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getResendModel()
77
    {
78
        return new EmailResend();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getVerifyModel()
85
    {
86
        return new EmailVerification();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getType()
93
    {
94
        return 'email';
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function configureOptions(OptionsResolver $resolver)
101
    {
102
        parent::configureOptions($resolver);
103
104
        $resolver->setDefaults(array(
105
            'token_resend_form' => 'dos_resend_confirmation_email',
106
            'token_verify_form' => 'dos_verification',
107
        ));
108
    }
109
}
110