Repository navigation
Expand file tree
/
Copy pathFsProducer.php
More file actions
130 lines (109 loc) · 3.03 KB
/
Copy pathFsProducer.php
File metadata and controls
130 lines (109 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Enqueue\Fs;
use Interop\Queue\DeliveryDelayNotSupportedException;
use Interop\Queue\InvalidDestinationException;
use Interop\Queue\InvalidMessageException;
use Interop\Queue\PriorityNotSupportedException;
use Interop\Queue\PsrDestination;
use Interop\Queue\PsrMessage;
use Interop\Queue\PsrProducer;
use Makasim\File\TempFile;
class FsProducer implements PsrProducer
{
/**
* @var float|int|null
*/
private $timeToLive;
/**
* @var FsContext
*/
private $context;
/**
* @param FsContext $context
*/
public function __construct(FsContext $context)
{
$this->context = $context;
}
/**
* {@inheritdoc}
*
* @param FsDestination $destination
* @param FsMessage $message
*/
public function send(PsrDestination $destination, PsrMessage $message)
{
InvalidDestinationException::assertDestinationInstanceOf($destination, FsDestination::class);
InvalidMessageException::assertMessageInstanceOf($message, FsMessage::class);
$this->context->workWithFile($destination, 'a+', function (FsDestination $destination, $file) use ($message) {
$fileInfo = $destination->getFileInfo();
if ($fileInfo instanceof TempFile && false == file_exists($fileInfo)) {
return;
}
if (null !== $this->timeToLive) {
$message->setHeader('x-expire-at', microtime(true) + ($this->timeToLive / 1000));
}
$rawMessage = json_encode($message);
$rawMessage = str_replace('|{', '\|\{', $rawMessage);
$rawMessage = '|'.$rawMessage;
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf(
'Could not encode value into json. Error %s and message %s',
json_last_error(),
json_last_error_msg()
));
}
$rawMessage = str_repeat(' ', 64 - (strlen($rawMessage) % 64)).$rawMessage;
fwrite($file, $rawMessage);
});
}
/**
* {@inheritdoc}
*/
public function setDeliveryDelay($deliveryDelay)
{
if (null === $deliveryDelay) {
return;
}
throw DeliveryDelayNotSupportedException::providerDoestNotSupportIt();
}
/**
* {@inheritdoc}
*/
public function getDeliveryDelay()
{
return null;
}
/**
* {@inheritdoc}
*/
public function setPriority($priority)
{
if (null === $priority) {
return;
}
throw PriorityNotSupportedException::providerDoestNotSupportIt();
}
/**
* {@inheritdoc}
*/
public function getPriority()
{
return null;
}
/**
* {@inheritdoc}
*/
public function setTimeToLive($timeToLive)
{
$this->timeToLive = $timeToLive;
return $this;
}
/**
* {@inheritdoc}
*/
public function getTimeToLive()
{
return null;
}
}