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
|
|
|
|