|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Queue; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\EmptyQueueException; |
|
7
|
|
|
use Genkgo\Mail\GenericMessage; |
|
8
|
|
|
use Genkgo\Mail\MessageInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class FilesystemQueue |
|
12
|
|
|
* @package Genkgo\Mail\Queue |
|
13
|
|
|
*/ |
|
14
|
|
|
final class FilesystemQueue implements QueueInterface, \Countable |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $directory; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $mode; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* FileStorage constructor. |
|
27
|
|
|
* @param string $directory |
|
28
|
|
|
* @param int $mode |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function __construct(string $directory, int $mode = 0750) |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->directory = $directory; |
|
33
|
4 |
|
$this->mode = $mode; |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param MessageInterface $message |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function store(MessageInterface $message): void |
|
41
|
|
|
{ |
|
42
|
4 |
|
$messageString = (string)$message; |
|
43
|
4 |
|
$filename = hash('sha256', $messageString) . '.eml'; |
|
44
|
|
|
|
|
45
|
4 |
|
file_put_contents( |
|
46
|
4 |
|
$this->directory . '/' . $filename, |
|
47
|
4 |
|
$messageString |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
4 |
|
chmod($this->directory . '/' . $filename, $this->mode); |
|
51
|
4 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return MessageInterface |
|
55
|
|
|
* @throws EmptyQueueException |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function fetch(): MessageInterface |
|
58
|
|
|
{ |
|
59
|
2 |
|
$queue = new \GlobIterator($this->directory . '/*.eml'); |
|
60
|
|
|
/** @var \SplFileInfo $item */ |
|
61
|
2 |
|
foreach ($queue as $item) { |
|
62
|
2 |
|
$messageString = file_get_contents($item->getPathname()); |
|
63
|
2 |
|
unlink($item->getPathname()); |
|
64
|
2 |
|
return GenericMessage::fromString($messageString); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
throw new EmptyQueueException('No message left in queue'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function count(): int |
|
74
|
|
|
{ |
|
75
|
1 |
|
return (new \GlobIterator($this->directory . '/*.eml'))->count(); |
|
76
|
|
|
} |
|
77
|
|
|
} |