Passed
Push — master ( ec0d02...c0f595 )
by Jan
06:31 queued 10s
created

LabelHTMLGenerator::getLabelHTML()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 30
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 48
rs 8.1954
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\Services\LabelSystem;
22
23
use App\Entity\Contracts\NamedElementInterface;
24
use App\Entity\LabelSystem\LabelOptions;
25
use App\Exceptions\TwigModeException;
26
use App\Services\ElementTypeNameGenerator;
27
use App\Services\LabelSystem\Barcodes\BarcodeContentGenerator;
28
use Symfony\Component\Security\Core\Security;
29
use Twig\Environment;
30
use Twig\Error\Error;
31
use Twig\Error\SyntaxError;
32
33
final class LabelHTMLGenerator
34
{
35
    private $twig;
36
    private $elementTypeNameGenerator;
37
    private $replacer;
38
    private $barcodeGenerator;
39
    private $sandboxedTwigProvider;
40
    private $partdb_title;
41
    private $security;
42
43
    public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, LabelTextReplacer $replacer, Environment $twig,
44
        BarcodeGenerator $barcodeGenerator, SandboxedTwigProvider $sandboxedTwigProvider, Security $security, string $partdb_title)
45
    {
46
        $this->twig = $twig;
47
        $this->elementTypeNameGenerator = $elementTypeNameGenerator;
48
        $this->replacer = $replacer;
49
        $this->barcodeGenerator = $barcodeGenerator;
50
        $this->sandboxedTwigProvider = $sandboxedTwigProvider;
51
        $this->security = $security;
52
        $this->partdb_title = $partdb_title;
53
    }
54
55
    public function getLabelHTML(LabelOptions $options, array $elements): string
56
    {
57
        if (empty($elements)) {
58
            throw new \InvalidArgumentException('$elements must not be empty');
59
        }
60
61
        $twig_elements = [];
62
63
        if ($options->getLinesMode() === 'twig') {
64
            $sandboxed_twig = $this->sandboxedTwigProvider->getTwig($options);
65
            $current_user = $this->security->getUser();
66
        }
67
68
        $page = 1;
69
        foreach ($elements as $element) {
70
            if ($options->getLinesMode() === 'twig' && isset($sandboxed_twig) && isset($current_user)) {
71
                try {
72
                    $lines = $sandboxed_twig->render(
73
                        'lines',
74
                        [
75
                            'element' => $element,
76
                            'page' => $page,
77
                            'user' => $current_user,
78
                            'install_title' => $this->partdb_title,
79
                        ]
80
                    );
81
                } catch (Error $exception) {
82
                    throw new TwigModeException($exception);
83
                }
84
            } else {
85
                $lines = $this->replacer->replace($options->getLines(), $element);
86
            }
87
88
            $twig_elements[] = [
89
                'element' => $element,
90
                'lines' => $lines,
91
                'barcode' => $this->barcodeGenerator->generateSVG($options, $element),
92
                'barcode_content' => $this->barcodeGenerator->getContent($options, $element),
93
            ];
94
95
            $page++;
96
        }
97
98
99
        return $this->twig->render('LabelSystem/labels/base_label.html.twig', [
100
            'meta_title' => $this->getPDFTitle($options, $elements[0]),
101
            'elements' => $twig_elements,
102
            'options' => $options,
103
        ]);
104
    }
105
106
107
    protected function getPDFTitle(LabelOptions $options, object $element)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

107
    protected function getPDFTitle(/** @scrutinizer ignore-unused */ LabelOptions $options, object $element)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        if ($element instanceof NamedElementInterface) {
110
            return $this->elementTypeNameGenerator->getTypeNameCombination($element, false);
111
        }
112
113
        return 'Part-DB label';
114
    }
115
}