Completed
Push — master ( 5621e4...94f5e8 )
by Lars
16:34
created

SerializerMsgpack::setUnserializeOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 null|array
19
     */
20
    private $unserialize_options;
21
22
    /**
23
     * SerializerIgbinary constructor.
24
     */
25 6
    public function __construct()
26
    {
27 6
        if (self::$_exists_msgpack === null) {
28 1
            self::$_exists_msgpack = (
29 1
                \function_exists('msgpack_pack')
30
                &&
31 1
                \function_exists('msgpack_unpack')
32
            );
33
        }
34 6
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    public function serialize($value)
40
    {
41 3
        if (self::$_exists_msgpack === true) {
42
            /** @noinspection PhpUndefinedFunctionInspection */
43
            /** @noinspection PhpComposerExtensionStubsInspection */
44
            return \msgpack_pack($value);
45
        }
46
47
        // fallback
48 3
        return \serialize($value);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function unserialize($value)
55
    {
56 3
        if (self::$_exists_msgpack === true) {
57
            /** @noinspection PhpUndefinedFunctionInspection */
58
            /** @noinspection PhpComposerExtensionStubsInspection */
59
            return \msgpack_unpack($value);
60
        }
61
62
        // fallback
63 3
        if ($this->unserialize_options !== null) {
64 3
            return \unserialize($value, $this->unserialize_options);
65
        }
66
67
        /** @noinspection UnserializeExploitsInspection */
68
        return \unserialize($value);
69
    }
70
71
    /**
72
     * @param array $options
73
     */
74 6
    public function setUnserializeOptions(array $options)
75
    {
76 6
        $this->unserialize_options = $options;
77 6
    }
78
}
79