Passed
Push — version-matrix ( df0591 )
by Tomas Norre
13:39 queued 08:00
created

ResultHandler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 67
ccs 0
cts 30
cp 0
rs 10
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getResultLog() 0 15 7
A getResFeVars() 0 7 2
A getResStatus() 0 22 6
A getJsonCompatibilityConverter() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Backend\Helper;
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
use AOE\Crawler\Converter\JsonCompatibilityConverter;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
class ResultHandler
26
{
27
    /**
28
     * Extract the log information from the current row and retrieve it as formatted string.
29
     *
30
     * @param array $resultRow
31
     * @return string
32
     */
33
    public static function getResultLog($resultRow)
34
    {
35
        $content = '';
36
        if (is_array($resultRow) && array_key_exists('result_data', $resultRow)) {
37
            $requestContent = self::getJsonCompatibilityConverter()->convert($resultRow['result_data']) ?: [];
38
            if (! array_key_exists('content', $requestContent)) {
0 ignored issues
show
Bug introduced by
It seems like $requestContent can also be of type true; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            if (! array_key_exists('content', /** @scrutinizer ignore-type */ $requestContent)) {
Loading history...
39
                return $content;
40
            }
41
            $requestResult = self::getJsonCompatibilityConverter()->convert($requestContent['content']);
42
43
            if (is_array($requestResult) && array_key_exists('log', $requestResult)) {
44
                $content = implode(chr(10), $requestResult['log']);
45
            }
46
        }
47
        return $content;
48
    }
49
50
    /**
51
     * @param array|bool $requestContent
52
     */
53
    public static function getResStatus($requestContent): string
54
    {
55
        if (empty($requestContent)) {
56
            return '-';
57
        }
58
        if (! array_key_exists('content', $requestContent)) {
0 ignored issues
show
Bug introduced by
It seems like $requestContent can also be of type boolean; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        if (! array_key_exists('content', /** @scrutinizer ignore-type */ $requestContent)) {
Loading history...
59
            return 'Content index does not exists in requestContent array';
60
        }
61
62
        $requestResult = self::getJsonCompatibilityConverter()->convert($requestContent['content']);
63
        if (is_array($requestResult)) {
64
            if (empty($requestResult['errorlog'])) {
65
                return 'OK';
66
            }
67
            return implode("\n", $requestResult['errorlog']);
68
        }
69
70
        if (is_bool($requestResult)) {
0 ignored issues
show
introduced by
The condition is_bool($requestResult) is always true.
Loading history...
71
            return 'Error - no info, sorry!';
72
        }
73
74
        return 'Error: ' . substr(preg_replace('/\s+/', ' ', strip_tags($requestResult)), 0, 10000) . '...';
75
    }
76
77
    /**
78
     * Find Fe vars
79
     */
80
    public static function getResFeVars(array $resultData): array
81
    {
82
        if (empty($resultData)) {
83
            return [];
84
        }
85
        $requestResult = self::getJsonCompatibilityConverter()->convert($resultData['content']);
86
        return $requestResult['vars'] ?? [];
87
    }
88
89
    private static function getJsonCompatibilityConverter(): JsonCompatibilityConverter
90
    {
91
        return GeneralUtility::makeInstance(JsonCompatibilityConverter::class);
92
    }
93
}
94