Base   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 289
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 289
rs 10
c 0
b 0
f 0
wmc 22
lcom 2
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A map() 0 14 3
B message() 0 27 2
B view() 0 26 5
B breadcrumb() 0 32 4
A form() 0 15 2
A callData() 0 13 2
A callRecord() 0 13 2
1
<?php
2
3
/**
4
 * Patterns for usual plugin actions
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   RAD
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\rad;
17
18
/**
19
 * Patterns for usual plugin actions
20
 *
21
 * @category  Core
22
 * @package   RAD
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Base
30
{
31
    /**
32
     * Plugin name
33
     **/
34
    protected $plugin = '';
35
36
    /**
37
     * Database table name
38
     **/
39
    protected $table = '';
40
41
    /**
42
     * Database schema name
43
     **/
44
    protected $schema = '';
45
46
    /**
47
     * Action name
48
     **/
49
    protected $action = '';
50
51
    /**
52
     * Template file name
53
     **/
54
    protected $tpl = '';
55
56
    /**
57
     * Service Loader object
58
     **/
59
    protected $loader = null;
60
61
    /**
62
     * View driver object
63
     **/
64
    protected $view = null;
65
66
    /**
67
     * Previous action
68
     **/
69
    protected $previous = 'list';
70
71
    /**
72
     * Data closure
73
     **/
74
    private $_data = null;
75
76
    /**
77
     * Record closure
78
     **/
79
    private $_record = null;
