Passed
Pull Request — main (#3273)
by Sascha
42:06
created

DebugWriter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 52.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 23
c 1
b 0
f 0
dl 0
loc 71
ccs 11
cts 21
cp 0.5238
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsAllowedByDevIPMask() 0 3 1
A writeDebugMessage() 0 9 3
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\Util;
19
use TYPO3\CMS\Core\Http\ApplicationType;
20
use TYPO3\CMS\Core\Utility\DebugUtility;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
23
24
/**
25
 * The DebugWriter is used to write the devLog messages to the output of the page, or to the TYPO3 console in the
26
 * backend to provide a simple and lightweigt debugging possibility.
27
 *
28
 * @author Timo Hund <[email protected]>
29
 */
30
class DebugWriter
31
{
32
33
    /**
34
     * When the feature is enabled with: plugin.tx_solr.logging.debugOutput the log writer uses the extbase
35
     * debug functionality in the frontend, or the console in the backend to display the devlog messages.
36
     *
37
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
38
     * @param string $message Log message.
39
     * @param array $data Additional data to log
40
     */
41
    public function write($level, $message, $data = [])
42
    {
43
        $debugAllowedForIp = $this->getIsAllowedByDevIPMask();
44
        if (!$debugAllowedForIp) {
45
            return;
46
        }
47
48
        $isDebugOutputEnabled = $this->getIsDebugOutputEnabled();
49
        if (!$isDebugOutputEnabled) {
50 43
            return;
51
        }
52 43
53 43
        $isPageIndexingRequest = $this->getIsPageIndexingRequest();
54 41
        if ($isPageIndexingRequest) {
55
            return;
56
        }
57 2
58 2
        $this->writeDebugMessage($level, $message, $data);
59 1
    }
60
61
    /**
62 1
     * @return bool
63 1
     */
64
    protected function getIsAllowedByDevIPMask()
65
    {
66
        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

66
        return GeneralUtility::cmpIP(/** @scrutinizer ignore-type */ GeneralUtility::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
Loading history...
67
    }
68 40
69
    /**
70 40
     * Check if Logging via debugOutput has been configured
71
     *
72
     * @return bool
73
     */
74
    protected function getIsDebugOutputEnabled()
75
    {
76
        return Util::getSolrConfiguration()->getLoggingDebugOutput();
77
    }
78
79
    protected function getIsPageIndexingRequest(): bool
80
    {
81
        if (!$GLOBALS['TYPO3_REQUEST'] instanceof Request) {
0 ignored issues
show
Bug introduced by
The type ApacheSolrForTypo3\Solr\System\Logging\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
            return false;
83
        }
84
        return $GLOBALS['TYPO3_REQUEST']->hasHeader(PageIndexerRequest::SOLR_INDEX_HEADER);
0 ignored issues
show
Bug introduced by
The type ApacheSolrForTypo3\Solr\...ging\PageIndexerRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
    }
86
87
    /**
88
     * @param int|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
89
     * @param string $message Log message.
90
     * @param array $data Additional data to log
91
     */
92
    protected function writeDebugMessage($level, $message, $data)
93
    {
94
        $parameters = ['extKey' => 'solr', 'msg' => $message, 'level' => $level, 'data' => $data];
95
        $message = isset($parameters['msg']) ? $parameters['msg'] : '';
96
        if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) {
97
            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

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