Completed
Push — master ( cc7816...e04f3c )
by Pavel
01:48
created

src/AbstractMail.php (1 issue)

Labels
Severity

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
declare(strict_types=1);
4
5
/**
6
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
7
 * @author      Pavel Janda <[email protected]>
8
 * @package     Ublaboo
9
 */
10
11
namespace Ublaboo\Mailing;
12
13
use Latte\Engine;
14
use Nette\Application\LinkGenerator;
15
use Nette\Application\UI\ITemplate;
16
use Nette\Application\UI\ITemplateFactory;
17
use Nette\Bridges\ApplicationLatte\Template;
18
use Nette\Mail\IMailer;
19
use Nette\Mail\Message;
20
use Ublaboo\Mailing\DI\MailingExtension;
21
22
abstract class AbstractMail
23
{
24
25
	/**
26
	 * @var array
27
	 */
28
	protected $mailAddresses;
29
30
	/**
31
	 * @var IMailer
32
	 */
33
	protected $mailer;
34
35
	/**
36
	 * @var Message
37
	 */
38
	protected $message;
39
40
	/**
41
	 * @var LinkGenerator
42
	 */
43
	protected $linkGenerator;
44
45
	/**
46
	 * @var ILogger
47
	 */
48
	protected $logger;
49
50
	/**
51
	 * @var ITemplate
52
	 */
53
	protected $template;
54
55
	/**
56
	 * @var string
57
	 */
58
	protected $mailImagesBasePath;
59
60
	/**
61
	 * @var string
62
	 */
63
	private $config;
64
65
	/**
66
	 * @var IMailData|null
67
	 */
68
	private $mailData;
69
70
71
	public function __construct(
72
		string $config,
73
		array $mailAddresses,
74
		IMailer $mailer,
75
		Message $message,
76
		LinkGenerator $linkGenerator,
77
		ITemplateFactory $templateFactory,
78
		ILogger $logger,
79
		?IMailData $mailData
80
	) {
81
		$this->config = $config;
82
		$this->mailAddresses = $mailAddresses;
83
		$this->mailer = $mailer;
84
		$this->message = $message;
85
		$this->linkGenerator = $linkGenerator;
86
		$this->logger = $logger;
87
		$this->mailData = $mailData;
88
89
		$this->template = $templateFactory->createTemplate();
90
91
		/**
92
		 * Initiate mail composing
93
		 */
94
		if ($this instanceof IComposableMail) {
95
			$this->compose($this->message, $this->mailData);
0 ignored issues
show
The method compose() does not seem to exist on object<Ublaboo\Mailing\AbstractMail>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
		}
97
	}
98
99
100
	public function setBasePath(string $mailImagesBasePath): void
101
	{
102
		$this->mailImagesBasePath = $mailImagesBasePath;
103
	}
104
105
	
106
	/**
107
	 * Render latte template to string and send (and/or log) mail
108
	 */
109
	public function send(): void
110
	{
111
		/**
112
		 * Template variables..
113
		 */
114
		$this->template->mailData = $this->mailData;
115
116
		/**
117
		 * Stick to convention that Email:
118
		 * 		/FooMail.php	
119
		 * 
120
		 * will have template with path of:
121
		 * 		/templates/FooMail.latte
122
		 * 
123
		 * @return string
124
		 */
125
		$templateName = (new \ReflectionClass($this))->getShortName();
126
127
		$this->template->setFile(sprintf('%s/templates/%s', __DIR__, $templateName));
128
129
		/**
130
		 * Set body/html body
131
		 */
132
		if (version_compare(Engine::VERSION, '2.4.0', '>=')) {
133
			$this->template->getLatte()->addProvider('uiControl', $this->linkGenerator);
134
		} else {
135
			$this->template->_control = $this->linkGenerator;
136
		}
137
138
		$this->message->setHtmlBody((string) $this->template, $this->mailImagesBasePath);
139
140
		/**
141
		 * In case mail sending in on, send message
142
		 */
143
		if ($this->config === MailingExtension::CONFIG_BOTH || $this->config === MailingExtension::CONFIG_SEND) {
144
			$this->mailer->send($this->message);
145
		}
146
147
		/**
148
		 * In case mail logging is turned on, log message
149
		 */
150
		if ($this->config === MailingExtension::CONFIG_LOG || $this->config === MailingExtension::CONFIG_BOTH) {
151
			$this->logger->log($templateName, $this->message);
152
		}
153
	}
154
}
155