Passed
Push — refactor/backendModule-ValueOb... ( 7f4b86...3e8499 )
by Tomas Norre
07:27
created

ResultHandler::getResultLog()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.1426

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 10
cts 14
cp 0.7143
crap 8.1426
rs 8.8333
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 6
    public static function getResultLog($resultRow)
34
    {
35 6
        $content = '';
36 6
        if (is_array($resultRow) && array_key_exists('result_data', $resultRow)) {
37 5
            $requestContent = self::getJsonCompatibilityConverter()->convert($resultRow['result_data']) ?: ['content' => ''];
38 5
            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 $search of array_key_exists() does only seem to accept 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 1
                return $content;
40
            }
41 4
            $requestResult = self::getJsonCompatibilityConverter()->convert($requestContent['content']);
42
43 4
            if (is_array($requestResult) && array_key_exists('log', $requestResult)) {
44 2
                $content = implode(chr(10), $requestResult['log']);
45
            }
46
        }
47 5
        return $content;
48
    }
49
50 7
    public static function getResStatus($requestContent): string
51
    {
52 7
        if (empty($requestContent)) {
53 2
            return '-';
54
        }
55 5
        if (! array_key_exists('content', $requestContent)) {
56 1
            return 'Content index does not exists in requestContent array';
57
        }
58
59 4
        $requestResult = self::getJsonCompatibilityConverter()->convert($requestContent['content']);
60 4
        if (is_array($requestResult)) {
61 3
            if (empty($requestResult['errorlog'])) {
62 1
                return 'OK';
63
            }
64 2
            return implode("\n", $requestResult['errorlog']);
65
        }
66
67 1
        if (is_bool($requestResult)) {
0 ignored issues
show
introduced by
The condition is_bool($requestResult) is always true.
Loading history...
68 1
            return 'Error - no info, sorry!';
69
        }
70
71
        return 'Error: ' . substr(preg_replace('/\s+/', ' ', strip_tags($requestResult)), 0, 10000) . '...';
72
    }
73
74
    /**
75
     * Find Fe vars
76
     */
77 4
    public static function getResFeVars(array $resultData): array
78
    {
79 4
        if (empty($resultData)) {
80 1
            return [];
81
        }
82 3
        $requestResult = self::getJsonCompatibilityConverter()->convert($resultData['content']);
83 3
        return $requestResult['vars'] ?? [];
84
    }
85
86 12
    private static function getJsonCompatibilityConverter(): JsonCompatibilityConverter
87
    {
88 12
        return GeneralUtility::makeInstance(JsonCompatibilityConverter::class);
89
    }
90
}
91