SuperClosure::unserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the DS Framework.
4
 *
5
 * (c) Dan Smith <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Ds\Router\Serializer;
11
12
use Ds\Router\Exceptions\RouteException;
13
use Ds\Router\Interfaces\SerializerInterface;
14
use SuperClosure\Serializer as SuperClosureSerializer;
15
16
/**
17
 * Class SuperClosure
18
 *
19
 * SuperClosure\Serializer Wrapper. Used to Serialize closures.
20
 *
21
 * @package Ds\Router\Serializer
22
 * @author  Dan Smith    <[email protected]>
23
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
24
 * @link    https://red-sqr.co.uk/Framework/Router/wikis/
25
 *
26
 * @see "jeremeamia/SuperClosure"
27
 */
28
class SuperClosure implements SerializerInterface
29
{
30
    /**
31
     * @var SuperClosureSerializer
32
     */
33
    public $serializer;
34
35
    /**
36
     * SuperClosure constructor.
37
     *
38
     * @param SuperClosureSerializer $serializer
39
     */
40 4
    public function __construct(SuperClosureSerializer $serializer)
41
    {
42 4
        $this->serializer = $serializer;
43 4
    }
44
45
    /**
46
     * @inheritdoc
47
     * @throws \Exception
48
     */
49 2
    public function serialize($data)
50
    {
51 2
        $classRef = new \ReflectionClass($data);
52 2
        if ($classRef->getName() && $classRef->getName() !== 'Closure') {
0 ignored issues
show
Bug introduced by
Consider using $classRef->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
53 1
            throw new RouteException('Class: ' . $classRef->getName() . ' can not be used as a handler. Must be a valid closure or string');
0 ignored issues
show
Bug introduced by
Consider using $classRef->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
54
        }
55
56 1
        return \base64_encode($this->serializer->serialize($data));
57
    }
58
59
    /**
60
     * @inheritdoc
61
     * @throws \SuperClosure\Exception\ClosureUnserializationException
62
     */
63 1
    public function unserialize(string $string)
64
    {
65 1
        return $this->serializer->unserialize(\base64_decode($string));
66
    }
67
}
68