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

GlobalProviders::replace()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 34
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 58
rs 8.0555

How to fix   Long Method   

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\UserSystem\User;
25
use IntlDateFormatter;
26
use Locale;
27
use Symfony\Component\Security\Core\Security;
28
29
/**
30
 * Provides Placeholders for infos about global infos like Installation name or datetimes.
31
 * @package App\Services\LabelSystem\PlaceholderProviders
32
 */
33
final class GlobalProviders implements PlaceholderProviderInterface
34
{
35
36
    private $partdb_title;
37
    private $security;
38
39
    public function __construct(string $partdb_title, Security $security)
40
    {
41
        $this->partdb_title = $partdb_title;
42
        $this->security = $security;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function replace(string $placeholder, object $label_target, array $options = []): ?string
49
    {
50
        if ($placeholder === "[[INSTALL_NAME]]") {
51
            return $this->partdb_title;
52
        }
53
54
55
        $user = $this->security->getUser();
56
        if ($placeholder === "[[USERNAME]]") {
57
            if ($user instanceof User) {
58
                return $user->getName();
59
            }
60
            return 'anonymous';
61
        }
62
63
        if ($placeholder === "[[USERNAME_FULL]]") {
64
            if ($user instanceof User) {
65
                return $user->getFullName(true);
66
            }
67
            return 'anonymous';
68
        }
69
70
        $now = new \DateTime();
71
72
        if ($placeholder === '[[DATETIME]]') {
73
            $formatter = IntlDateFormatter::create(
74
                Locale::getDefault(),
75
                IntlDateFormatter::SHORT,
76
                IntlDateFormatter::SHORT,
77
                $now->getTimezone()
0 ignored issues
show
Bug introduced by
$now->getTimezone() of type DateTimeZone is incompatible with the type string expected by parameter $timezone of IntlDateFormatter::create(). ( Ignorable by Annotation )

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

77
                /** @scrutinizer ignore-type */ $now->getTimezone()
Loading history...
78
            );
79
80
            return $formatter->format($now);
81
        }
82
83
        if ($placeholder === '[[DATE]]') {
84
            $formatter = IntlDateFormatter::create(
85
                Locale::getDefault(),
86
                IntlDateFormatter::SHORT,
87
                IntlDateFormatter::NONE,
88
                $now->getTimezone()
89
            );
90
91
            return $formatter->format($now);
92
        }
93
94
        if ($placeholder === '[[TIME]]') {
95
            $formatter = IntlDateFormatter::create(
96
                Locale::getDefault(),
97
                IntlDateFormatter::NONE,
98
                IntlDateFormatter::SHORT,
99
                $now->getTimezone()
100
            );
101
102
            return $formatter->format($now);
103
        }
104
105
        return null;
106
    }
107
}