SiteController::Menu()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.7579
c 0
b 0
f 0
cc 5
nc 5
nop 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
 * The base site controller.
4
 */
5
class SiteController extends Controller
6
{
7
8
    public function init()
9
    {
10
        RSSFeed::linkToFeed("add-ons/rss", "New modules on addons.silverstripe.org");
11
12
        Requirements::javascript("//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
13
        Requirements::javascript("themes/".SSViewer::current_theme()."/bootstrap/js/bootstrap.min.js");
0 ignored issues
show
Deprecated Code introduced by
The method SSViewer::current_theme() has been deprecated with message: 4.0 Use the "SSViewer.theme" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
14
        Requirements::javascript("themes/".SSViewer::current_theme()."/javascript/addons.js");
0 ignored issues
show
Deprecated Code introduced by
The method SSViewer::current_theme() has been deprecated with message: 4.0 Use the "SSViewer.theme" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
15
        Requirements::javascript("//www.google.com/jsapi");
16
        Requirements::javascript("themes/".SSViewer::current_theme()."/javascript/chart.js");
0 ignored issues
show
Deprecated Code introduced by
The method SSViewer::current_theme() has been deprecated with message: 4.0 Use the "SSViewer.theme" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
17
        parent::init();
18
    }
19
20
    public function Menu()
21
    {
22
        $menu = new ArrayList();
23
24
        $menuEntries = array(
25
            //array('controller' => 'HomeController'),
26
            array('controller' => 'AddonsController'),
27
            array(
28
                'link' => Controller::join_links(
29
                    singleton('AddonsController')->Link(),
30
                    '?type=theme&view=expanded'
31
                ),
32
                'title' => 'Themes',
33
            ),
34
            array('controller' => 'VendorsController'),
35
            array('controller' => 'AuthorsController'),
36
            array('controller' => 'TagsController'),
37
            array('controller' => 'SubmitAddonController'),
38
        );
39
40
        foreach ($menuEntries as $menuEntry) {
41
            if (isset($menuEntry['controller'])) {
42
                $inst = singleton($menuEntry['controller']);
43
                $active = false;
44
45
                foreach (self::$controller_stack as $candidate) {
46
                    $active = (
47
                        $candidate instanceof $menuEntry['controller']
48
                        && $this->request->getVar('type') != 'theme'
49
                    );
50
                }
51
52
                $menu->push(new ArrayData(array(
53
                    'Title' => $inst->Title(),
54
                    'Link' => $inst->Link(),
55
                    'Active' => $active,
56
                    'MenuItemType' => $inst->MenuItemType()
57
                )));
58
            } else {
59
                $active = ($this->request->getVar('type') == 'theme');
60
                $menu->push(new ArrayData(array(
61
                    'Title' => $menuEntry['title'],
62
                    'Link' => $menuEntry['link'],
63
                    'Active' => $active,
64
                    'MenuItemType' => 'link',
65
                )));
66
            }
67
        }
68
69
        return $menu;
70
    }
71
72
    /**
73
     * @return String 'link' or 'button'
74
     */
75
    public function MenuItemType()
76
    {
77
        return 'link';
78
    }
79
80
    public function GATrackingCode()
81
    {
82
        return defined('GA_TRACKING_CODE') ? GA_TRACKING_CODE : null;
83
    }
84
85
    public function LinkWithSearch($extraParamStr = '')
86
    {
87
        $params = array_diff_key($this->request->getVars(), array('url' => null));
88
        parse_str($extraParamStr, $extraParams);
89
        $params = array_merge($params, (array)$extraParams);
90
91
        return Controller::join_links($this->Link(), '?' . http_build_query($params));
92
    }
93
}
94