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

PartProvider::replace()   D

Complexity

Conditions 24
Paths 24

Size

Total Lines 68
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 24
eloc 34
c 1
b 0
f 0
nc 24
nop 3
dl 0
loc 68
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 - 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\PlaceholderProviders;
22
23
24
use App\Entity\Parts\Part;
25
use App\Services\SIFormatter;
26
use Symfony\Contracts\Translation\TranslatorInterface;
27
28
final class PartProvider implements PlaceholderProviderInterface
29
{
30
31
    private $siFormatter;
32
    private $translator;
33
34
    public function __construct(SIFormatter $SIFormatter, TranslatorInterface $translator)
35
    {
36
        $this->siFormatter = $SIFormatter;
37
        $this->translator = $translator;
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function replace(string $placeholder, object $part, array $options = []): ?string
44
    {
45
        if (!$part instanceof Part) {
46
            return null;
47
        }
48
49
        if ($placeholder === '[[CATEGORY]]') {
50
            return $part->getCategory() ? $part->getCategory()->getName() : '';
51
        }
52
53
        if ($placeholder === '[[CATEGORY_FULL]]') {
54
            return $part->getCategory() ? $part->getCategory()->getFullPath() : '';
55
        }
56
57
        if ($placeholder === '[[MANUFACTURER]]') {
58
            return $part->getManufacturer() ? $part->getManufacturer()->getName() : '';
59
        }
60
61
        if ($placeholder === '[[MANUFACTURER_FULL]]') {
62
            return $part->getManufacturer() ? $part->getManufacturer()->getFullPath() : '';
63
        }
64
65
        if ($placeholder === '[[FOOTPRINT]]') {
66
            return $part->getFootprint() ? $part->getFootprint()->getName() : '';
67
        }
68
69
        if ($placeholder === '[[FOOTPRINT_FULL]]') {
70
            return $part->getFootprint() ? $part->getFootprint()->getFullPath() : '';
71
        }
72
73
        if ($placeholder === '[[MASS]]') {
74
            return $part->getMass() ? $this->siFormatter->format($part->getMass(), 'g', 1) : '';
75
        }
76
77
        if ($placeholder === '[[MPN]]') {
78
            return $part->getManufacturerProductNumber();
79
        }
80
81
        if ($placeholder === '[[TAGS]]') {
82
            return $part->getTags();
83
        }
84
85
        if ($placeholder === '[[M_STATUS]]') {
86
            if ($part->getManufacturingStatus() === '') {
87
                return '';
88
            }
89
            return $this->translator->trans('m_status.' . $part->getManufacturingStatus());
90
        }
91
92
        $parsedown = new \Parsedown();
93
94
        if ($placeholder === '[[DESCRIPTION]]') {
95
            return $parsedown->line($part->getDescription());
96
        }
97
98
        if ($placeholder === '[[DESCRIPTION_T]]') {
99
            return strip_tags($parsedown->line($part->getDescription()));
100
        }
101
102
        if ($placeholder === '[[COMMENT]]') {
103
            return $parsedown->line($part->getComment());
104
        }
105
106
        if ($placeholder === '[[COMMENT_T]]') {
107
            return strip_tags($parsedown->line($part->getComment()));
108
        }
109
110
        return null;
111
    }
112
}