Completed
Push — master ( a9b611...61b36b )
by Matthew
05:54 queued 04:26
created

ActionGridColumn::format()   B

Complexity

Conditions 10
Paths 33

Size

Total Lines 47
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 37
c 2
b 1
f 0
dl 0
loc 47
rs 7.6666
cc 10
nc 33
nop 2

How to fix   Complexity   

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
namespace Dtc\GridBundle\Grid\Column;
4
5
use Dtc\GridBundle\Grid\Source\GridSourceInterface;
6
use Symfony\Component\Routing\RouterInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Routing\RouterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class ActionGridColumn extends AbstractGridColumn
9
{
10
    static protected $spinnerHtml = '<i class="fa fa-circle-o-notch fa-spin dtc-grid-hidden"></i> ';
11
12
    protected $actions;
13
    protected $idField;
14
15
    /** @var RouterInterface */
16
    protected $router;
17
    protected $gridSourceId;
18
19
    public function __construct($field, array $actions, $idField = 'id')
20
    {
21
        $this->actions = $actions;
22
        $this->idField = $idField;
23
        $this->label = 'Actions';
24
        $this->field = $field;
25
    }
26
27
    public function format($object, GridSourceInterface $gridsource)
28
    {
29
        $method = 'get'.ucfirst($this->idField);
30
        $id = $object->$method();
31
        $idHtml = htmlspecialchars($id);
32
        $content = '';
33
        foreach ($this->actions as $action => $options) {
34
            $label = $options['label'];
35
            if ($content) {
36
                $content .= ' ';
37
            }
38
            $route = isset($options['route']) ? $options['route'] : '';
39
            switch ($options['action']) {
40
                case 'show':
41
                    if (!$route) {
42
                        $route = 'dtc_grid_show';
43
                    }
44
                    $uri = $this->router->generate($route, ['identifier' => $id, 'id' => $this->gridSourceId]);
45
                    $uri = htmlspecialchars($uri);
46
                    $content .= "<button class=\"btn btn-primary grid-show\" data-route=\"$uri\" data-id=\"$idHtml\"";
47
                    $content .= "onclick=\"dtc_grid_show(this)\">$label</button>";
48
                    break;
49
                case 'delete':
50
                    if (!$route) {
51
                        $route = 'dtc_grid_delete';
52
                    }
53
                    $uri = $this->router->generate($route, ['identifier' => $id, 'id' => $this->gridSourceId]);
54
                    $uri = htmlspecialchars($uri);
55
                    $content .= "<button class=\"btn btn-primary grid-delete\" data-route=\"$uri\" data-id=\"$idHtml\"";
56
                    $content .= "onclick=\"dtc_grid_delete(this)\">" . static::$spinnerHtml . "$label</button>";
57
                    break;
58
                default:
59
                    $uri = $this->router->generate($route, ['identifier' => $id, 'id' => $this->gridSourceId]);
60
                    $uri = htmlspecialchars($uri);
61
                    $content .= "<button class \"";
62
                    if (isset($options['button_class'])) {
63
                        $content .= " " . $options['button_class'];
64
                    }
65
                    $content .= " data-route=\"$uri\" data-id=\"$idHtml\"";
66
                    if (isset($options['onclick'])) {
67
                        $content .= " onclick=\"" . htmlspecialchars($options['onclick']) . "\"";
68
                    }
69
                    $content .= ">$label</button>";
70
            }
71
        }
72
73
        return $content;
74
    }
75
76
    public function setRouter(RouterInterface $router)
77
    {
78
        $this->router = $router;
79
    }
80
81
    public function setGridSourceId($gridSourceId)
82
    {
83
        $this->gridSourceId = $gridSourceId;
84
    }
85
}
86