Passed
Push — master ( 2d425f...eb03d1 )
by Jan
04:57 queued 10s
created

RevertLogColumn::render()   B

Complexity

Conditions 9
Paths 3

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 28
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 40
rs 8.0555
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 modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (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 Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\DataTables\Column;
22
23
24
use App\Entity\LogSystem\AbstractLogEntry;
25
use App\Entity\LogSystem\CollectionElementDeleted;
26
use App\Entity\LogSystem\ElementCreatedLogEntry;
27
use App\Entity\LogSystem\ElementDeletedLogEntry;
28
use App\Entity\LogSystem\ElementEditedLogEntry;
29
use Omines\DataTablesBundle\Column\AbstractColumn;
30
use Symfony\Component\Security\Core\Security;
31
use Symfony\Contracts\Translation\TranslatorInterface;
32
33
class RevertLogColumn extends AbstractColumn
34
{
35
    protected $translator;
36
    protected $security;
37
38
    public function __construct(TranslatorInterface $translator, Security $security)
39
    {
40
        $this->translator = $translator;
41
        $this->security = $security;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function normalize($value)
48
    {
49
        return $value;
50
    }
51
52
    public function render($value, $context)
53
    {
54
        $revertable = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $revertable is dead and can be removed.
Loading history...
55
        if (
56
            $context instanceof CollectionElementDeleted
57
            || ($context instanceof ElementDeletedLogEntry && $context->hasOldDataInformations())
58
        ) {
59
            $icon = 'fa-trash-restore';
60
            $title = $this->translator->trans('log.undo.undelete');
61
        } elseif (
62
            $context instanceof ElementCreatedLogEntry
63
            || ($context instanceof ElementEditedLogEntry && $context->hasOldDataInformations())
64
        ) {
65
            $icon = 'fa-undo';
66
            $title = $this->translator->trans('log.undo.undo');
67
        } else {
68
            return '';
69
        }
70
71
        $disabled = !$this->security->isGranted('revert_element', $context->getTargetClass());
72
73
        $tmp = '<div class="btn-group btn-group-sm">';
74
        $tmp .= sprintf(
75
            '<button type="submit" class="btn btn-outline-secondary" name="undo" value="%d" %s><i class="fas fa-fw %s" title="%s"></i></button>',
76
            $context->getID(),
77
            $disabled ? 'disabled' : '',
78
            $icon,
79
            $title
80
        );
81
82
        $tmp .= sprintf(
83
            '<button type="submit" class="btn btn-outline-secondary" name="revert" value="%d" %s><i class="fas fa-fw fa-backward" title="%s"></i></button>',
84
            $context->getID(),
85
            $disabled ? 'disabled' : '',
86
            $this->translator->trans('log.undo.revert')
87
        );
88
89
        $tmp .= '</div>';
90
91
        return $tmp;
92
    }
93
}