Pack   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A wrap() 0 22 4
A unwrap() 0 23 5
1
<?php
2
3
namespace PhpDbaCache;
4
5
/**
6
 * Cache Serializer
7
 *
8
 * @category PhpDbaCache
9
 */
10
class Pack
11
{
12
    /**
13
     * @param          $object
14
     * @param int|bool $ltime
15
     *
16
     * @return string
17
     * @throws \RuntimeException If problems by serializing
18
     */
19
    public static function wrap($object, $ltime = false)
20
    {
21
        $fake = false;
22
23
        if (false === is_object($object)) {
24
            $object = (object)$object;
25
            $fake = true;
26
        }
27
28
        $capsule = new Capsule($fake, $ltime, $object);
29
30
        if ($object instanceof \SimpleXMLElement) {
31
            $capsule->object = $object->asXml();
32
        }
33
34
        if (false === ($res = @serialize($capsule))) {
35
            $err = error_get_last();
36
            throw new \RuntimeException($err['message']);
37
        }
38
39
        return $res;
40
    }
41
42
    /**
43
     * @param $object
44
     *
45
     * @return Capsule
46
     * @throws \RuntimeException If problems on un-serializing
47
     */
48
    public static function unwrap($object)
49
    {
50
        $serialized = (false !== ($capsule = @unserialize(trim($object))));
51
52
        if (!$serialized) {
53
            $err = error_get_last();
54
            throw new \RuntimeException($err['message']);
55
        }
56
57
        if (true === $capsule->fake) {
58
            if (isset($capsule->object->scalar)) {
59
                $capsule->object = $capsule->object->scalar;
60
            } else {
61
                $capsule->object = (array)$capsule->object;
62
            }
63
        }
64
65
        if ($capsule->type == 'SimpleXMLElement') {
66
            $capsule->object = simplexml_load_string($capsule->object);
67
        }
68
69
        return $capsule;
70
    }
71
}
72