Completed
Push — master ( 332a47...cbc99e )
by WEBEWEB
03:08
created

RoleLabelUtilityTwigExtension::getExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\Utility;
13
14
use Symfony\Component\Security\Core\User\UserInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use Twig_SimpleFunction;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\LabelComponentTwigExtension;
18
use WBW\Library\Core\Utility\Argument\StringUtility;
19
20
/**
21
 * Role label utility Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility
25
 */
26
class RoleLabelUtilityTwigExtension extends AbstractUtilityTwigExtension {
27
28
    /**
29
     * Service name.
30
     *
31
     * @var string
32
     */
33
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.utility.rolelabel";
34
35
    /**
36
     * Extension.
37
     *
38
     * @var LabelComponentTwigExtension
39
     */
40
    private $extension;
41
42
    /**
43
     * Translator.
44
     *
45
     * @var TranslatorInterface
46
     */
47
    private $translator;
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param LabelComponentTwigExtension $extension The label component Twig extension.
53
     */
54
    public function __construct(TranslatorInterface $translator, LabelComponentTwigExtension $extension) {
55
        parent::__construct();
56
        $this->setExtension($extension);
57
        $this->setTranslator($translator);
58
    }
59
60
    /**
61
     * Apply the color.
62
     *
63
     * @param string $label The label.
64
     * @param string $content The content.
65
     * @param string $color The color.
66
     * @return string Returns the label with applied color.
67
     */
68
    private function applyColor($label, $content, $color) {
69
70
        $searches = ">" . $content;
71
        $replaces = " style=\"background-color:" . $color . ";\"" . $searches;
72
73
        return StringUtility::replace($label, [$searches], [$replaces]);
74
    }
75
76
    /**
77
     * Display a Bootstrap role label.
78
     *
79
     * @param UserInterface $user The user.
80
     * @param array $roleColors The role colors.
81
     * @param array $roleTrans The role translations.
82
     * @return string Returns the Bootstrap role label.
83
     */
84
    public function bootstrapRoleLabelFunction(UserInterface $user = null, array $roleColors = [], array $roleTrans = []) {
85
86
        // Check the user.
87
        if (null === $user) {
88
            return "";
89
        }
90
91
        // Initialize the ouptut.
92
        $output = [];
93
94
        // Handle each role.
95
        foreach ($user->getRoles() as $current) {
96
97
            // Initialize the translation.
98
            $trans = $current;
99
            if (true === array_key_exists($current, $roleTrans)) {
100
                $trans = $this->getTranslator()->trans($roleTrans[$current]);
101
            }
102
103
            // Initialize the label.
104
            $label = $this->getExtension()->bootstrapLabelDefaultFunction(["content" => $trans]);
105
            if (true === array_key_exists($current, $roleColors)) {
106
                $label = $this->applyColor($label, $trans, $roleColors[$current]);
0 ignored issues
show
Bug introduced by
It seems like $trans defined by $current on line 98 can also be of type object<Symfony\Component\Security\Core\Role\Role>; however, WBW\Bundle\BootstrapBund...Extension::applyColor() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
            }
108
109
            // Add the label.
110
            $output[] = $label;
111
        }
112
113
        // Return the output.
114
        return implode(" ", $output);
115
    }
116
117
    /**
118
     * Get the Twig functions.
119
     *
120
     * @return array Returns the Twig functions.
121
     */
122
    public function getFunctions() {
123
        return [
124
            new Twig_SimpleFunction("bootstrapRoleLabel", [$this, "bootstrapRoleLabelFunction"], ["is_safe" => ["html"]]),
125
        ];
126
    }
127
128
    /**
129
     * Get the extension.
130
     *
131
     * @return LabelComponentTwigExtension Returns the extension.
132
     */
133
    public function getExtension() {
134
        return $this->extension;
135
    }
136
137
    /**
138
     * Get the translator.
139
     *
140
     * @return TranslatorInterface Returns the translator.
141
     */
142
    public function getTranslator() {
143
        return $this->translator;
144
    }
145
146
    /**
147
     * Set the extension.
148
     *
149
     * @param LabelComponentTwigExtension $extension The extension.
150
     * @return RoleLabelUtilityTwigExtension Returns this role label utility Twig extension.
151
     */
152
    protected function setExtension(LabelComponentTwigExtension $extension) {
153
        $this->extension = $extension;
154
        return $this;
155
    }
156
157
    /**
158
     * Set the translator.
159
     *
160
     * @param TranslatorInterface $translator The translator.
161
     * @return RoleLabelUtilityTwigExtension Returns this role label utility Twig extension.
162
     */
163
    protected function setTranslator(TranslatorInterface $translator) {
164
        $this->translator = $translator;
165
        return $this;
166
    }
167
168
}
169