Passed
Push — master ( 2d425f...eb03d1 )
by Jan
04:57 queued 10s
created

IconLinkColumn   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 98
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHref() 0 11 3
A render() 0 18 3
A configureOptions() 0 16 1
A normalize() 0 3 1
A getDisabled() 0 10 3
A getTitle() 0 11 3
A getIcon() 0 11 3
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\DataTables\Column;
22
23
24
use Omines\DataTablesBundle\Column\AbstractColumn;
25
use Symfony\Component\OptionsResolver\OptionsResolver;
26
27
class IconLinkColumn extends AbstractColumn
28
{
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function normalize($value)
34
    {
35
        return $value;
36
    }
37
38
    public function configureOptions(OptionsResolver $resolver)
39
    {
40
        parent::configureOptions($resolver);
41
        $resolver->setDefaults([
42
                                   'icon' => 'fas fa-fw fa-edit',
43
                                   'title' => null,
44
                                   'href' => null,
45
                                   'disabled' => false,
46
                               ]);
47
48
        $resolver->setAllowedTypes('title', ['null', 'string', 'callable']);
49
        $resolver->setAllowedTypes('icon', ['null', 'string', 'callable']);
50
        $resolver->setAllowedTypes('href', ['null', 'string', 'callable']);
51
        $resolver->setAllowedTypes('disabled', ['bool', 'callable']);
52
53
        return $this;
54
    }
55
56
    public function render($value, $context)
57
    {
58
        $href = $this->getHref($value, $context);
59
        $icon = $this->getIcon($value, $context);
60
        $title = $this->getTitle($value, $context);
61
        $disabled = $this->getDisabled($value, $context);
62
63
        if ($href !== null) {
64
            return sprintf(
65
                '<a class="btn btn-primary btn-sm %s" href="%s" title="%s"><i class="%s"></i></a>',
66
                $disabled ? 'disabled' : '',
67
                $href,
68
                $title,
69
                $icon
70
            );
71
        }
72
73
        return "";
74
    }
75
76
    protected function getDisabled($value, $context): bool
77
    {
78
        $provider = $this->options['disabled'];
79
        if (is_bool($provider)) {
80
            return $provider;
81
        }
82
        if (is_callable($provider)) {
83
            return call_user_func($provider, $value, $context);
84
        }
85
        return false;
86
    }
87
88
    protected function getHref($value, $context): ?string
89
    {
90
        $provider = $this->options['href'];
91
        if (is_string($provider)) {
92
            return $provider;
93
        }
94
        if (is_callable($provider)) {
95
            return call_user_func($provider, $value, $context);
96
        }
97
98
        return null;
99
    }
100
101
    protected function getIcon($value, $context): ?string
102
    {
103
        $provider = $this->options['icon'];
104
        if (is_string($provider)) {
105
            return $provider;
106
        }
107
        if (is_callable($provider)) {
108
            return call_user_func($provider, $value, $context);
109
        }
110
111
        return null;
112
    }
113
114
    protected function getTitle($value, $context): ?string
115
    {
116
        $provider = $this->options['title'];
117
        if (is_string($provider)) {
118
            return $provider;
119
        }
120
        if (is_callable($provider)) {
121
            return call_user_func($provider, $value, $context);
122
        }
123
124
        return null;
125
    }
126
}