GenerateLink   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 130
rs 10
c 2
b 1
f 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A getFormattedValue() 0 17 2
A getViewRenderer() 0 4 1
A setViewRenderer() 0 6 1
A getRoute() 0 4 1
A setRoute() 0 4 1
A getRouteParams() 0 4 1
A setRouteParams() 0 4 1
A getRouteKey() 0 4 1
A setRouteKey() 0 4 1
1
<?php
2
3
namespace ZfcDatagrid\Column\Formatter;
4
5
use Zend\ServiceManager\ServiceLocatorInterface;
6
use Zend\View\Renderer\RendererInterface;
7
use ZfcDatagrid\Column\AbstractColumn;
8
9
class GenerateLink extends AbstractFormatter
10
{
11
    /** @var array */
12
    protected $validRenderers = [
13
        'jqGrid',
14
        'bootstrapTable',
15
    ];
16
17
    /** @var string */
18
    protected $route;
19
    /** @var array */
20
    protected $routeParams;
21
    /** @var string|null */
22
    protected $routeKey;
23
    /** @var \Zend\View\Renderer\PhpRenderer */
24
    protected $viewRenderer;
25
26
    /**
27
     * @param ServiceLocatorInterface|RendererInterface $viewRenderer
28
     * @param                                           $route
29
     * @param null                                      $key
30
     * @param array                                     $params
31
     */
32
    public function __construct($viewRenderer, $route, $key = null, $params = [])
33
    {
34
        /*
35
         * old fallback that should be removed in 2.0
36
         * TODO remove in 2.0
37
         */
38
        if (!$viewRenderer instanceof RendererInterface) {
39
            $viewRenderer = $viewRenderer->get('ViewRenderer');
40
        }
41
42
        $this->setViewRenderer($viewRenderer);
43
        $this->setRoute($route);
44
        $this->setRouteParams($params);
45
        $this->setRouteKey($key);
46
    }
47
48
    /**
49
     * @param AbstractColumn $column
50
     *
51
     * @return string
52
     */
53
    public function getFormattedValue(AbstractColumn $column)
54
    {
55
        $row = $this->getRowData();
56
        $value = $row[$column->getUniqueId()];
57
58
        $routeKey = !is_null($this->getRouteKey()) ?
59
            $this->getRouteKey()
60
            :
61
            $column->getUniqueId();
62
63
        $params = $this->getRouteParams();
64
        $params[$routeKey] = $value;
65
66
        $url = (string) $this->getViewRenderer()->url($this->getRoute(), $params);
67
68
        return sprintf('<a href="%s">%s</a>', $url, $value);
69
    }
70
71
    /**
72
     * @return \Zend\View\Renderer\PhpRenderer
73
     */
74
    public function getViewRenderer()
75
    {
76
        return $this->viewRenderer;
77
    }
78
79
    /**
80
     * @param \Zend\View\Renderer\PhpRenderer $viewRenderer
81
     *
82
     * @return self
83
     */
84
    public function setViewRenderer($viewRenderer)
85
    {
86
        $this->viewRenderer = $viewRenderer;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getRoute()
95
    {
96
        return $this->route;
97
    }
98
99
    /**
100
     * @param string $route
101
     */
102
    public function setRoute($route)
103
    {
104
        $this->route = $route;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getRouteParams()
111
    {
112
        return $this->routeParams;
113
    }
114
115
    /**
116
     * @param array $routeParams
117
     */
118
    public function setRouteParams($routeParams)
119
    {
120
        $this->routeParams = $routeParams;
121
    }
122
123
    /**
124
     * @return null|string
125
     */
126
    public function getRouteKey()
127
    {
128
        return $this->routeKey;
129
    }
130
131
    /**
132
     * @param null|string $routeKey
133
     */
134
    public function setRouteKey($routeKey)
135
    {
136
        $this->routeKey = $routeKey;
137
    }
138
}
139