MailFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 97
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A createByType() 0 33 3
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 Nette\Application\LinkGenerator;
14
use Nette\Application\UI\ITemplateFactory;
15
use Nette\Mail\IMailer;
16
use Nette\Mail\Message;
17
use Ublaboo\Mailing\Exception\MailingMailCreationException;
18
19
class MailFactory
20
{
21
22
	/**
23
	 * @var string
24
	 */
25
	private $config;
26
27
	/**
28
	 * @var IMailer
29
	 */
30
	private $mailer;
31
32
	/**
33
	 * @var Message
34
	 */
35
	private $message;
36
37
	/**
38
	 * @var array
39
	 */
40
	private $mails;
41
42
	/**
43
	 * @var ITemplateFactory
44
	 */
45
	private $templateFactory;
46
47
	/**
48
	 * @var LinkGenerator
49
	 */
50
	private $linkGenerator;
51
52
	/**
53
	 * @var ILogger
54
	 */
55
	private $logger;
56
57
	/**
58
	 * @var string
59
	 */
60
	private $mailImagesBasePath;
61
62
63
	public function __construct(
64
		string $config,
65
		string $mailImagesBasePath,
66
		array $mails,
67
		IMailer $mailer,
68
		LinkGenerator $linkGenerator,
69
		ITemplateFactory $templateFactory,
70
		ILogger $logger
71
	) {
72
		$this->config = $config;
73
		$this->mailImagesBasePath = $mailImagesBasePath;
74
		$this->mailer = $mailer;
75
		$this->mails = $mails;
76
		$this->linkGenerator = $linkGenerator;
77
		$this->templateFactory = $templateFactory;
78
		$this->logger = $logger;
79
	}
80
81
82
	public function createByType(string $type, ?IMessageData $mailData): IComposableMail
83
	{
84
		$this->message = new Message;
85
86
		if (class_exists($type)) {
87
			$mail = new $type(
88
				$this->config,
89
				$this->mails,
90
				$this->mailer,
91
				$this->message,
92
				$this->linkGenerator,
93
				$this->templateFactory,
94
				$this->logger,
95
				$mailData
96
			);
97
98
			$mail->setBasePath($this->mailImagesBasePath);
99
100
			if (!$mail instanceof IComposableMail) {
101
				throw new MailingMailCreationException(
102
					sprintf(
103
						'Email of type %s does not implement %s',
104
						$type,
105
						IComposableMail::class
106
					)
107
				);
108
			}
109
110
			return $mail;
111
		}
112
113
		throw new MailingMailCreationException(sprintf('Email of type %s does not exist', $type));
114
	}
115
}
116