GlossaryEditQueue::approve()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 63
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 5
nop 1
dl 0
loc 63
rs 8.8497
c 0
b 0
f 0

How to fix   Long Method   

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
 * GlossaryEditQueue Class
4
 *
5
 * @package TheyWorkForYou
6
 */
7
8
namespace MySociety\TheyWorkForYou;
9
10
/**
11
 * Glossary Edit Queue
12
 */
13
14
// Glossary overrides
15
class GlossaryEditQueue extends EditQueue {
16
    public function approve($data) {
17
        // Approve items for inclusion
18
        // Create new epobject and update the editqueue
19
20
        global $THEUSER;
21
22
        // We need a list of editqueue items to play with
23
        $this->get_pending();
24
        if (!isset($this->pending)) {
25
            return false;
26
        }
27
        $timestamp = date('Y-m-d H:i:s', time());
28
29
        foreach ($data['approvals'] as $approval_id) {
30
            // create a new epobject
31
            //      title VARCHAR(255),
32
            //      body TEXT,
33
            //      type INTEGER,
34
            //      created DATETIME,
35
            //      modified DATETIME,
36
            /*print "<pre>";
37
            print_r($data);
38
            print "</pre>";*/
39
            // Check to see that we actually have something to approve
40
            if (!isset($this->pending[$approval_id])) {
41
                break;
42
            }
43
            $q = $this->db->query("INSERT INTO glossary
44
                            (title, body, type, created, visible)
45
                            VALUES
46
                            ('" . addslashes($this->pending[$approval_id]['title']) . "',
47
                            '" . addslashes($this->pending[$approval_id]['body']) . "',
48
                            '" . $data['epobject_type'] . "',
49
                            '" . $timestamp . "',
50
                            1);");
51
52
            // If that didn't work we can't go any further...
53
            if (!$q->success()) {
54
                print "glossary trouble";
55
                return false;
56
            }
57
            $this->current_epobject_id = $q->insert_id();
0 ignored issues
show
Bug Best Practice introduced by
The property current_epobject_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
59
            // Then finally update the editqueue with
60
            // the new epobject id and approval details.
61
            $q = $this->db->query("UPDATE editqueue
62
                            SET
63
                            glossary_id='" . $this->current_epobject_id . "',
64
                            editor_id='" . addslashes($THEUSER->user_id()) . "',
65
                            approved='1',
66
                            decided='" . $timestamp . "'
67
                            WHERE edit_id=" . $approval_id . ";");
68
            if (!$q->success()) {
69
                break;
70
            } else {
71
                // Scrub that one from the list of pending items
72
                unset($this->pending[$approval_id]);
73
            }
74
        }
75
76
        $this->update_pending_count();
77
78
        return true;
79
    }
80
81
}
82