Passed
Pull Request — master (#27)
by Florian
03:25
created

EmailService::sendEmailToAdministrator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the vseth-semesterly-reports project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Service;
13
14
use App\Service\Interfaces\EmailServiceInterface;
15
use Swift_Mailer;
16
use Swift_Message;
17
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
18
19
class EmailService implements EmailServiceInterface
20
{
21
    /**
22
     * @var Swift_Mailer
23
     */
24
    private $mailer;
25
26
    /**
27
     * @var string
28
     */
29
    private $replyEmail;
30
31
    /**
32
     * EmailService constructor.
33
     */
34
    public function __construct(Swift_Mailer $mailer, ParameterBagInterface $parameterBag)
35
    {
36
        $this->mailer = $mailer;
37
        $this->replyEmail = $parameterBag->get('REPLY_EMAIL');
38
    }
39
40
    /**
41
     * @param string[] $options
42
     *
43
     * @return bool
44
     */
45
    public function sendEmail(string $receiver, string $subject, string $body)
46
    {
47
        $message = (new Swift_Message())
48
            ->setSubject($subject)
49
            ->setFrom($this->replyEmail)
50
            ->setReplyTo($this->replyEmail)
51
            ->setTo($receiver)
52
            ->setBody($body);
53
54
        //send message & check if at least one receiver was reached
55
        return $this->mailer->send($message) > 0;
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function sendEmailToAdministrator(string $subject, string $body)
62
    {
63
        return $this->sendEmail($this->replyEmail, $subject, $body);
64
    }
65
}
66