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
|
3 |
|
public function __construct(string $directory, int $mode = 0750) |
31
|
|
|
{ |
32
|
3 |
|
$this->directory = $directory; |
33
|
3 |
|
$this->mode = $mode; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param MessageInterface $message |
39
|
|
|
*/ |
40
|
3 |
|
public function store(MessageInterface $message): void |
41
|
|
|
{ |
42
|
3 |
|
$messageString = (string)$message; |
43
|
3 |
|
$filename = hash('sha256', $messageString) . '.eml'; |
44
|
|
|
|
45
|
3 |
|
file_put_contents( |
46
|
3 |
|
$this->directory . '/' . $filename, |
47
|
3 |
|
$messageString |
48
|
|
|
); |
49
|
|
|
|
50
|
3 |
|
chmod($this->directory . '/' . $filename, $this->mode); |
51
|
3 |
|
} |
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
|
|
|
public function count(): int |
74
|
|
|
{ |
75
|
|
|
return (new \GlobIterator($this->directory . '/*.eml'))->count(); |
76
|
|
|
} |
77
|
|
|
} |