ZombieReport   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 297
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 158
dl 0
loc 297
rs 9.1199
c 0
b 0
f 0
wmc 41

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get_additional_parameters() 0 3 1
A create() 0 3 1
B get_ceiling() 0 14 8
A get_action() 0 12 2
A format_email() 0 3 1
A format_status() 0 5 1
A display_parameters() 0 9 2
A get_active_only() 0 8 3
A get_parameters_form() 0 26 2
A is_valid() 0 5 2
A format_active() 0 14 2
A get_data() 0 23 2
A display() 0 13 3
A perform_action() 0 20 5
A display_data() 0 42 3
A get_parameters() 0 32 1
A count() 0 7 1

How to fix   Complexity   

Complex Class

Complex classes like ZombieReport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ZombieReport, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Description of zombie_report.
5
 *
6
 * @copyright (c) 2012 University of Geneva
7
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
8
 * @author Laurent Opprecht <[email protected]>
9
 */
10
class ZombieReport implements Countable
11
{
12
    protected $additional_parameters = [];
13
14
    protected $parameters_form = null;
15
16
    public function __construct($additional_parameters = [])
17
    {
18
        $this->additional_parameters = $additional_parameters;
19
    }
20
21
    /**
22
     * @return ZombieReport
23
     */
24
    public static function create($additional_parameters = [])
25
    {
26
        return new self($additional_parameters);
27
    }
28
29
    public function get_additional_parameters()
30
    {
31
        return $this->additional_parameters;
32
    }
33
34
    public function get_parameters()
35
    {
36
        $result = [
37
            'items' => [
38
                [
39
                    'name' => 'ceiling',
40
                    'label' => get_lang('LastAccess'),
41
                    'type' => 'date_picker',
42
                    'default' => $this->get_ceiling('Y-m-d'),
43
                    'rules' => [
44
                        [
45
                            'type' => 'date',
46
                            'message' => get_lang('Date'),
47
                        ],
48
                    ],
49
                ],
50
                [
51
                    'name' => 'active_only',
52
                    'label' => get_lang('ActiveOnly'),
53
                    'type' => 'checkbox',
54
                    'default' => $this->get_active_only(),
55
                ],
56
                [
57
                    'name' => 'submit_button',
58
                    'type' => 'button',
59
                    'value' => get_lang('Search'),
60
                    'attributes' => ['class' => 'search'],
61
                ],
62
            ],
63
        ];
64
65
        return $result;
66
    }
67
68
    /**
69
     * @return FormValidator
70
     */
71
    public function get_parameters_form()
72
    {
73
        $form = new FormValidator(
74
            'zombie_report_parameters',
75
            'get',
76
            null,
77
            null,
78
            ['class' => 'well form-horizontal form-search']
79
        );
80
81
        $form->addDatePicker('ceiling', get_lang('LastAccess'));
82
        $form->addCheckBox('active_only', get_lang('ActiveOnly'));
83
        $form->addButtonSearch(get_lang('Search'));
84
85
        $params = [
86
            'active_only' => $this->get_active_only(),
87
            'ceiling' => $this->get_ceiling('Y-m-d'),
88
        ];
89
        $form->setDefaults($params);
90
        $additional = $this->get_additional_parameters();
91
        foreach ($additional as $key => $value) {
92
            $value = Security::remove_XSS($value);
93
            $form->addHidden($key, $value);
94
        }
95
96
        return $form;
97
    }
98
99
    public function display_parameters($return = false)
100
    {
101
        $form = $this->get_parameters_form();
102
        $result = $form->returnForm();
103
104
        if ($return) {
105
            return $result;
106
        } else {
107
            echo $result;
108
        }
109
    }
110
111
    public function is_valid()
112
    {
113
        $form = $this->get_parameters_form();
114
115
        return $form->isSubmitted() == false || $form->validate();
116
    }
117
118
    public function get_ceiling($format = null)
119
    {
120
        $result = Request::get('ceiling');
121
        $result = $result ? $result : ZombieManager::last_year();
122
123
        $result = is_array($result) && count($result) == 1 ? reset($result) : $result;
124
        $result = is_array($result) ? mktime(0, 0, 0, $result['F'], $result['d'], $result['Y']) : $result;
125
        $result = is_numeric($result) ? (int) $result : $result;
126
        $result = is_string($result) ? strtotime($result) : $result;
127
        if ($format) {
128
            $result = date($format, $result);
129
        }
130
131
        return $result;
132
    }
133
134
    public function get_active_only()
135
    {
136
        $result = Request::get('active_only', false);
137
        $result = $result === 'true' ? true : $result;
138
        $result = $result === 'false' ? false : $result;
139
        $result = (bool) $result;
140
141
        return $result;
142
    }
143
144
    public function get_action()
145
    {
146
        /**
147
         * todo check token.
148
         */
149
        $check = Security::check_token('post');
150
        Security::clear_token();
151
        if (!$check) {
152
            return 'display';
153
        }
154
155
        return Request::post('action', 'display');
156
    }
157
158
    public function perform_action()
159
    {
160
        $ids = Request::post('id');
161
        if (empty($ids)) {
162
            return $ids;
163
        }
164
165
        $action = $this->get_action();
166
        switch ($action) {
167
            case 'activate':
168
                return UserManager::activate_users($ids);
169
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
170
            case 'deactivate':
171
                return UserManager::deactivate_users($ids);
172
                break;
173
            case 'delete':
174
                return UserManager::delete_users($ids);
175
        }
176
177
        return false;
178
    }
179
180
    public function count()
181
    {
182
        $ceiling = $this->get_ceiling();
183
        $active_only = $this->get_active_only();
184
        $items = ZombieManager::listZombies($ceiling, $active_only, null, null);
185
186
        return count($items);
187
    }
188
189
    public function get_data($from, $count, $column, $direction)
190
    {
191
        $ceiling = $this->get_ceiling();
192
        $active_only = $this->get_active_only();
193
        $items = ZombieManager::listZombies($ceiling, $active_only, $from, $count, $column, $direction);
194
        $result = [];
195
        foreach ($items as $item) {
196
            $row = [];
197
            $row[] = $item['user_id'];
198
            $row[] = $item['official_code'];
199
            $row[] = $item['firstname'];
200
            $row[] = $item['lastname'];
201
            $row[] = $item['username'];
202
            $row[] = $item['email'];
203
            $row[] = $item['status'];
204
            $row[] = $item['auth_source'];
205
            $row[] = api_format_date($item['registration_date'], DATE_FORMAT_SHORT);
206
            $row[] = api_format_date($item['login_date'], DATE_FORMAT_SHORT);
207
            $row[] = $item['active'];
208
            $result[] = $row;
209
        }
210
211
        return $result;
212
    }
213
214
    public function display_data($return = false)
215
    {
216
        $count = [$this, 'count'];
217
        $data = [$this, 'get_data'];
218
219
        $parameters = [];
220
        $parameters['sec_token'] = Security::get_token();
221
        $parameters['ceiling'] = $this->get_ceiling();
222
        $parameters['active_only'] = $this->get_active_only() ? 'true' : 'false';
223
        $additional_parameters = $this->get_additional_parameters();
224
        $parameters = array_merge($additional_parameters, $parameters);
225
226
        $table = new SortableTable('zombie_users', $count, $data, 1, 50);
227
        $table->set_additional_parameters($parameters);
228
229
        $col = 0;
230
        $table->set_header($col++, '', false);
231
        $table->set_header($col++, get_lang('OfficialCode'));
232
        $table->set_header($col++, get_lang('FirstName'));
233
        $table->set_header($col++, get_lang('LastName'));
234
        $table->set_header($col++, get_lang('LoginName'));
235
        $table->set_header($col++, get_lang('Email'));
236
        $table->set_header($col++, get_lang('Profile'));
237
        $table->set_header($col++, get_lang('AuthenticationSource'));
238
        $table->set_header($col++, get_lang('RegisteredDate'));
239
        $table->set_header($col++, get_lang('LastAccess'), false);
240
        $table->set_header($col, get_lang('Active'), false);
241
242
        $table->set_column_filter(5, [$this, 'format_email']);
243
        $table->set_column_filter(6, [$this, 'format_status']);
244
        $table->set_column_filter(10, [$this, 'format_active']);
245
246
        $table->set_form_actions([
247
            'activate' => get_lang('Activate'),
248
            'deactivate' => get_lang('Deactivate'),
249
            'delete' => get_lang('Delete'),
250
        ]);
251
252
        if ($return) {
253
            return $table->return_table();
254
        } else {
255
            echo $table->return_table();
256
        }
257
    }
258
259
    /**
260
     * Table formatter for the active column.
261
     *
262
     * @param string $active
263
     *
264
     * @return string
265
     */
266
    public function format_active($active)
267
    {
268
        $active = $active == '1';
269
        if ($active) {
270
            $image = 'accept';
271
            $text = get_lang('Yes');
272
        } else {
273
            $image = 'error';
274
            $text = get_lang('No');
275
        }
276
277
        $result = Display::return_icon($image.'.png', $text);
278
279
        return $result;
280
    }
281
282
    public function format_status($status)
283
    {
284
        $statusname = api_get_status_langvars();
285
286
        return $statusname[$status];
287
    }
288
289
    public function format_email($email)
290
    {
291
        return Display::encrypted_mailto_link($email, $email);
292
    }
293
294
    public function display($return = false)
295
    {
296
        $result = $this->display_parameters($return);
297
        $valid = $this->perform_action();
298
299
        if ($valid) {
300
            echo Display::return_message(get_lang('Updated'), 'confirmation');
301
        }
302
303
        $result .= $this->display_data($return);
304
305
        if ($return) {
306
            return $result;
307
        }
308
    }
309
}
310