JobMarshaler::unmarshal()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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