80
81
    /**
82
     * Prepare database specific settings
83
     *
84
     * @param string $plugin Plugin name
85
     * @param string $table  Database table name if it differs from plugin name
86
     *
87
     * @return \csphere\core\rad\Base
88
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
89
90
    public function __construct($plugin, $table = '')
91
    {
92
        // Set class options
93
        $this->plugin = $plugin;
94
        $this->table = $table;
95
        $this->schema = $plugin;
96
97
        if (!empty($table)) {
98
99
            $this->schema .= '_' . $table;
100
        }
101
102
        // Get important objects
103
        $this->loader = \csphere\core\service\Locator::get();
104
        $this->view = $this->loader->load('view');
105
    }
106
107
    /**
108
     * Set template specific settings
109
     *
110
     * @param string $action   Action name if it differs from method name
111
     * @param string $tpl      Template file name
112
     * @param string $previous Adds the previous action to breadcrumb
113
     *
114
     * @return boolean
115
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
116
117
    public function map($action = '', $tpl = '', $previous = '')
118
    {
119
        $this->action = $action;
120
        $this->tpl = $tpl;
121
        $this->previous = ($previous == '') ? 'list' : $previous;
122
123
        // Manage should be handled like list
124
        if ($action == 'manage') {
125
126
            $this->previous = 'manage';
127
        }
128
129
        return true;
130
    }
131
132
    /**
133
     * If only a message is required for output
134
     *
135
     * @param string  $key  Key for translation in default plugin
136
     * @param integer $rid  Record ID if important for URL
137
     * @param string  $type Type of message: default, red, green
138
     *
139
     * @return void
140
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
141
142
    protected function message($key, $rid = 0, $type = 'default')
143
    {
144
        // Get message and action from translation
145
        $msg = \csphere\core\translation\Fetch::key('default', $key);
146
        $act = \csphere\core\translation\Fetch::exists('default', $this->action);
147
148
        $plugin = ($act == true) ? 'default' : $this->plugin;
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...
149
150
        $act = \csphere\core\translation\Fetch::key($plugin, $this->action);
151
        $plg = \csphere\core\translation\Fetch::key($this->plugin, $this->plugin);
152
153
        $previous = \csphere\core\url\Link::href(
154
            $this->plugin, $this->previous
155
        );
156
157
        // Data array
158
        $data = ['action_name' => $act,
159
            'plugin_name' => $plg,
160
            'message' => $msg,
161
            'previous' => $previous,
162
            'type' => $type];
163
164
        // Change template file and send to view method
165
        $this->tpl = 'message';
166
167
        $this->view($data, $rid, true);
168
    }
169
170
    /**
171
     * Send data array to template file
172
     *
173
     * @param array   $data       Data as an array
174
     * @param integer $rid        Record ID if important for URL
175
     * @param boolean $skip       Whether to skip the callback
176
     * @param boolean $breadcrumb Activate Breadcrumb
177
     *
178
     * @return void
179
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
180
181
    protected function view(array $data, $rid = 0, $skip = false,
182
        $breadcrumb = false
183
    ) {
184
185
        if ($breadcrumb) {
186
            // Set breadcrumb
187
            $this->breadcrumb($rid);
188
        }
189
190
        // Set plugin and action
191
        $data['plugin'] = $this->plugin;
192
        $data['action'] = $this->action;
193
194
        // Apply registered closure
195
        if ($skip != true && is_callable($this->_data)) {
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...
196
197
            $func = call_user_func($this->_data, $data[$this->schema]);
198
199
            $data[$this->schema] = $func;
200
        }
201
202
        $plugin = $this->tpl == 'message' ? 'default' : $this->plugin;
203
204
        // Pass data to template
205
        $this->view->template($plugin, $this->tpl, $data);
206
    }
207
208
    /**
209
     * Generate breadcrumb navigation
210
     *
211
     * @param integer $rid Record ID if important for URL
212
     *
213
     * @return void
214
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
215
216
    protected function breadcrumb($rid = 0)
217
    {
218
        // All actions that depend on manage get an admin link
219
        if ($this->previous == 'manage') {
220
221
            $bread = new \csphere\core\template\Breadcrumb('admin');
222
            $bread->add('content');
223
            $bread->plugin($this->plugin, 'manage');
224
225
        } else {
226
227
            $bread = new \csphere\core\template\Breadcrumb($this->plugin);
228
            $bread->plugin($this->plugin, $this->previous);
229
        }
230
231
        // Just add an additional link if action and previous action differ
232
        if ($this->previous != $this->action) {
233
234
            $url = $this->plugin . '/' . $this->action;
235
236
            // Requests for a specific ID must contain it here
237
            if (!empty($rid)) {
238
239
                $url .= '/id/' . (int)$rid;
240
            }
241
242
            // Add current action
243
            $bread->add($this->action, $url);
244
        }
245
246
        $bread->trace();
247
    }
248
249
    /**
250
     * Record data should be replaced with new content of form submit
251
     *
252
     * @param array $record Record as an array
253
     * @param array $new    New content as an array
254
     *
255
     * @return array
256
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
257
258
    protected function form(array $record, array $new)
259
    {
260
        // Move new data into record array
261
        $record = array_merge($record, $new);
262
263
        unset($record['csphere_form']);
264
265
        // Apply registered closure
266
        if (is_callable($this->_record)) {
267
268
            $record = call_user_func($this->_record, $record);
269
        }
270
271
        return $record;
272
    }
273
274
    /**
275
     * Set closure for template data
276
     *
277
     * @param \Closure $closure Closure
278
     *
279
     * @return boolean
280
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
281
282
    public final function callData($closure)
283
    {
284
        $result = false;
285
286
        if ($closure instanceof \Closure) {
287
288
            $this->_data = $closure;
289
290
            $result = true;
291
        }
292
293
        return $result;
294
    }
295
296
    /**
297
     * Set closure for record data before persistency gets involved
298
     *
299
     * @param \Closure $closure Closure
300
     *
301
     * @return boolean
302
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
303
304
    public final function callRecord($closure)
305
    {
306
        $result = false;
307
308
        if ($closure instanceof \Closure) {
309
310
            $this->_record = $closure;
311
312
            $result = true;
313
        }
314
315
        return $result;
316
    }
317
}
318