Failed Conditions
Push — master ( 96da53...0afbc1 )
by Andreas
06:02 queued 03:20
created

Subscribe::show()   C

Complexity

Conditions 11
Paths 72

Size

Total Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 72
nop 0
dl 0
loc 93
rs 6.006
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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\Ui;
4
5
use dokuwiki\Form\Form;
6
7
/**
8
 * DokuWiki Subscribe Interface
9
 *
10
 * @package dokuwiki\Ui
11
 */
12
class Subscribe extends Ui
13
{
14
    /**
15
     * Display the subscribe form
16
     *
17
     * @author Adrian Lang <[email protected]>
18
     *
19
     * @return void
20
     */
21
    public function show()
22
    {
23
        global $INPUT;
24
        global $INFO;
25
        global $ID;
26
        global $lang;
27
        global $conf;
28
        $stime_days = $conf['subscribe_time'] / 60 / 60 / 24;
29
30
        // print intro
31
        echo p_locale_xhtml('subscr_form');
32
33
        // list up current subscriptions
34
        echo '<h2>'.$lang['subscr_m_current_header'].'</h2>';
35
        echo '<div class="level2">';
36
        if ($INFO['subscribed'] === false) {
37
            echo '<p>'.$lang['subscr_m_not_subscribed'].'</p>';
38
        } else {
39
            echo '<ul>';
40
            foreach ($INFO['subscribed'] as $sub) {
41
                echo '<li><div class="li">';
42
                if ($sub['target'] !== $ID) {
43
                    echo '<code class="ns">'.hsc(prettyprint_id($sub['target'])).'</code>';
44
                } else {
45
                    echo '<code class="page">'.hsc(prettyprint_id($sub['target'])).'</code>';
46
                }
47
                $sstl = sprintf($lang['subscr_style_'.$sub['style']], $stime_days);
48
                if (!$sstl) $sstl = hsc($sub['style']);
49
                echo ' ('.$sstl.') ';
50
51
                echo '<a href="'.wl(
52
                    $ID,
53
                    array(
54
                         'do'        => 'subscribe',
55
                         'sub_target'=> $sub['target'],
56
                         'sub_style' => $sub['style'],
57
                         'sub_action'=> 'unsubscribe',
58
                         'sectok'    => getSecurityToken()
59
                    )
60
                ).
61
                    '" class="unsubscribe">'.$lang['subscr_m_unsubscribe'].
62
                    '</a></div></li>';
63
            }
64
            echo '</ul>';
65
        }
66
        echo '</div>';
67
68
        // Add new subscription form
69
        echo '<h2>'.$lang['subscr_m_new_header'].'</h2>';
70
        echo '<div class="level2">';
71
        $ns      = getNS($ID).':';
72
        $targets = [
73
            $ID => '<code class="page">'.prettyprint_id($ID).'</code>',
74
            $ns => '<code class="ns">'.prettyprint_id($ns).'</code>',
75
        ];
76
        $styles = [
77
            'every'  => $lang['subscr_style_every'],
78
            'digest' => sprintf($lang['subscr_style_digest'], $stime_days),
79
            'list'   => sprintf($lang['subscr_style_list'], $stime_days),
80
        ];
81
82
        // create the form
83
        $form = new Form(['id' => 'subscribe__form']);
84
        $form->addTagOpen('div')->addClass('no');
85
        $form->setHiddenField('id', $ID);
86
        $form->setHiddenField('do', 'subscribe');
87
        $form->setHiddenField('sub_action', 'subscribe');
88
89
        $form->addFieldsetOpen($lang['subscr_m_subscribe']);
90
        $value = (array_key_exists($INPUT->post->str('sub_target'), $targets)) ?
91
                 $INPUT->str('sub_target') : key($targets);
92
        foreach ($targets as $val => $label) {
93
            $data = ($value === $val) ? ['checked' => 'checked'] : [];
94
            $form->addRadioButton('sub_target', $label)->val($val)->attrs($data);
95
        }
96
        $form->addFieldsetClose();
97
98
        $form->addFieldsetOpen($lang['subscr_m_receive']);
99
        $value = (array_key_exists($INPUT->post->str('sub_style'), $styles)) ?
100
                 $INPUT->str('sub_style') : key($styles);
101
        foreach ($styles as $val => $label) {
102
            $data = ($value === $val) ? ['checked' => 'checked'] : [];
103
            $form->addRadioButton('sub_style', $label)->val($val)->attrs($data);
104
        }
105
        $form->addFieldsetClose();
106
107
        $form->addButton('do[subscribe]', $lang['subscr_m_subscribe'])->attr('type', 'submit');
108
        $form->addTagClose('div');
109
110
        print $form->toHTML('Subscribe');
111
112
        echo '</div>';
113
    }
114
115
}
116