SendEmailAfterExecution::composeMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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