SymfonySerializer::deserialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * GpsLab component.
6
 *
7
 * @author    Peter Gribanov <[email protected]>
8
 * @copyright Copyright (c) 2011, Peter Gribanov
9
 * @license   http://opensource.org/licenses/MIT
10
 */
11
12
namespace GpsLab\Component\Command\Queue\Serializer;
13
14
use GpsLab\Component\Command\Command;
15
use Symfony\Component\Serializer\SerializerInterface;
16
17
class SymfonySerializer implements Serializer
18
{
19
    public const DEFAULT_FORMAT = 'predis';
20
21
    /**
22
     * @var SerializerInterface
23
     */
24
    private $serializer;
25
26
    /**
27
     * @var string
28
     */
29
    private $format;
30
31
    /**
32
     * @param SerializerInterface $serializer
33
     * @param string              $format
34
     */
35 6
    public function __construct(SerializerInterface $serializer, string $format = self::DEFAULT_FORMAT)
36
    {
37 6
        $this->serializer = $serializer;
38 6
        $this->format = $format;
39 6
    }
40
41
    /**
42
     * @param object $data
43
     *
44
     * @return string
45
     */
46 2
    public function serialize($data): string
47
    {
48 2
        return $this->serializer->serialize($data, $this->format);
49
    }
50
51
    /**
52
     * @param string $data
53
     *
54
     * @return object
55
     */
56 4
    public function deserialize(string $data)
57
    {
58 4
        $result = $this->serializer->deserialize($data, Command::class, $this->format);
59
60 4
        if (!is_object($result)) {
61 2
            throw new \RuntimeException(sprintf('Failed deserialize data "%s"', $data));
62
        }
63
64 2
        return $result;
65
    }
66
}
67