Completed
Push — feature/modernize ( 620b83 )
by Narcotic
04:48
created

JsonSerializer::unserializeObject()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.1429

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 30
ccs 18
cts 21
cp 0.8571
rs 6.7272
cc 7
eloc 18
nc 16
nop 1
crap 7.1429
1
<?php
2
/**
3
 * a small extension to the Zumba\Util\JsonSerializer to allow deserializing mongodb legacy driver objects
4
 *
5
 * NOTE: this is only needed as we widely use the "mongodb legacy" driver (pecl/mongo) instead of the newer one
6
 * (pecl/mongodb) - if we once switch to the new driver, we should be able to ditch this.
7
 */
8
9
namespace Graviton\ImportExport\Util;
10
11
use Zumba\JsonSerializer\Exception\JsonSerializerException;
12
use Zumba\JsonSerializer\JsonSerializer as BaseSerializer;
13
14
/**
15
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class JsonSerializer extends BaseSerializer
20
{
21
    /**
22
     * Convert the serialized array into an object
23
     *
24
     * @param mixed $value the value
25
     * @return object
26
     * @throws JsonSerializerException
27
     */
28 2
    protected function unserializeObject($value)
29
    {
30 2
        $className = $value[static::CLASS_IDENTIFIER_KEY];
31
32 2
        $obj = false;
33 2
        if ($className == 'MongoDate') {
34 2
            $obj = new \MongoDate($value['sec'], $value['usec']);
35 2
        } elseif ($className == 'MongoId') {
36 1
            $thisId = null;
37 1
            if (isset($value['$id'])) {
38 1
                $thisId = $value['$id'];
39 1
            }
40 1
            if (isset($value['objectID']['oid'])) {
41
                $thisId = $value['objectID']['oid'];
42
            }
43
44 1
            if (is_null($thisId)) {
45
                throw new \LogicException('Could not deserialize MongoID instance, could not find $id!');
46
            }
47
48 1
            $obj = new \MongoId($thisId);
49 1
        }
50
51 2
        if ($obj !== false) {
52 2
            $this->objectMapping[$this->objectMappingIndex++] = $obj;
53 2
            return $obj;
54
        }
55
56 1
        return parent::unserializeObject($value);
57
    }
58
}
59