Issues (3)

src/Mailer/FileMailer.php (3 issues)

1
<?php declare(strict_types=1);
2
3
namespace h4kuna\MailManager\Mailer;
4
5
use Nette\Mail;
6
use Nette\Utils;
7
8
/**
9
 * File Mailer - store mail to server uploads (file)
10
 * @todo https://github.com/romanmatyus/FileMailer
11
 * @author David Grudl
12
 */
13
class FileMailer implements Mail\IMailer
0 ignored issues
show
Deprecated Code introduced by
The interface Nette\Mail\IMailer has been deprecated: use Nette\Mail\Mailer ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

13
class FileMailer implements /** @scrutinizer ignore-deprecated */ Mail\IMailer

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
14
{
15
16
	/** @var string */
17
	private $path;
18
19
	/** @var string|null */
20
	private $live;
21
22
	/** @var string */
23
	private $lastFile;
24
25
26
	public function __construct(string $path)
27
	{
28
		Utils\FileSystem::createDir($path);
29
		$this->path = realpath($path) . DIRECTORY_SEPARATOR;
30
	}
31
32
33
	public function setLive(string $live): void
34
	{
35
		$this->live = $live;
36
	}
37
38
39
	public function getLastFile(): string
40
	{
41
		return $this->lastFile;
42
	}
43
44
45
	public function send(Mail\Message $mail): void
46
	{
47
		$this->autoremove();
48
		list($sec) = explode(' ', substr(microtime(), 2));
49
		$this->lastFile = $this->path . date('Y-m-d_H-i-s-') . $sec . '.eml';
50
		file_put_contents($this->lastFile, $mail->generateMessage());
51
	}
52
53
54
	private function autoremove(): void
55
	{
56
		if (!$this->live) {
57
			return;
58
		}
59
		$finder = Utils\Finder::findFiles('*.eml');
60
		if (is_string($this->live)) {
0 ignored issues
show
The condition is_string($this->live) is always true.
Loading history...
61
			$finder->date('<=', "- {$this->live}");
62
		}
63
64
		foreach ($finder->in($this->path) as $file) {
65
			@unlink($file->getPathname());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

65
			/** @scrutinizer ignore-unhandled */ @unlink($file->getPathname());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
66
		}
67
		$this->live = null; // clear temporary onetime per execution.
68
	}
69
70
}
71