Passed
Push — master ( 48f77b...329461 )
by Timo
15:28
created

DevLogDebugWriter::log()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 8
nop 1
crap 5
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\System\Logging;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2010-2016 Timo Hund <[email protected]
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Util;
29
use TYPO3\CMS\Core\Utility\DebugUtility;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
32
33
/**
34
 * The DevLogDebugWriter is used to write the devLog messages to the output of the page, or to the TYPO3 console in the
35
 * backend to provide a simple and lightweigt debugging possibility.
36
 *
37
 * @author Timo Hund <[email protected]>
38
 */
39
class DevLogDebugWriter
40
{
41
42
    /**
43
     * When the feature is enabled with: plugin.tx_solr.logging.debugOutput the log writer uses the extbase
44
     * debug functionality in the frontend, or the console in the backend to display the devlog messages.
45
     *
46
     * @param array $parameters
47
     */
48 7
    public function log($parameters)
49
    {
50 7
        $extensionKey = isset($parameters['extKey']) ? $parameters['extKey'] : '';
51
52 7
        $isLogMessageFromSolr = $extensionKey === 'solr';
53 7
        if (!$isLogMessageFromSolr) {
54 4
            return;
55
        }
56
57 3
        $debugAllowedForIp = $this->getIsAllowedByDevIPMask();
58 3
        if (!$debugAllowedForIp) {
59 1
            return;
60
        }
61
62 2
        $isDebugOutputEnabled = $this->getIsdebugOutputEnabled();
63 2
        if (!$isDebugOutputEnabled) {
64 1
            return;
65
        }
66
67 1
        $this->writeDebugMessage($parameters);
68 1
    }
69
70
    /**
71
     * @return bool
72
     */
73
    protected function getIsAllowedByDevIPMask()
74
    {
75
        return GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
76
    }
77
78
    /**
79
     * Check if Logging via debugOutput has been configured
80
     *
81
     * @return bool
82
     */
83
    protected function getIsdebugOutputEnabled()
84
    {
85
        $logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__);
86
        return $logger->isDebugOutputEnabled();
87
    }
88
89
    /**
90
     * @param $parameters
91
     */
92
    protected function writeDebugMessage($parameters)
93
    {
94
        $message = isset($parameters['msg']) ? $parameters['msg'] : '';
95
        if (TYPO3_MODE == 'BE') {
96
            DebugUtility::debug($parameters, $parameters['extKey'], 'DevLog ext:solr: ' . $message);
97
        } else {
98
            echo $message . ':<br/>';
99
            DebuggerUtility::var_dump($parameters);
100
        }
101
    }
102
}
103