TwitterDisconnectSerializer::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Assert\Assertion;
6
use Twitter\Object\TwitterDisconnect;
7
use Twitter\TwitterSerializable;
8
use Twitter\TwitterSerializer;
9
10
class TwitterDisconnectSerializer implements TwitterSerializer
11
{
12
    /**
13
     * @param  TwitterSerializable $object
14
     * @return \stdClass
15
     */
16 6
    public function serialize(TwitterSerializable $object)
17
    {
18
        /* @var TwitterDisconnect $object */
19 6
        Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterDisconnect');
20
21 3
        $obj = new \stdClass();
22 3
        $obj->code = $object->getCode();
23 3
        $obj->stream_name = $object->getStreamName();
24 3
        $obj->reason = $object->getReason();
25
26 3
        $disconnect = new \stdClass();
27 3
        $disconnect->disconnect = $obj;
28
29 3
        return $disconnect;
30
    }
31
32
    /**
33
     * @param  \stdClass $obj
34
     * @param  array     $context
35
     * @return TwitterDisconnect
36
     */
37 6
    public function unserialize($obj, array $context = [])
38
    {
39 6
        Assertion::true($this->canUnserialize($obj), 'object is not unserializable');
40
41 3
        $d = $obj->disconnect;
42
43 3
        return TwitterDisconnect::create(
44 3
            $d->code,
45 3
            $d->stream_name,
46 3
            $d->reason
47 2
        );
48
    }
49
50
    /**
51
     * @param  TwitterSerializable $object
52
     * @return boolean
53
     */
54 6
    public function canSerialize(TwitterSerializable $object)
55
    {
56 6
        return $object instanceof TwitterDisconnect;
57
    }
58
59
    /**
60
     * @param  \stdClass $object
61
     * @return boolean
62
     */
63 6
    public function canUnserialize($object)
64
    {
65 6
        return (isset($object->disconnect));
66
    }
67
68
    /**
69
     * @return TwitterDisconnectSerializer
70
     */
71 6
    public static function build()
72
    {
73 6
        return new self();
74
    }
75
}
76