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

src/AbstractMail.php (2 issues)

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);
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
	 * @throws \UnexpectedValueException
110
	 */
111
	public function send(): void
112
	{
113
		/**
114
		 * Template variables..
115
		 */
116
		$this->template->mailData = $this->mailData;
0 ignored issues
show
Accessing mailData on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
117
118
		/**
119
		 * Stick to convention that Email:
120
		 * 		/FooMail.php	
121
		 * 
122
		 * will have template with path of:
123
		 * 		/templates/FooMail.latte
124
		 * 
125
		 * @return string
126
		 */
127
		$templateName = (new \ReflectionClass($this))->getShortName();
128
129
		$this->template->setFile(sprintf('%s/templates/%s', __DIR__, $templateName));
130
131
		/**
132
		 * Set body/html body
133
		 */
134
		if (version_compare(Engine::VERSION, '2.4.0', '>=')) {
135
			if (!$this->template instanceof Template) {
136
				throw new \UnexpectedValueException;
137
			}
138
139
			$this->template->getLatte()->addProvider('uiControl', $this->linkGenerator);
140
		} else {
141
			$this->template->_control = $this->linkGenerator;
0 ignored issues
show
Accessing _control on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
142
		}
143
144
		$this->message->setHtmlBody((string) $this->template, $this->mailImagesBasePath);
145
146
		/**
147
		 * In case mail sending in on, send message
148
		 */
149
		if ($this->config === MailingExtension::CONFIG_BOTH || $this->config === MailingExtension::CONFIG_SEND) {
150
			$this->mailer->send($this->message);
151
		}
152
153
		/**
154
		 * In case mail logging is turned on, log message
155
		 */
156
		if ($this->config === MailingExtension::CONFIG_LOG || $this->config === MailingExtension::CONFIG_BOTH) {
157
			$this->logger->log($templateName, $this->message);
158
		}
159
	}
160
}
161