Passed
Push — master ( 246a96...b27079 )
by Jan
04:53
created

LogEntryTargetColumn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7
 *
8
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23
 */
24
25
namespace App\DataTables\Column;
26
27
use App\Entity\Base\AbstractDBElement;
28
use App\Entity\Base\AbstractNamedDBElement;
29
use App\Entity\LogSystem\AbstractLogEntry;
30
use App\Exceptions\EntityNotSupportedException;
31
use App\Services\ElementTypeNameGenerator;
32
use App\Services\EntityURLGenerator;
33
use Doctrine\ORM\EntityManagerInterface;
34
use Omines\DataTablesBundle\Column\AbstractColumn;
35
use Symfony\Component\OptionsResolver\OptionsResolver;
36
use Symfony\Contracts\Translation\TranslatorInterface;
37
38
class LogEntryTargetColumn extends AbstractColumn
39
{
40
    protected $em;
41
    protected $entryRepository;
42
    protected $entityURLGenerator;
43
    protected $elementTypeNameGenerator;
44
    protected $translator;
45
46
    public function __construct(EntityManagerInterface $entityManager, EntityURLGenerator $entityURLGenerator,
47
        ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator)
48
    {
49
        $this->em = $entityManager;
50
        $this->entryRepository = $entityManager->getRepository(AbstractLogEntry::class);
51
52
        $this->entityURLGenerator = $entityURLGenerator;
53
        $this->elementTypeNameGenerator = $elementTypeNameGenerator;
54
        $this->translator = $translator;
55
    }
56
57
    public function normalize($value)
58
    {
59
        return $value;
60
    }
61
62
    public function configureOptions(OptionsResolver $resolver)
63
    {
64
        parent::configureOptions($resolver);
65
        return $this;
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 AbstractNamedDBElement) {
75
            try {
76
                return sprintf(
77
                    '<a href="%s">%s</a>',
78
                    $this->entityURLGenerator->infoURL($target),
79
                    $this->elementTypeNameGenerator->getTypeNameCombination($target, true)
80
                );
81
            } catch (EntityNotSupportedException $exception) {
82
                return $this->elementTypeNameGenerator->getTypeNameCombination($target, true);
83
            }
84
        }
85
86
        //Target does not have a name
87
        if ($target instanceof AbstractDBElement) {
88
            return sprintf(
89
                '<i>%s</i>: %s',
90
                $this->elementTypeNameGenerator->getLocalizedTypeLabel($target),
91
                $target->getID()
92
            );
93
        }
94
95
        //Element was deleted
96
        if (null === $target && $context->hasTarget()) {
97
            return sprintf(
98
                '<i>%s</i>: %s [%s]',
99
                $this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getTargetClass()),
100
                $context->getTargetID(),
101
                $this->translator->trans('log.target_deleted')
102
            );
103
        }
104
105
        //Log is not associated with an element
106
        return '';
107
    }
108
}
109