Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

src/Kunstmaan/FormBundle/Helper/FormMailer.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FormBundle\Helper;
4
5
use Kunstmaan\FormBundle\Entity\FormSubmission;
6
use Swift_Mailer;
7
use Swift_Message;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
use Symfony\Component\Templating\EngineInterface;
11
use Twig\Environment;
12
13
/**
14
 * The form mailer
15
 */
16
class FormMailer implements FormMailerInterface
17
{
18
    /** @var \Swift_Mailer */
19
    private $mailer;
20
21
    /** @var EngineInterface|Environment */
22
    private $twig;
23
24
    /** @var RequestStack */
25
    private $requestStack;
26
27
    /**
28
     * @param Swift_Mailer                    $mailer
29
     * @param EngineInterface                 $twig
30
     * @param ContainerInterface|RequestStack $requestStack
31
     */
32 3
    public function __construct(Swift_Mailer $mailer, /*Environment*/ $twig, /*RequestStack*/ $requestStack)
33
    {
34 3
        $this->mailer = $mailer;
35 3
        $this->twig = $twig;
36
37 3
        if ($twig instanceof EngineInterface) {
38 1
            @trigger_error('Passing the "@templating" service as the 2nd argument is deprecated since KunstmaanFormBundle 5.4 and will be replaced by the Twig service in KunstmaanFormBundle 6.0. Injected the "@twig" service instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
        }
40
41 3
        $this->requestStack = $requestStack;
42 3
        if ($requestStack instanceof ContainerInterface) {
43 2
            @trigger_error('Passing the container as the 3th argument is deprecated since KunstmaanFormBundle 5.4 and will be replaced by the "request_stack" service in KunstmaanFormBundle 6.0. Injected the "@request_stack" service instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
45 2
            $this->requestStack = $requestStack->get('request_stack');
46
        }
47 3
    }
48
49
    /**
50
     * @param FormSubmission $submission The submission
51
     * @param string         $from       The from address
52
     * @param string         $to         The to address(es) seperated by \n
53
     * @param string         $subject    The subject
54
     */
55 1
    public function sendContactMail(FormSubmission $submission, $from, $to, $subject)
56
    {
57 1
        $request = $this->requestStack->getCurrentRequest();
58
59 1
        $toArr = explode("\r\n", $to);
60
61 1
        $message = (new Swift_Message($subject))
62 1
            ->setFrom($from)
63 1
            ->setTo($toArr)
64 1
            ->setBody(
65 1
                $this->twig->render(
66 1
                    '@KunstmaanForm/Mailer/mail.html.twig',
67
                    [
68 1
                        'submission' => $submission,
69 1
                        'host' => $request->getScheme().'://'.$request->getHttpHost(),
70
                    ]
71
                ),
72 1
                'text/html'
73
            );
74 1
        $this->mailer->send($message);
75 1
    }
76
}
77