/
JobMarshaler.php
38 lines (35 loc) · 1 KB
/
JobMarshaler.php
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
<?php
namespace Disque\Queue\Marshal;
use Disque\Queue\Job;
use Disque\Queue\JobInterface;
class JobMarshaler implements MarshalerInterface
{
/**
* Creates a JobInterface instance based on data obtained from queue
*
* @param string $source Source data
* @return JobInterface
* @throws MarshalException
*/
public function unmarshal($source)
{
$body = @json_decode($source, true);
if (is_null($body)) {
throw new MarshalException("Could not deserialize {$source}");
}
return new Job($body);
}
/**
* Marshals the body of the job ready to be put into the queue
*
* @param JobInterface $job Job to put in the queue
* @return string Source data to be put in the queue
*/
public function marshal(JobInterface $job)
{
if (!($job instanceof Job)) {
throw new MarshalException(get_class($job) . ' is not a ' . Job::class);
}
return json_encode($job->getBody());
}
}