Completed
Push — actionrefactor ( 6e4bf0 )
by Andreas
04:36
created

Subscribe::handleSubscribeData()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 34
nc 15
nop 0
dl 0
loc 54
rs 7.8331
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
namespace dokuwiki\Action;
4
5
/**
6
 * Class Subscribe
7
 *
8
 * E-Mail subscription handling
9
 *
10
 * @package dokuwiki\Action
11
 */
12
class Subscribe extends AbstractUserAction {
13
14
    /** @inheritdoc */
15
    function minimumPermission() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
        return AUTH_READ;
17
    }
18
19
    /** @inheritdoc */
20
    public function preProcess() {
21
        $act = $this->actionname;
22
        try {
23
            $act = $this->handleSubscribeData();
24
        } catch(\Exception $e) {
25
            msg($e->getMessage(), -1);
26
        }
27
        return $act;
28
    }
29
30
    /** @inheritdoc */
31
    public function tplContent() {
32
        tpl_subscribe();
33
    }
34
35
    /**
36
     * Handle page 'subscribe'
37
     *
38
     * Throws exception on error.
39
     *
40
     * @author Adrian Lang <[email protected]>
41
     *
42
     * @return string action command
43
     * @throws \Exception if (un)subscribing fails
44
     */
45
    protected function handleSubscribeData() {
46
        global $lang;
47
        global $INFO;
48
        global $ID;
49
        global $INPUT;
50
51
        // get and preprocess data.
52
        $params = array();
53
        foreach(array('target', 'style', 'action') as $param) {
54
            if($INPUT->has("sub_$param")) {
55
                $params[$param] = $INPUT->str("sub_$param");
56
            }
57
        }
58
59
        // any action given? if not just return and show the subscription page
60
        if(empty($params['action']) || !checkSecurityToken()) return $this->actionname;
61
62
        // Handle POST data, may throw exception.
63
        trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
64
65
        $target = $params['target'];
66
        $style = $params['style'];
67
        $action = $params['action'];
68
69
        // Perform action.
70
        $sub = new \Subscription();
71
        if($action == 'unsubscribe') {
72
            $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
73
        } else {
74
            $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
75
        }
76
77
        if($ok) {
78
            msg(
79
                sprintf(
80
                    $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
81
                    prettyprint_id($target)
82
                ), 1
83
            );
84
            act_redirect($ID, $this->actionname);
85
        } else {
86
            throw new \Exception(
87
                sprintf(
88
                    $lang["subscr_{$action}_error"],
89
                    hsc($INFO['userinfo']['name']),
90
                    prettyprint_id($target)
91
                )
92
            );
93
        }
94
95
        // Assure that we have valid data if act_redirect somehow fails. should never be reached
96
        $INFO['subscribed'] = $sub->user_subscription();
97
        return 'show';
98
    }
99
100
}
101