Link   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 250
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
B _init() 0 29 4
B href() 0 32 5
A current() 0 14 1
A share() 0 10 1
B params() 0 51 5
A transform() 0 18 3
1
<?php
2
3
/**
4
 * Creates the target url for links
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   URL
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\url;
17
18
/**
19
 * Creates the target url for links
20
 *
21
 * PHP Version 5
22
 *
23
 * @category  Core
24
 * @package   URL
25
 * @author    Hans-Joachim Piepereit <[email protected]>
26
 * @copyright 2013 cSphere Team
27
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
28
 * @link      http://www.csphere.eu
29
 **/
30
31
abstract class Link
32
{
33
    /**
34
     * Checks if options are fetched already
35
     **/
36
    private static $_init = false;
37
38
    /**
39
     * Stores if ajax links are activated
40
     **/
41
    private static $_ajax = false;
42
43
    /**
44
     * Stores if pretty links are activated
45
     **/
46
    private static $_pretty = false;
47
48
    /**
49
     * Stores the request array data
50
     **/
51
    private static $_request = '';
52
53
    /**
54
     * Stores the plugin part of links
55
     **/
56
    private static $_plugin = '';
57
58
    /**
59
     * Stores the action part of links
60
     **/
61
    private static $_action = '';
62
63
    /**
64
     * Stores the key part of links
65
     **/
66
    private static $_key = '';
67
68
    /**
69
     * Stores the value part of links
70
     **/
71
    private static $_value = '';
72
73
    /**
74
     * Fetch options and set flags
75
     *
76
     * @return void
77
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
78
79
    private static function _init()
80
    {
81
        self::$_request = \csphere\core\http\Request::get();
82
83
        // Fetch view options
84
        $loader = \csphere\core\service\Locator::get();
85
        $view   = $loader->load('view');
86
87
        self::$_ajax   = $view->getOption('links_ajax');
88
        self::$_pretty = $view->getOption('links_pretty');
89
90
        // Set flags
91
        if (empty(self::$_pretty) && empty(self::$_ajax)) {
92
93
            self::$_plugin = '?plugin=';
94
            self::$_action = '&amp;action=';
95
            self::$_key   = '&amp;';
96
            self::$_value = '=';
97
98
        } else {
99
100
            self::$_plugin = empty(self::$_ajax) ? '' : '#';
101
            self::$_action = '/';
102
            self::$_key   = '/';
103
            self::$_value = '/';
104
        }
105
106
        self::$_init = true;
107
    }
108
109
    /**
110
     * Creates a link for the specified parameters
111
     *
112
     * @param string $plugin Plugin
113
     * @param string $action Action
114
     * @param array  $params Array of key value pairs where value can be empty
115
     *
116
     * @return string
117
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
118
119
    public static function href($plugin, $action, array $params = [])
120
    {
121
        // Check and set options if init is not done yet
122
        if (self::$_init == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
123
124
            self::_init();
125
        }
126
127
        // @TODO: Use urlencode on dynamic parts
128
129
        // Add plugin and action
130
        $link = self::$_request['dirname'] . self::$_plugin . $plugin;
131
132
        if ($action != '') {
133
134
            $link .= self::$_action . $action;
135
        }
136
137
        // Add params
138
        unset($params['plugin'], $params['action'], $params['']);
139
140
        foreach ($params AS $key => $value) {
141
142
            // Only add params that are not empty
143
            if ($value != '') {
144
145
                $link .= self::$_key . $key . self::$_value . $value;
146
            }
147
        }
148
149
        return $link;
150
    }
151
152
    /**
153
     * Creates a link for the current parameters
154
     *
155
     * @return string
156
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
157
158
    public static function current()
159
    {
160
        $plugin = self::$_request['data']['plugin'];
161
        $action = self::$_request['data']['action'];
162
        $params = self::$_request['data'];
163
164
        // Unset plugin and action from params
165
        unset($params['plugin'], $params['action']);
166
167
        // Proceed with usual link creation now
168
        $result = self::href($plugin, $action, $params);
0 ignored issues
show
Documentation introduced by
$params is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
170
        return $result;
171
    }
172
173
    /**
174
     * Combines the request data for sharing the link
175
     *
176
     * @param string $link Unencoded local part e.g. generated by href method
177
     *
178
     * @return string
179
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
180
181
    public static function share($link)
182
    {
183
        // Next to the request dns should be a slash provided by the link variable
184
        $result = self::$_request['protocol']
185
                . '://'
186
                . self::$_request['dns']
187
                . $link;
188
189
        return $result;
190
    }
191
192
    /**
193
     * Prepares link urls
194
     *
195
     * @param string $url The internal url that is used in slash syntax
196
     *
197
     * @throws \Exception
198
     *
199
     * @return string
200
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
201
202
    public static function params($url)
203
    {
204
        $link = '';
205
206
        // Determine plugin and action
207
        $target = explode('/', $url, 2);
208
        $plugin = $target[0];
209
210
        if (empty($plugin)) {
211
212
            throw new \Exception('URL without plugin: ' . $url);
213
214
        } else {
215
216
            // Sort link parameters
217
            $params = [];
218
219
            if (empty($target[1])) {
220
221
                $action = '';
222
            } else {
223
224
                // Remove action from parameters
225
                $split  = explode('/', $target[1]);
226
                $action = $split[0];
227
228
                unset($split[0]);
229
230
                // Create valid key value pairs
231
                $split_key = '';
232
233
                foreach ($split AS $splitted) {
234
235
                    if ($split_key == '') {
236
237
                        $split_key          = $splitted;
238
                        $params[$split_key] = '';
239
240
                    } else {
241
242
                        $params[$split_key] = $splitted;
243
                        $split_key          = '';
244
                    }
245
                }
246
            }
247
248
            $link .= self::href($plugin, $action, $params);
249
        }
250
251
        return $link;
252
    }
253
254
    /**
255
     * Transforms a slash-separated string to an key value array
256
     *
257
     * @param string $url The internal url that is used in slash syntax
258
     *
259
     * @return array
260
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
261
262
    public static function transform($url)
263
    {
264
        $params = [];
265
        $split  = explode('/', $url);
266
        $splits = count($split);
267
268
        // Just use every second entry due to key value structure
269
        for ($i = 0; $i < $splits; $i+=2) {
270
271
            // Not every value might contain data
272
            $params[$split[$i]] = isset($split[($i+1)]) ? $split[($i+1)] : '';
273
        }
274
275
        // Clear empty keys
276
        unset($params['']);
277
278
        return $params;
279
    }
280
}
281