IgbinarySerializer::serialize()   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 1
crap 1
1
<?php
2
3
namespace Tasksuki\Component\IgbinarySerializer;
4
5
use RuntimeException;
6
use Tasksuki\Component\Message\Message;
7
use Tasksuki\Component\Serializer\SerializerInterface;
8
use Tasksuki\Component\Serializer\Exception\NotValidInputException;
9
10
/**
11
 * Class IgbinarySerializer
12
 *
13
 * @package Tasksuki\Component\IgbinarySerializer
14
 * @author  Aurimas Niekis <[email protected]>
15
 */
16
class IgbinarySerializer implements SerializerInterface
17
{
18
    /**
19
     * @codeCoverageIgnore
20
     */
21
    public function __construct()
22
    {
23
        if (false === extension_loaded('igbinary')) {
24
            throw new RuntimeException('Extension `igbinary` is missing');
25
        }
26
    }
27
28
    /**
29
     * @param Message $message
30
     *
31
     * @return string
32
     */
33 1
    public function serialize(Message $message): string
34
    {
35 1
        return igbinary_serialize($message);
36
    }
37
38
    /**
39
     * @param string $data
40
     *
41
     * @return Message
42
     * @throws NotValidInputException
43
     */
44 2
    public function unserialize(string $data): Message
45
    {
46 2
        $message = @igbinary_unserialize($data);
47
48 2
        if (false === ($message instanceof Message)) {
49 1
            throw new NotValidInputException();
50
        }
51
52 1
        return $message;
53
    }
54
55
}