Completed
Push — master ( b96965...99f77d )
by Lars
02:12
created

SerializerIgbinary::unserialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 5
cts 6
cp 0.8333
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0416
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * SerializerIgbinary: serialize / unserialize
9
 */
10
class SerializerIgbinary implements iSerializer
11
{
12
    /**
13
     * @var bool
14
     */
15
    public static $_exists_igbinary;
16
17
    /**
18
     * @var null|array
19
     */
20
    private $unserialize_options;
21
22
    /**
23
     * SerializerIgbinary constructor.
24
     */
25 69
    public function __construct()
26
    {
27 69
        if (self::$_exists_igbinary === null) {
28
            self::$_exists_igbinary = (
29 1
                \function_exists('igbinary_serialize')
30
                &&
31 1
                \function_exists('igbinary_unserialize')
32
            );
33
        }
34 69
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 33
    public function serialize($value)
40
    {
41 33
        if (self::$_exists_igbinary === true) {
42
            /** @noinspection PhpUndefinedFunctionInspection */
43
            /** @noinspection PhpComposerExtensionStubsInspection */
44
            return \igbinary_serialize($value);
45
        }
46
47
        // fallback
48 33
        return \serialize($value);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 26
    public function unserialize($value)
55
    {
56 26
        if (self::$_exists_igbinary === true) {
57
            /** @noinspection PhpUndefinedFunctionInspection */
58
            /** @noinspection PhpComposerExtensionStubsInspection */
59
            return \igbinary_unserialize($value);
60
        }
61
62
        // fallback
63 26
        if ($this->unserialize_options !== null) {
64 13
            return \unserialize($value, $this->unserialize_options);
65
        }
66
67
        /** @noinspection UnserializeExploitsInspection */
68 13
        return \unserialize($value);
69
    }
70
71
    /**
72
     * @param array $options
73
     */
74 47
    public function setUnserializeOptions(array $options)
75
    {
76 47
        $this->unserialize_options = $options;
77 47
    }
78
}
79