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

PartLotProvider::replace()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
c 1
b 0
f 0
nc 13
nop 3
dl 0
loc 49
rs 6.6166

How to fix   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\PartLot;
25
use App\Services\AmountFormatter;
26
use App\Services\LabelSystem\LabelTextReplacer;
27
use IntlDateFormatter;
28
use Locale;
29
30
final class PartLotProvider implements PlaceholderProviderInterface
31
{
32
    private $labelTextReplacer;
33
    private $amountFormatter;
34
35
    public function __construct(LabelTextReplacer $labelTextReplacer, AmountFormatter $amountFormatter)
36
    {
37
        $this->labelTextReplacer = $labelTextReplacer;
38
        $this->amountFormatter = $amountFormatter;
39
    }
40
41
    public function replace(string $placeholder, object $label_target, array $options = []): ?string
42
    {
43
        if ($label_target instanceof PartLot) {
44
            if ($placeholder === '[[LOT_ID]]') {
45
                return $label_target->getID() ?? 'unknown';
46
            }
47
48
            if ($placeholder === '[[LOT_NAME]]') {
49
                return $label_target->getName();
50
            }
51
52
            if ($placeholder === '[[LOT_COMMENT]]') {
53
                return $label_target->getComment();
54
            }
55
56
            if ($placeholder === '[[EXPIRATION_DATE]]') {
57
                if ($label_target->getExpirationDate() === null) {
58
                    return '';
59
                }
60
                $formatter = IntlDateFormatter::create(
61
                    Locale::getDefault(),
62
                    IntlDateFormatter::SHORT,
63
                    IntlDateFormatter::NONE
64
                //$label_target->getExpirationDate()->getTimezone()
65
                );
66
67
                return $formatter->format($label_target->getExpirationDate());
68
            }
69
70
            if ($placeholder === '[[AMOUNT]]') {
71
                if ($label_target->isInstockUnknown()) {
72
                    return '?';
73
                }
74
                return $this->amountFormatter->format($label_target->getAmount(), $label_target->getPart()->getPartUnit());
75
            }
76
77
            if ($placeholder === '[[LOCATION]]') {
78
                return $label_target->getStorageLocation() ? $label_target->getStorageLocation()->getName() : '';
79
            }
80
81
            if ($placeholder === '[[LOCATION_FULL]]') {
82
                return $label_target->getStorageLocation() ? $label_target->getStorageLocation()->getFullPath() : '';
83
            }
84
85
86
            return $this->labelTextReplacer->handlePlaceholder($placeholder, $label_target->getPart());
87
        }
88
89
        return null;
90
    }
91
}