Passed
Pull Request — main (#3338)
by Rafael
03:25
created

DebugWriter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 57.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 23
c 1
b 0
f 0
dl 0
loc 70
ccs 15
cts 26
cp 0.5769
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsAllowedByDevIPMask() 0 3 1
A writeDebugMessage() 0 9 2
A write() 0 18 4
A getIsPageIndexingRequest() 0 6 2
A getIsDebugOutputEnabled() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\System\Logging;
17
18
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest;
19
use ApacheSolrForTypo3\Solr\Util;
20
use TYPO3\CMS\Core\Http\ApplicationType;
21
use TYPO3\CMS\Core\Http\Request;
22
use TYPO3\CMS\Core\Utility\DebugUtility;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
25
26
/**
27
 * The DebugWriter is used to write the devLog messages to the output of the page, or to the TYPO3 console in the
28
 * backend to provide a simple and lightweight debugging possibility.
29
 *
30
 * @author Timo Hund <[email protected]>
31
 */
32
class DebugWriter
33
{
34
    /**
35
     * When the feature is enabled with: plugin.tx_solr.logging.debugOutput the log writer uses the extbase
36
     * debug functionality in the frontend, or the console in the backend to display the devlog messages.
37
     *
38
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
39
     * @param string $message Log message.
40
     * @param array $data Additional data to log
41
     */
42 83
    public function write($level, $message, $data = [])
43
    {
44 83
        $debugAllowedForIp = $this->getIsAllowedByDevIPMask();
45 83
        if (!$debugAllowedForIp) {
46 81
            return;
47
        }
48
49 2
        $isDebugOutputEnabled = $this->getIsDebugOutputEnabled();
50 2
        if (!$isDebugOutputEnabled) {
51 1
            return;
52
        }
53
54 1
        $isPageIndexingRequest = $this->getIsPageIndexingRequest();
55 1
        if ($isPageIndexingRequest) {
56
            return;
57
        }
58
59 1
        $this->writeDebugMessage($level, $message, $data);
60
    }
61
62
    /**
63
     * @return bool
64
     */
65 80
    protected function getIsAllowedByDevIPMask()
66
    {
67 80
        return GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
0 ignored issues
show
Bug introduced by
It seems like TYPO3\CMS\Core\Utility\G...tIndpEnv('REMOTE_ADDR') can also be of type array<string,boolean|null|string>; however, parameter $baseIP of TYPO3\CMS\Core\Utility\GeneralUtility::cmpIP() does only seem to accept string, 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

67
        return GeneralUtility::cmpIP(/** @scrutinizer ignore-type */ GeneralUtility::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
Loading history...
68
    }
69
70
    /**
71
     * Check if Logging via debugOutput has been configured
72
     *
73
     * @return bool
74
     */
75
    protected function getIsDebugOutputEnabled()
76
    {
77
        return Util::getSolrConfiguration()->getLoggingDebugOutput();
78
    }
79
80 1
    protected function getIsPageIndexingRequest(): bool
81
    {
82 1
        if (!($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof Request) {
83 1
            return false;
84
        }
85
        return $GLOBALS['TYPO3_REQUEST']->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER);
86
    }
87
88
    /**
89
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
90
     * @param string $message Log message.
91
     * @param array $data Additional data to log
92
     */
93
    protected function writeDebugMessage($level, $message, $data)
94
    {
95
        $parameters = ['extKey' => 'solr', 'msg' => $message, 'level' => $level, 'data' => $data];
96
        $message = $parameters['msg'] ?? '';
97
        if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) {
98
            DebugUtility::debug($parameters, $parameters['extKey'], 'DevLog ext:solr: ' . $message);
0 ignored issues
show
Bug introduced by
$parameters of type array<string,array|integer|string> is incompatible with the type string expected by parameter $var of TYPO3\CMS\Core\Utility\DebugUtility::debug(). ( Ignorable by Annotation )

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

98
            DebugUtility::debug(/** @scrutinizer ignore-type */ $parameters, $parameters['extKey'], 'DevLog ext:solr: ' . $message);
Loading history...
99
        } else {
100
            echo $message . ':<br/>';
101
            DebuggerUtility::var_dump($parameters);
102
        }
103
    }
104
}
105