Passed
Branch master (c87ba8)
by Christian
16:02
created

getNumberOfErrorsInPeriod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 2
dl 0
loc 22
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Dashboard\Widgets\Provider;
19
20
use TYPO3\CMS\Core\Database\Connection;
21
use TYPO3\CMS\Core\Database\ConnectionPool;
22
use TYPO3\CMS\Core\Localization\LanguageService;
23
use TYPO3\CMS\Core\SysLog\Type as SystemLogType;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Dashboard\WidgetApi;
26
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;
27
28
/**
29
 * Provides chart data for sys log errors.
30
 */
31
class SysLogErrorsDataProvider implements ChartDataProviderInterface
32
{
33
    /**
34
     * Number of days to gather information for.
35
     *
36
     * @var int
37
     */
38
    protected $days = 31;
39
40
    /**
41
     * @var array
42
     */
43
    protected $labels = [];
44
45
    /**
46
     * @var array
47
     */
48
    protected $data = [];
49
50
    public function __construct(int $days = 31)
51
    {
52
        $this->days = $days;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function getChartData(): array
59
    {
60
        $this->calculateDataForLastDays();
61
62
        return [
63
            'labels' => $this->labels,
64
            'datasets' => [
65
                [
66
                    'label' => $this->getLanguageService()->sL('LLL:EXT:dashboard/Resources/Private/Language/locallang.xlf:widgets.sysLogErrors.chart.dataSet.0'),
67
                    'backgroundColor' => WidgetApi::getDefaultChartColors()[0],
68
                    'border' => 0,
69
                    'data' => $this->data
70
                ]
71
            ]
72
        ];
73
    }
74
75
    protected function getNumberOfErrorsInPeriod(int $start, int $end): int
76
    {
77
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_log');
78
        return (int)$queryBuilder
79
            ->count('*')
80
            ->from('sys_log')
81
            ->where(
82
                $queryBuilder->expr()->eq(
83
                    'type',
84
                    $queryBuilder->createNamedParameter(SystemLogType::ERROR, Connection::PARAM_INT)
85
                ),
86
                $queryBuilder->expr()->gte(
87
                    'tstamp',
88
                    $queryBuilder->createNamedParameter($start, Connection::PARAM_INT)
89
                ),
90
                $queryBuilder->expr()->lte(
91
                    'tstamp',
92
                    $queryBuilder->createNamedParameter($end, Connection::PARAM_INT)
93
                )
94
            )
95
            ->execute()
96
            ->fetchColumn();
97
    }
98
99
    protected function calculateDataForLastDays(): void
100
    {
101
        $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
102
103
        for ($daysBefore = $this->days; $daysBefore >= 0; $daysBefore--) {
104
            $this->labels[] = date($format, strtotime('-' . $daysBefore . ' day'));
105
            $startPeriod = strtotime('-' . $daysBefore . ' day 0:00:00');
106
            $endPeriod =  strtotime('-' . $daysBefore . ' day 23:59:59');
107
108
            $this->data[] = $this->getNumberOfErrorsInPeriod($startPeriod, $endPeriod);
109
        }
110
    }
111
112
    protected function getLanguageService(): LanguageService
113
    {
114
        return $GLOBALS['LANG'];
115
    }
116
}
117