Completed
Push — master ( 94f5e8...a88b9e )
by Lars
01:57 queued 11s
created

SerializerMsgpack::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 3
cts 4
cp 0.75
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * SerializerMsgpack: serialize / unserialize
9
 */
10 View Code Duplication
class SerializerMsgpack implements iSerializer
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * @var bool
14
     */
15
    public static $_exists_msgpack;
16
17
    /**
18
     * @var array|null
19
     */
20
    private $unserialize_options;
21
22
    /**
23
     * @var string
24
     */
25
    private $name = '';
26
27
    /**
28
     * SerializerIgbinary constructor.
29
     */
30 6
    public function __construct()
31
    {
32 6
        if (self::$_exists_msgpack === null) {
33
            self::$_exists_msgpack = (
34 1
                \function_exists('msgpack_pack')
35
                &&
36 1
                \function_exists('msgpack_unpack')
37
            );
38
        }
39
40 6
        if (self::$_exists_msgpack) {
41
            $this->name = 'msgpack';
42
        } else {
43 6
            $this->name = 'default';
44
        }
45 6
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getName(): string
51
    {
52
        return $this->name;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function serialize($value)
59
    {
60 3
        if (self::$_exists_msgpack === true) {
61
            /** @noinspection PhpUndefinedFunctionInspection */
62
            return \msgpack_pack($value);
63
        }
64
65
        // fallback
66 3
        return \serialize($value);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 3
    public function unserialize($value)
73
    {
74 3
        if (self::$_exists_msgpack === true) {
75
            /** @noinspection PhpUndefinedFunctionInspection */
76
            return \msgpack_unpack($value);
77
        }
78
79
        // fallback
80 3
        if ($this->unserialize_options !== null) {
81 3
            return \unserialize($value, $this->unserialize_options);
82
        }
83
84
        /** @noinspection UnserializeExploitsInspection */
85
        return \unserialize($value);
86
    }
87
88
    /**
89
     * @param array $options
90
     */
91 6
    public function setUnserializeOptions(array $options)
92
    {
93 6
        $this->unserialize_options = $options;
94 6
    }
95
}
96