JobMarshaler::marshal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Disque\Queue\Marshal;
3
4
use Disque\Queue\Job;
5
use Disque\Queue\JobInterface;
6
7
/**
8
 * Serialize and deserialize the job body
9
 *
10
 * Serialize the job body when adding the job to the queue,
11
 * deserialize it and instantiate a new Job object when reading the job
12
 * from the queue.
13
 *
14
 * This marshaler uses JSON serialization for the whole Job body.
15
 */
16
class JobMarshaler implements MarshalerInterface
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function unmarshal($source)
22
    {
23
        $body = json_decode($source, true);
24
        if (is_null($body)) {
25
            throw new MarshalException("Could not deserialize {$source}");
26
        }
27
        return new Job($body);
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function marshal(JobInterface $job)
34
    {
35
        return json_encode($job->getBody());
36
    }
37
}
38