Completed
Push — master ( 2c43ba...36c6fb )
by Bernhard
07:34
created

Serializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
nc 1
cc 1
eloc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/key-value-store package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\KeyValueStore\Util;
13
14
use Exception;
15
use Webmozart\KeyValueStore\Api\SerializationFailedException;
16
use Webmozart\KeyValueStore\Api\UnserializationFailedException;
17
18
/**
19
 * Wrapper for `serialize()`/`unserialize()` that throws proper exceptions.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 */
25
final class Serializer
26
{
27
    /**
28
     * Serializes a value.
29
     *
30
     * @param mixed $value The value to serialize.
31
     *
32
     * @return string The serialized value.
33
     *
34
     * @throws SerializationFailedException If the value cannot be serialized.
35
     */
36 249
    public static function serialize($value)
37
    {
38 249
        if (is_resource($value)) {
39 6
            throw SerializationFailedException::forValue($value);
40
        }
41
42
        try {
43 243
            $serialized = serialize($value);
44 6
        } catch (Exception $e) {
45 6
            throw SerializationFailedException::forValue($value, $e->getMessage(), $e->getCode(), $e);
46
        }
47
48 237
        return $serialized;
49
    }
50
51
    /**
52
     * Unserializes a value.
53
     *
54
     * @param mixed $serialized The serialized value.
55
     *
56
     * @return string The unserialized value.
57
     *
58
     * @throws UnserializationFailedException If the value cannot be unserialized.
59
     */
60 170
    public static function unserialize($serialized)
61
    {
62 170
        if (!is_string($serialized)) {
63 1
            throw UnserializationFailedException::forValue($serialized);
64
        }
65
66 169
        $errorMessage = null;
67 169
        $errorCode = 0;
68
69 169
        set_error_handler(function ($errno, $errstr) use (&$errorMessage, &$errorCode) {
70 21
            $errorMessage = $errstr;
71 21
            $errorCode = $errno;
72 169
        });
73
74 169
        $value = unserialize($serialized);
75
76 169
        restore_error_handler();
77
78 169
        if (null !== $errorMessage) {
79 21
            if (false !== $pos = strpos($errorMessage, '): ')) {
80
                // cut "unserialize(%path%):" to make message more readable
81 21
                $errorMessage = substr($errorMessage, $pos + 3);
82
            }
83
84 21
            throw UnserializationFailedException::forValue($serialized, $errorMessage, $errorCode);
85
        }
86
87 148
        return $value;
88
    }
89
90
    private function __construct()
91
    {
92
    }
93
}
94