EnabledBadgeRendererTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 6
dl 0
loc 32
c 1
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A renderEnabledBadge() 0 10 5
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2022 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\JQuery\DataTablesBundle\Renderer\Assets;
13
14
use Symfony\Contracts\Translation\TranslatorInterface;
15
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\BadgeTwigExtension;
16
use WBW\Bundle\JQuery\DataTablesBundle\WBWJQueryDataTablesBundle;
17
18
/**
19
 * Enabled badge renderer trait.
20
 *
21
 * @author webeweb <https://github.com/webeweb>
22
 * @package WBW\Bundle\JQuery\DataTablesBundle\Renderer\Assets
23
 */
24
trait EnabledBadgeRendererTrait {
25
26
    /**
27
     * Get the badge Twig extension.
28
     *
29
     * @return BadgeTwigExtension|null Returns the badge Twig extension.
30
     */
31
    abstract public function getBadgeTwigExtension(): ?BadgeTwigExtension;
32
33
    /**
34
     * Get the translator.
35
     *
36
     * @return TranslatorInterface|null Returns the translator.
37
     */
38
    abstract public function getTranslator(): ?TranslatorInterface;
39
40
    /**
41
     * Render an enabled badge.
42
     *
43
     * @param bool|null $enabled Enabled ?
44
     * @return string|null Returns the rendered enabled badge.
45
     */
46
    protected function renderEnabledBadge(?bool $enabled): ?string {
47
48
        if (null === $this->getTranslator() || null === $this->getBadgeTwigExtension()) {
49
            return null;
50
        }
51
52
        $method  = sprintf("bootstrapBadge%sFunction", true === $enabled ? "Success" : "Danger");
53
        $content = $this->getTranslator()->trans(true === $enabled ? "label.enabled" : "label.disabled", [], WBWJQueryDataTablesBundle::getTranslationDomain());
54
55
        return $this->getBadgeTwigExtension()->$method(["content" => $content]);
56
    }
57
}
58