Passed
Push — master ( 3e4f5a...05d6b0 )
by Jan
04:28
created

PartAttachmentsColumn::render()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 30
rs 9.6666
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 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
25
use App\Entity\Attachments\Attachment;
26
use App\Entity\Parts\Part;
27
use App\Services\Attachments\AttachmentURLGenerator;
28
use App\Services\EntityURLGenerator;
29
use App\Services\FAIconGenerator;
30
use Omines\DataTablesBundle\Column\AbstractColumn;
31
use Symfony\Component\OptionsResolver\OptionsResolver;
32
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
33
34
35
class PartAttachmentsColumn extends AbstractColumn
36
{
37
38
    protected $FAIconGenerator;
39
    protected $urlGenerator;
40
41
    public function __construct(FAIconGenerator $FAIconGenerator, AttachmentURLGenerator $urlGenerator)
42
    {
43
        $this->FAIconGenerator = $FAIconGenerator;
44
        $this->urlGenerator = $urlGenerator;
45
    }
46
47
    /**
48
     * The normalize function is responsible for converting parsed and processed data to a datatables-appropriate type.
49
     *
50
     * @param mixed $value The single value of the column
51
     * @return mixed
52
     */
53
    public function normalize($value)
54
    {
55
        return $value;
56
    }
57
58
    public function render($value, $context)
59
    {
60
        if (!$context instanceof Part) {
61
            throw new \RuntimeException('$context must be a Part object!');
62
        }
63
        $tmp = "";
64
        $attachments = $context->getAttachments()->filter(function (Attachment $attachment) {
65
            return $attachment->getShowInTable();
66
        });
67
68
        $count = 5;
69
        foreach ($attachments as $attachment) {
70
            //Only show the first 5 attachments
71
            if (--$count <= 0) {
72
                continue;
73
            }
74
            /** @var Attachment $attachment */
75
            $tmp .= sprintf(
76
                '<a href="%s" title="%s" class="attach-table-icon" target="_blank" rel="noopener" data-no-ajax>%s</a>',
77
                $this->urlGenerator->getViewURL($attachment),
78
                $attachment->getName() . ': ' . $attachment->getFilename(),
79
                $this->FAIconGenerator->generateIconHTML(
80
                    $this->FAIconGenerator->fileExtensionToFAType($attachment->getExtension()),
0 ignored issues
show
Bug introduced by
It seems like $attachment->getExtension() can also be of type null; however, parameter $extension of App\Services\FAIconGener...fileExtensionToFAType() does only seem to accept string, 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

80
                    $this->FAIconGenerator->fileExtensionToFAType(/** @scrutinizer ignore-type */ $attachment->getExtension()),
Loading history...
81
                    'fas',
82
                    'fa-2x'
83
                )
84
            );
85
        }
86
87
        return $tmp;
88
    }
89
90
    public function configureOptions(OptionsResolver $resolver)
91
    {
92
        parent::configureOptions($resolver);
93
    }
94
}