Passed
Push — master ( d757d1...9035d9 )
by Jan
04:33
created

LogEntryTargetColumn   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 54
c 3
b 0
f 0
dl 0
loc 92
rs 10
wmc 23

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A configureOptions() 0 6 1
A normalize() 0 3 1
D render() 0 60 20
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
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\DataTables\Column;
44
45
use App\Entity\Attachments\Attachment;
46
use App\Entity\Base\AbstractDBElement;
47
use App\Entity\Base\AbstractNamedDBElement;
48
use App\Entity\LogSystem\AbstractLogEntry;
49
use App\Entity\Parameters\AbstractParameter;
50
use App\Entity\PriceInformations\Orderdetail;
51
use App\Entity\PriceInformations\Pricedetail;
52
use App\Exceptions\EntityNotSupportedException;
53
use App\Services\ElementTypeNameGenerator;
54
use App\Services\EntityURLGenerator;
55
use Doctrine\ORM\EntityManagerInterface;
56
use Omines\DataTablesBundle\Column\AbstractColumn;
57
use Symfony\Component\OptionsResolver\OptionsResolver;
58
use Symfony\Contracts\Translation\TranslatorInterface;
59
60
class LogEntryTargetColumn extends AbstractColumn
61
{
62
    protected $em;
63
    protected $entryRepository;
64
    protected $entityURLGenerator;
65
    protected $elementTypeNameGenerator;
66
    protected $translator;
67
68
    public function __construct(EntityManagerInterface $entityManager, EntityURLGenerator $entityURLGenerator,
69
        ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator)
70
    {
71
        $this->em = $entityManager;
72
        $this->entryRepository = $entityManager->getRepository(AbstractLogEntry::class);
73
74
        $this->entityURLGenerator = $entityURLGenerator;
75
        $this->elementTypeNameGenerator = $elementTypeNameGenerator;
76
        $this->translator = $translator;
77
    }
78
79
    public function normalize($value)
80
    {
81
        return $value;
82
    }
83
84
    public function configureOptions(OptionsResolver $resolver)
85
    {
86
        parent::configureOptions($resolver);
87
        $resolver->setDefault('show_associated', true);
88
89
        return $this;
90
    }
91
92
    public function render($value, $context)
93
    {
94
        /** @var AbstractLogEntry $context */
95
        $target = $this->entryRepository->getTargetElement($context);
96
97
        $tmp = '';
98
99
        //The element is existing
100
        if ($target instanceof AbstractNamedDBElement) {
101
            try {
102
                $tmp = sprintf(
103
                    '<a href="%s">%s</a>',
104
                    $this->entityURLGenerator->infoURL($target),
105
                    $this->elementTypeNameGenerator->getTypeNameCombination($target, true)
106
                );
107
            } catch (EntityNotSupportedException $exception) {
108
                $tmp = $this->elementTypeNameGenerator->getTypeNameCombination($target, true);
109
            }
110
        } elseif ($target instanceof AbstractDBElement) { //Target does not have a name
111
            $tmp = sprintf(
112
                '<i>%s</i>: %s',
113
                $this->elementTypeNameGenerator->getLocalizedTypeLabel($target),
114
                $target->getID()
115
            );
116
        } elseif (null === $target && $context->hasTarget()) {  //Element was deleted
117
            $tmp = sprintf(
118
                '<i>%s</i>: %s [%s]',
119
                $this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getTargetClass()),
120
                $context->getTargetID(),
121
                $this->translator->trans('log.target_deleted')
122
            );
123
        }
124
125
        //Add a hint to the associated element if possible
126
        if (null !== $target && $this->options['show_associated']) {
127
            if ($target instanceof Attachment && $target->getElement() !== null) {
128
                $on = $target->getElement();
129
            } elseif ($target instanceof AbstractParameter && $target->getElement() !== null) {
130
                $on = $target->getElement();
131
            } elseif ($target instanceof Orderdetail && $target->getPart() !== null) {
132
                $on = $target->getPart();
133
            } elseif ($target instanceof Pricedetail && $target->getOrderdetail() !== null && $target->getOrderdetail()->getPart() !== null) {
134
                $on = $target->getOrderdetail()->getPart();
135
            }
136
137
            if (isset($on) && is_object($on)) {
138
                try {
139
                    $tmp .= sprintf(
140
                        ' (<a href="%s">%s</a>)',
141
                        $this->entityURLGenerator->infoURL($on),
142
                        $this->elementTypeNameGenerator->getTypeNameCombination($on, true)
143
                    );
144
                } catch (EntityNotSupportedException $exception) {
145
                    $tmp .= ' (' . $this->elementTypeNameGenerator->getTypeNameCombination($target, true) .')';
0 ignored issues
show
Bug introduced by
It seems like $target can also be of type App\Entity\Base\AbstractDBElement and App\Entity\PriceInformations\Orderdetail and App\Entity\PriceInformations\Pricedetail; however, parameter $entity of App\Services\ElementType...etTypeNameCombination() does only seem to accept App\Entity\Contracts\NamedElementInterface, 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

145
                    $tmp .= ' (' . $this->elementTypeNameGenerator->getTypeNameCombination(/** @scrutinizer ignore-type */ $target, true) .')';
Loading history...
146
                }
147
            }
148
        }
149
150
        //Log is not associated with an element
151
        return $tmp;
152
    }
153
}
154