TableButtonTwigExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A bootstrapRowButtonDefaultFunction() 0 7 1
A bootstrapRowButtonDeleteFunction() 0 7 1
A bootstrapRowButtonEditFunction() 0 7 1
A getFunctions() 0 7 1
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 Twig\Environment;
15
use Twig\TwigFunction;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtension;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\CSS\ButtonTwigExtensionTrait;
18
use WBW\Bundle\CoreBundle\Component\Translation\BaseTranslatorInterface;
19
use WBW\Library\Core\Argument\Helper\ArrayHelper;
20
21
/**
22
 * Table button Twig extension.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility
26
 */
27
class TableButtonTwigExtension extends AbstractUtilityTwigExtension {
28
29
    use ButtonTwigExtensionTrait;
30
31
    /**
32
     * Service name.
33
     *
34
     * @var string
35
     */
36
    const SERVICE_NAME = "wbw.bootstrap.twig.extension.utility.table_button";
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param Environment $twigEnvironment The Twig environment.
42
     * @param BaseTranslatorInterface $translator The translator.
43
     * @param ButtonTwigExtension $extension The button Twig extension.
44
     */
45
    public function __construct(Environment $twigEnvironment, $translator, ButtonTwigExtension $extension) {
46
        parent::__construct($twigEnvironment, $translator);
47
        $this->setButtonTwigExtension($extension);
48
    }
49
50
    /**
51
     * Displays a Bootstrap row button "default".
52
     *
53
     * @param array $args The arguments.
54
     * @return string Returns the Bootstrap form button "default".
55
     */
56
    public function bootstrapRowButtonDefaultFunction(array $args = []): string {
57
58
        $editButton   = $this->bootstrapRowButtonEditFunction(["href" => ArrayHelper::get($args, "edit_href")]);
59
        $deleteButton = $this->bootstrapRowButtonDeleteFunction(["href" => ArrayHelper::get($args, "delete_href")]);
60
61
        return implode(" ", [$editButton, $deleteButton]);
62
    }
63
64
    /**
65
     * Displays a Bootstrap row button "delete".
66
     *
67
     * @param array $args The arguments.
68
     * @return string Returns the Bootstrap row button "delete".
69
     */
70
    public function bootstrapRowButtonDeleteFunction(array $args = []): string {
71
72
        $txt = $this->getTranslator()->trans("label.delete", [], "WBWBootstrapBundle");
73
        $but = $this->getButtonTwigExtension()->bootstrapButtonDangerFunction(["title" => $txt, "icon" => "g:trash"]);
74
75
        return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($but, ArrayHelper::get($args, "href", self::DEFAULT_HREF));
76
    }
77
78
    /**
79
     * Displays a Bootstrap row button "edit".
80
     *
81
     * @param array $args The arguments.
82
     * @return string Returns the Bootstrap row button "edit".
83
     */
84
    public function bootstrapRowButtonEditFunction(array $args = []): string {
85
86
        $txt = $this->getTranslator()->trans("label.edit", [], "WBWBootstrapBundle");
87
        $but = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["title" => $txt, "icon" => "g:pencil"]);
88
89
        return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($but, ArrayHelper::get($args, "href", self::DEFAULT_HREF));
90
    }
91
92
    /**
93
     * Get the Twig functions.
94
     *
95
     * @return TwigFunction[] Returns the Twig functions.
96
     */
97
    public function getFunctions(): array {
98
        return [
99
            new TwigFunction("bootstrapRowButtonDefault", [$this, "bootstrapRowButtonDefaultFunction"], ["is_safe" => ["html"]]),
100
            new TwigFunction("bootstrapRowButtonDelete", [$this, "bootstrapRowButtonDeleteFunction"], ["is_safe" => ["html"]]),
101
            new TwigFunction("bootstrapRowButtonEdit", [$this, "bootstrapRowButtonEditFunction"], ["is_safe" => ["html"]]),
102
        ];
103
    }
104
}
105