Completed
Pull Request — master (#33)
by Pavel
03:21
created

MailerService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 17.65%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 3
c 3
b 1
f 2
lcom 1
cbo 5
dl 0
loc 55
ccs 6
cts 34
cp 0.1765
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A sendMail() 0 19 1
A sendMailRecovery() 0 19 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: device
5
 * Date: 19.03.16
6
 * Time: 10:05
7
 */
8
9
namespace AppBundle\Services;
10
11
12
use Symfony\Bundle\TwigBundle\TwigEngine;
13
14
class MailerService
15
{
16
    private $mailer;
17
    private $templating;
18
    private $generator;
19
    private $mailerFrom;
20
21 1
    public function __construct(\Swift_Mailer $mailer, TwigEngine $template, RandomGenerator $randomGenerator, $mailerFrom)
22
    {
23 1
        $this->mailer = $mailer;
24 1
        $this->templating = $template;
25 1
        $this->generator = $randomGenerator;
26 1
        $this->mailerFrom = $mailerFrom;
27 1
    }
28
29
    public function sendMail($mailTo)
30
    {
31
        $hash = md5(uniqid());
32
        $message = \Swift_Message::newInstance()
33
            ->setSubject('Registration')
34
            ->setFrom($this->mailerFrom)
35
            ->setTo($mailTo)
36
            ->setBody(
37
                $this->templating->render(
38
                    '@App/Emails/registration.html.twig',
39
                    array('hash' => $hash, 'email' => $mailTo)
40
                ),
41
                'text/html'
42
            );
43
44
        $this->mailer->send($message);
45
46
        return $hash;
47
    }
48
49
    public function sendMailRecovery($mailTo)
50
    {
51
        $password = $this->generator->generator();
52
        $message = \Swift_Message::newInstance()
53
            ->setSubject('Registration')
54
            ->setFrom($this->mailerFrom)
55
            ->setTo($mailTo)
56
            ->setBody(
57
                $this->templating->render(
58
                    '@App/Emails/recovery.html.twig',
59
                    array('password' => $password)
60
                ),
61
                'text/html'
62
            );
63
64
        $this->mailer->send($message);
65
66
        return $password;
67
    }
68
}