JsonCompatibilityConverter::convert()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Converter;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
/**
23
 * @internal since v9.2.5
24
 */
25
class JsonCompatibilityConverter
26
{
27
    /**
28
     * This is implemented as we want to switch away from serialized data to json data, when the crawler is storing
29
     * in the database. To ensure that older crawler entries, which have already been stored as serialized data
30
     * still works, we have added this converter that can be used for the reading part. The writing part will be done
31
     * in json from now on.
32
     * @see https://github.com/AOEpeople/crawler/issues/417
33
     *
34
     * @return array|bool
35
     * @throws \Exception
36
     */
37 22
    public function convert(string $dataString)
38
    {
39 22
        $unserialized = unserialize($dataString, ['allowed_classes' => false]);
40 22
        if (is_object($unserialized)) {
41 1
            throw new \Exception('Objects are not allowed' . var_export($unserialized, true), 1593758307);
42
        }
43
44 21
        if ($unserialized && ! is_object($unserialized)) {
45 3
            return $unserialized;
46
        }
47
48 18
        $decoded = json_decode($dataString, true);
49 18
        if ($decoded) {
50 11
            return $decoded;
51
        }
52
53 7
        return false;
54
    }
55
}
56