PartProvider::replace()   D
last analyzed

Complexity

Conditions 24
Paths 24

Size

Total Lines 69
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 34
c 0
b 0
f 0
nc 24
nop 3
dl 0
loc 69
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2022 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 - 2022 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software: you can redistribute it and/or modify
29
 * it under the terms of the GNU Affero General Public License as published
30
 * by the Free Software Foundation, either version 3 of the License, or
31
 * (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 Affero General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU Affero General Public License
39
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
40
 */
41
42
namespace App\Services\LabelSystem\PlaceholderProviders;
43
44
use App\Entity\Parts\Part;
45
use App\Services\Formatters\SIFormatter;
46
use Parsedown;
47
use Symfony\Contracts\Translation\TranslatorInterface;
48
49
final class PartProvider implements PlaceholderProviderInterface
50
{
51
    private SIFormatter $siFormatter;
52
    private TranslatorInterface $translator;
53
54
    public function __construct(SIFormatter $SIFormatter, TranslatorInterface $translator)
55
    {
56
        $this->siFormatter = $SIFormatter;
57
        $this->translator = $translator;
58
    }
59
60
    public function replace(string $placeholder, object $part, array $options = []): ?string
61
    {
62
        if (!$part instanceof Part) {
63
            return null;
64
        }
65
66
        if ('[[CATEGORY]]' === $placeholder) {
67
            return $part->getCategory() ? $part->getCategory()->getName() : '';
68
        }
69
70
        if ('[[CATEGORY_FULL]]' === $placeholder) {
71
            return $part->getCategory() ? $part->getCategory()->getFullPath() : '';
72
        }
73
74
        if ('[[MANUFACTURER]]' === $placeholder) {
75
            return $part->getManufacturer() ? $part->getManufacturer()->getName() : '';
76
        }
77
78
        if ('[[MANUFACTURER_FULL]]' === $placeholder) {
79
            return $part->getManufacturer() ? $part->getManufacturer()->getFullPath() : '';
80
        }
81
82
        if ('[[FOOTPRINT]]' === $placeholder) {
83
            return $part->getFootprint() ? $part->getFootprint()->getName() : '';
84
        }
85
86
        if ('[[FOOTPRINT_FULL]]' === $placeholder) {
87
            return $part->getFootprint() ? $part->getFootprint()->getFullPath() : '';
88
        }
89
90
        if ('[[MASS]]' === $placeholder) {
91
            return $part->getMass() ? $this->siFormatter->format($part->getMass(), 'g', 1) : '';
92
        }
93
94
        if ('[[MPN]]' === $placeholder) {
95
            return $part->getManufacturerProductNumber();
96
        }
97
98
        if ('[[TAGS]]' === $placeholder) {
99
            return $part->getTags();
100
        }
101
102
        if ('[[M_STATUS]]' === $placeholder) {
103
            if ('' === $part->getManufacturingStatus()) {
104
                return '';
105
            }
106
107
            return $this->translator->trans('m_status.'.$part->getManufacturingStatus());
108
        }
109
110
        $parsedown = new Parsedown();
111
112
        if ('[[DESCRIPTION]]' === $placeholder) {
113
            return $parsedown->line($part->getDescription());
114
        }
115
116
        if ('[[DESCRIPTION_T]]' === $placeholder) {
117
            return strip_tags($parsedown->line($part->getDescription()));
118
        }
119
120
        if ('[[COMMENT]]' === $placeholder) {
121
            return $parsedown->line($part->getComment());
122
        }
123
124
        if ('[[COMMENT_T]]' === $placeholder) {
125
            return strip_tags($parsedown->line($part->getComment()));
126
        }
127
128
        return null;
129
    }
130
}
131