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

LogEntryTargetColumn::render()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 35
rs 9.3554
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\Column;
23
24
use App\Entity\Base\DBElement;
25
use App\Entity\Base\NamedDBElement;
26
use App\Entity\LogSystem\AbstractLogEntry;
27
use App\Repository\LogEntryRepository;
28
use App\Services\ElementTypeNameGenerator;
29
use App\Services\EntityURLGenerator;
30
use Doctrine\ORM\EntityManagerInterface;
31
use Omines\DataTablesBundle\Column\AbstractColumn;
32
use Symfony\Component\OptionsResolver\OptionsResolver;
33
use Symfony\Contracts\Translation\TranslatorInterface;
34
35
class LogEntryTargetColumn extends AbstractColumn
36
{
37
38
    protected $em;
39
    protected $entryRepository;
40
    protected $entityURLGenerator;
41
    protected $elementTypeNameGenerator;
42
    protected $translator;
43
44
    public function __construct(EntityManagerInterface $entityManager, EntityURLGenerator $entityURLGenerator,
45
        ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator)
46
    {
47
        $this->em = $entityManager;
48
        $this->entryRepository = $entityManager->getRepository(AbstractLogEntry::class);
49
50
        $this->entityURLGenerator = $entityURLGenerator;
51
        $this->elementTypeNameGenerator = $elementTypeNameGenerator;
52
        $this->translator = $translator;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function normalize($value)
59
    {
60
        return $value;
61
    }
62
63
    public function configureOptions(OptionsResolver $resolver)
64
    {
65
        parent::configureOptions($resolver);
66
    }
67
68
    public function render($value, $context)
69
    {
70
        /** @var AbstractLogEntry $context */
71
        $target = $this->entryRepository->getTargetElement($context);
72
73
        //The element is existing
74
        if ($target instanceof NamedDBElement) {
75
            return sprintf(
76
                '<a href="%s">%s</a>',
77
                $this->entityURLGenerator->infoURL($target),
78
                $this->elementTypeNameGenerator->getTypeNameCombination($target, true)
79
            );
80
        }
81
82
        //Target does not have a name
83
        if ($target instanceof DBElement) {
84
            return sprintf(
85
                '<i>%s</i>: %s',
86
                $this->elementTypeNameGenerator->getLocalizedTypeLabel($target),
87
                $target->getID()
88
            );
89
        }
90
91
        //Element was deleted
92
        if ($target === null && $context->hasTarget()) {
93
            return sprintf(
94
                '<i>%s</i>: %s [%s]',
95
                $this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getTargetClass()),
96
                $context->getTargetID(),
97
                $this->translator->trans('log.target_deleted')
98
            );
99
        }
100
101
        //Log is not associated with an element
102
        return "";
103
    }
104
}