Passed
Push — master ( 3f23ea...c594c9 )
by Jan
05:15 queued 11s
created

LogDataTable::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\DataTables;
23
24
25
use App\DataTables\Column\EntityColumn;
26
use App\DataTables\Column\LocaleDateTimeColumn;
27
use App\DataTables\Column\LogEntryExtraColumn;
28
use App\DataTables\Column\LogEntryTargetColumn;
29
use App\Entity\Attachments\Attachment;
30
use App\Entity\LogSystem\AbstractLogEntry;
31
use App\Entity\UserSystem\User;
32
use App\Services\ElementTypeNameGenerator;
33
use Doctrine\ORM\QueryBuilder;
34
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
35
use Omines\DataTablesBundle\Column\TextColumn;
36
use Omines\DataTablesBundle\DataTable;
37
use Omines\DataTablesBundle\DataTableTypeInterface;
38
use Psr\Log\LogLevel;
39
use SebastianBergmann\CodeCoverage\Report\Text;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCoverage\Report\Text 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...
40
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
41
use Symfony\Contracts\Translation\TranslatorInterface;
42
43
class LogDataTable implements DataTableTypeInterface
44
{
45
    protected $elementTypeNameGenerator;
46
    protected $translator;
47
    protected $urlGenerator;
48
49
    public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator,
50
        UrlGeneratorInterface $urlGenerator)
51
    {
52
        $this->elementTypeNameGenerator = $elementTypeNameGenerator;
53
        $this->translator = $translator;
54
        $this->urlGenerator = $urlGenerator;
55
    }
56
57
    public function configure(DataTable $dataTable, array $options)
58
    {
59
        $dataTable->add('symbol', TextColumn::class, [
60
            'label' => '',
61
            'render' => function ($value, AbstractLogEntry $context) {
62
                switch ($context->getLevelString()) {
63
                    case LogLevel::DEBUG:
64
                        $symbol = 'fa-bug';
65
                        break;
66
                    case LogLevel::INFO:
67
                        $symbol = 'fa-info';
68
                        break;
69
                    case LogLevel::NOTICE:
70
                        $symbol = 'fa-flag';
71
                        break;
72
                    case LogLevel::WARNING:
73
                        $symbol = 'fa-exclamation-circle';
74
                        break;
75
                    case LogLevel::ERROR:
76
                        $symbol = 'fa-exclamation-triangle';
77
                        break;
78
                    case LogLevel::CRITICAL:
79
                        $symbol = 'fa-bolt';
80
                        break;
81
                    case LogLevel::ALERT:
82
                        $symbol = 'fa-radiation';
83
                        break;
84
                    case LogLevel::EMERGENCY:
85
                        $symbol = 'fa-skull-crossbones';
86
                        break;
87
                    default:
88
                        $symbol = 'fa-question-circle';
89
                        break;
90
                }
91
92
                return sprintf('<i class="fas fa-fw %s"></i>', $symbol);
93
            }
94
        ]);
95
96
        $dataTable->add('id', TextColumn::class, [
97
            'label' => $this->translator->trans('log.id'),
98
            'visible' => false,
99
        ]);
100
101
        $dataTable->add('timestamp', LocaleDateTimeColumn::class, [
102
            'label' => $this->translator->trans('log.timestamp'),
103
            'timeFormat' => 'medium'
104
        ]);
105
106
        $dataTable->add('type', TextColumn::class, [
107
            'label' => $this->translator->trans('log.type'),
108
            'propertyPath' => 'type',
109
            'render' => function (string $value, AbstractLogEntry $context) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

109
            'render' => function (string $value, /** @scrutinizer ignore-unused */ AbstractLogEntry $context) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
                return $this->translator->trans('log.type.' . $value);
111
            }
112
113
        ]);
114
115
        $dataTable->add('level', TextColumn::class, [
116
            'label' => $this->translator->trans('log.level'),
117
            'propertyPath' => 'levelString',
118
            'render' => function (string $value, AbstractLogEntry $context) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

118
            'render' => function (string $value, /** @scrutinizer ignore-unused */ AbstractLogEntry $context) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
                return $value;
120
            }
121
        ]);
122
123
124
        $dataTable->add('user', TextColumn::class, [
125
            'label' => $this->translator->trans('log.user'),
126
            'render' => function ($value, AbstractLogEntry $context) {
127
                $user = $context->getUser();
128
                return sprintf(
129
                    '<a href="%s">%s</a>',
130
                    $this->urlGenerator->generate('user_info', ['id' => $user->getID()]),
131
                    $user->getFullName(true)
132
                );
133
            }
134
        ]);
135
136
137
138
        $dataTable->add('target_type', TextColumn::class, [
139
            'label' => $this->translator->trans('log.target_type'),
140
            'visible' => false,
141
            'render' => function ($value, AbstractLogEntry $context) {
142
                $class = $context->getTargetClass();
143
                if ($class !== null) {
144
                    return $this->elementTypeNameGenerator->getLocalizedTypeLabel($class);
145
                }
146
                return '';
147
            }
148
        ]);
149
150
        $dataTable->add('target', LogEntryTargetColumn::class, [
151
            'label' => $this->translator->trans('log.target')
152
        ]);
153
154
        $dataTable->add('extra', LogEntryExtraColumn::class, [
155
            'label' => $this->translator->trans('log.extra')
156
        ]);
157
158
        $dataTable->addOrderBy('timestamp', DataTable::SORT_DESCENDING);
159
160
        $dataTable->createAdapter(ORMAdapter::class, [
161
            'entity' => AbstractLogEntry::class,
162
            'query' => function (QueryBuilder $builder): void {
163
                $this->getQuery($builder);
164
            },
165
        ]);
166
    }
167
168
    protected function getQuery(QueryBuilder $builder): void
169
    {
170
        $builder->distinct()->select('log')
171
            ->addSelect('user')
172
            ->from(AbstractLogEntry::class, 'log')
173
            ->leftJoin('log.user', 'user');
174
    }
175
}