GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 569a9d...5a02aa )
by Pavel
03:21
created

Mail::getTemplateFile()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 37
rs 8.8571
cc 3
eloc 16
nc 3
nop 0
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\Mailing;
10
11
use Nette;
12
use Ublaboo;
13
use Ublaboo\Mailing\Exception\MailingException;
14
15
abstract class Mail extends Nette\Object
16
{
17
18
	const CONFIG_LOG  = 'log';
19
	const CONFIG_SEND = 'send';
20
	const CONFIG_BOTH = 'both';
21
22
23
	/**
24
	 * @var string
25
	 */
26
	private $config;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $mails;
32
33
	/**
34
	 * @var Nette\Mail\IMailer
35
	 */
36
	protected $mailer;
37
38
	/**
39
	 * @var Nette\Mail\Message
40
	 */
41
	protected $message;
42
43
	/**
44
	 * @var array
45
	 */
46
	protected $args;
47
48
	/**
49
	 * @var Nette\Application\LinkGenerator
50
	 */
51
	protected $linkGenerator;
52
53
	/**
54
	 * @var ILogger
55
	 */
56
	protected $logger;
57
58
	/**
59
	 * @var Nette\Application\UI\ITemplate
60
	 */
61
	protected $template;
62
63
	/**
64
	 * @var string
65
	 */
66
	protected $underscore_name;
67
68
	/**
69
	 * @var string
70
	 */
71
	protected $log_type;
72
73
	/**
74
	 * @var string
75
	 */
76
	protected $mail_images_base_path;
77
78
	/**
79
	 * @var string
80
	 */
81
	protected $template_file;
82
83
84
	public function __construct(
85
		$config,
86
		$mails,
87
		Nette\Mail\IMailer $mailer,
88
		Nette\Mail\Message $message,
89
		Nette\Application\LinkGenerator $linkGenerator,
90
		Nette\Application\UI\ITemplateFactory $templateFactory,
91
		ILogger $logger,
92
		$args
93
	) {
94
		$this->config = $config;
95
		$this->mails = $mails;
96
		$this->mailer = $mailer;
97
		$this->message = $message;
98
		$this->linkGenerator = $linkGenerator;
99
		$this->logger = $logger;
100
		$this->args = $args;
101
102
		$this->template = $templateFactory->createTemplate();
103
104
		/**
105
		 * Initiate mail composing
106
		 */
107
		if ($this instanceof IComposableMail) {
108
			$this->compose($this->message, $this->args);
0 ignored issues
show
Documentation Bug introduced by
The method compose does not exist on object<Ublaboo\Mailing\Mail>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
109
		}
110
	}
111
112
113
	/**
114
	 * Set template file
115
	 * @return void
116
	 */
117
	public function setTemplateFile($template_file)
118
	{
119
		$this->template_file = (string) $template_file;
120
	}
121
122
123
	/**
124
	 * Set template variables
125
	 * @return void
126
	 */
127
	protected function setTemplateVariables()
128
	{
129
		foreach ($this->args as $key => $value) {
130
			$this->template->$key = $value;
131
		}
132
	}
133
134
135
	/**
136
	 * Set absolute base path for images
137
	 * @param string $mail_images_base_path
138
	 */
139
	public function setBasePath($mail_images_base_path)
140
	{
141
		$this->mail_images_base_path = (string) $mail_images_base_path;
142
143
		return $this;
144
	}
145
146
147
	/**
148
	 * Stick to convention that Email:
149
	 * 		?/mailing/Mails/FooMail.php	
150
	 * 
151
	 * will have template with path of:
152
	 * 		?/mailing/Mails/templates/foo_mail.latte
153
	 * 
154
	 * @return string
155
	 */
156
	public function getTemplateFile()
157
	{
158
		if ($this->template_file) {
159
			return $this->template_file;
160
		}
161
162
		/**
163
		 * Get child class file path
164
		 * @var \ReflectionClass
165
		 */
166
		$reflection = new \ReflectionClass(get_class($this));
167
		
168
		/**
169
		 * Split path to directory and file
170
		 */
171
		$class_path = $reflection->getFileName();
172
		$class_dir = dirname($class_path);
173
		$class_name = pathinfo($class_path, PATHINFO_FILENAME);
174
175
		/**
176
		 * Convert class name to underscore and set latte file extension
177
		 */
178
		$this->underscore_name = lcfirst(preg_replace_callback('/(?<=.)([A-Z])/', function ($m) {
179
			return '_' . strtolower($m[1]);
180
		}, $class_name));
181
182
		$template_name = $this->underscore_name . '.latte';
183
		$this->log_type = $this->underscore_name;
184
185
		$template_file = "$class_dir/templates/$template_name";
186
187
		if (!file_exists($template_file)) {
188
			throw new MailingException("Error creating template from file [$template_file]", 1);
189
		}
190
191
		return $template_file;
192
	}
193
194
195
	/**
196
	 * Render latte template to string and send (and/or log) mail
197
	 * @return void
198
	 */
199
	public function send()
200
	{
201
		/**
202
		 * Set template variables
203
		 */
204
		$this->setTemplateVariables();
205
206
		/**
207
		 * Set body/html body
208
		 */
209
		try {
210
			$this->template->setFile($this->getTemplateFile());
211
			$this->template->_control = $this->linkGenerator;
0 ignored issues
show
Bug introduced by
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...
212
213
			$this->message->setHtmlBody((string) $this->template, $this->mail_images_base_path);
214
		} catch (MailingException $e) {
215
			/**
216
			 * If mail template was set and not found, bubble exception up
217
			 */
218
			if ($this->template_file) {
219
				throw $e;
220
			}
221
			/**
222
			 * Otherwise just suppose that user has set message body via ::setBody
223
			 */
224
		}
225
226
		/**
227
		 * In case mail sending in on, send message
228
		 */
229
		if ($this->config === self::CONFIG_BOTH || $this->config === self::CONFIG_SEND) {
230
			$this->mailer->send($this->message);
231
		}
232
233
		/**
234
		 * In case mail logging is turned on, log message
235
		 */
236
		if ($this->config === self::CONFIG_LOG || $this->config === self::CONFIG_BOTH) {
237
			$this->logger->log($this->log_type, $this->message);
238
		}
239
	}
240
241
}
242