Completed
Pull Request — master (#19)
by Łukasz
07:12
created

SendEmailAfterExecution::composeMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 1
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Check;
4
5
use Swift_Message;
6
use Twig\Node\Expression\Binary\StartsWithBinary;
7
use Tworzenieweb\SqlProvisioner\Config\Config;
8
use Tworzenieweb\SqlProvisioner\Database\Connection;
9
use Tworzenieweb\SqlProvisioner\Model\Candidate;
10
use Tworzenieweb\SqlProvisioner\Service\Mailer;
11
use Tworzenieweb\SqlProvisioner\View\View;
12
13
class SendEmailAfterExecution implements CheckInterface
14
{
15
    /** @var Mailer */
16
    private $mailer;
17
18
    /** @var Config */
19
    private $config;
20
21
    /** @var View */
22
    private $view;
23
24
    /** @var Connection */
25
    private $connection;
26
27
28
29
    public function __construct(Mailer $mailer, Config $config, View $view, Connection $connection)
30
    {
31
        $this->mailer = $mailer;
32
        $this->config = $config;
33
        $this->view = $view;
34
        $this->connection = $connection;
35
    }
36
37
38
39
    public function execute(Candidate $candidate): bool
40
    {
41
        $message = $this->composeMessage($candidate);
42
        $mailsSent = $this->mailer->send($message);
43
44
        return $mailsSent > 0;
45
    }
46
47
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function getErrorCode(): string
53
    {
54
        return '';
55
    }
56
57
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function getLastErrorMessage(): string
63
    {
64
        return '';
65
    }
66
67
68
69
    private function composeMessage(Candidate $candidate): Swift_Message
70
    {
71
        $subject = $this->config->getEmailSubject();
72
        $fromEmail = $this->config->getFromEmail();
73
        $fromName = $this->config->getFromName();
74
        $toEmails = $this->config->getRecipientsList();
75
        $serverHost = $this->config->getServerHost();
76
        $sql = $candidate->getContent();
77
        $filename = $candidate->getName();
78
        $message = new Swift_Message($subject);
79
        $message->setFrom([$fromEmail => $fromName]);
80
        $message->setTo($toEmails);
81
        $dbName = $this->connection->getDatabaseName();
82
        $body = $this->view->render(['serverHost' => $serverHost, 'sql' => $sql, 'filename' => $filename, 'dbName' => $dbName]);
83
        $message->setBody($body, 'text/html');
84
85
        return $message;
86
    }
87
}
88