EmailController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 19
c 1
b 0
f 0
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 9 1
A __construct() 0 4 1
A send() 0 14 1
1
<?php
2
3
namespace ProjetNormandie\EmailBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\Request;
8
use ProjetNormandie\EmailBundle\Service\Mailer;
9
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
10
use Symfony\Contracts\Translation\TranslatorInterface;
11
12
/**
13
 * Controller used to manage email contents in the public part of the site.
14
 */
15
class EmailController extends AbstractController
16
{
17
18
    private Mailer $mailer;
19
    private TranslatorInterface $translator;
20
21
    /**
22
     * EmailController constructor.
23
     * @param Mailer              $mailer
24
     * @param TranslatorInterface $translator
25
     */
26
    public function __construct(Mailer $mailer, TranslatorInterface $translator)
27
    {
28
        $this->mailer = $mailer;
29
        $this->translator = $translator;
30
    }
31
32
    /**
33
     * @param Request $request
34
     * @return Response
35
     * @throws TransportExceptionInterface
36
     */
37
    public function send(Request $request): Response
38
    {
39
        $data = json_decode($request->getContent(), true);
40
41
        $this->mailer->send(
42
            $data['subject'],
43
            $data['message'],
44
            $data['email'],
45
            null
46
47
        );
48
49
        return $this->getResponse(
50
            $this->translator->trans('email.success')
51
        );
52
    }
53
54
    /**
55
     * @param string|null $message
56
     * @return Response
57
     */
58
    private function getResponse(string $message = null): Response
59
    {
60
        $response = new Response();
61
        $response->headers->set('Content-Type', 'application/json');
62
        $response->setContent(json_encode([
63
            'success' => true,
64
            'message' => $message,
65
        ]));
66
        return $response;
67
    }
68
}
69