MongoDBBinaryPacker::pack()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cache package.
4
 *
5
 * Copyright (c) Daniel González
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author Daniel González <[email protected]>
11
 * @author Arnold Daniels <[email protected]>
12
 */
13
14
namespace Desarrolla2\Cache\Packer;
15
16
use Desarrolla2\Cache\Packer\PackerInterface;
17
use MongoDB\BSON\Binary;
18
19
/**
20
 * Pack as BSON binary
21
 *
22
 * @todo Don't use serialize when packer chain is here.
23
 */
24
class MongoDBBinaryPacker implements PackerInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $options;
30
31
    /**
32
     * SerializePacker constructor
33
     *
34
     * @param array $options  Any options to be provided to unserialize()
35
     */
36 61
    public function __construct(array $options = ['allowed_classes' => true])
37
    {
38 61
        $this->options = $options;
39
    }
40
41
    /**
42
     * Get cache type (might be used as file extension)
43
     *
44
     * @return string
45
     */
46
    public function getType()
47
    {
48
        return 'bson';
49
    }
50
51
    /**
52
     * Pack the value
53
     *
54
     * @param mixed $value
55
     * @return string
56
     */
57 61
    public function pack($value)
58
    {
59 61
        return new Binary(serialize($value), Binary::TYPE_GENERIC);
60
    }
61
62
    /**
63
     * Unpack the value
64
     *
65
     * @param string $packed
66
     * @return string
67
     * @throws \UnexpectedValueException if he value can't be unpacked
68
     */
69 37
    public function unpack($packed)
70
    {
71 37
        if (!$packed instanceof Binary) {
0 ignored issues
show
introduced by
$packed is never a sub-type of MongoDB\BSON\Binary.
Loading history...
72
            throw new \InvalidArgumentException("packed value should be BSON binary");
73
        }
74
75 37
        return unserialize((string)$packed, $this->options);
76
    }
77
}