SendMail   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 25 2
A __construct() 0 4 1
1
<?php
2
3
namespace App\Mail;
4
5
use Swift_Image;
6
use Swift_Mailer;
7
use Swift_Message;
8
use Symfony\Component\Templating\EngineInterface;
9
10
class SendMail
11
{
12
13
    /**
14
     * @var Swift_Mailer
15
     */
16
    private $mailer;
17
    /**
18
     * @var EngineInterface
19
     */
20
    private $templating;
21
22
    public function __construct(Swift_Mailer $mailer, EngineInterface $templating)
23
    {
24
        $this->mailer = $mailer;
25
        $this->templating = $templating;
26
    }
27
28
    public function send(string $subject, string $template, $mailData, string $to, String $from = null): bool
29
    {
30
        if ($from === null) {
31
            $from = getenv('ADMIN_EMAIL');
32
        }
33
34
        $message = (new Swift_Message($subject))
35
            ->setFrom($from)
36
            ->setTo($to)
37
        ;
38
        $img = $message->embed(Swift_Image::fromPath('img/snowtricks-logo.png'));
39
        $message
40
            ->setBody(
41
                $this->templating->render(
42
                    $template,
43
                    [
44
                        'mailData' => $mailData,
45
                        'img' =>$img,
46
                        'subject' => $subject,
47
                    ]
48
                ),
49
                'text/html'
50
            );
51
52
        return $this->mailer->send($message) > 0;
53
54
    }
55
56
}