Completed
Push — master ( d03358...143fc5 )
by Nicolaas
02:11
created

TemplateOverviewPage   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 41
lcom 1
cbo 10
dl 0
loc 257
rs 8.2769
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B ontroller::init() 0 15 5
A ontroller::showmore() 0 18 2
A ontroller::ConfigurationDetails() 0 17 4
A ontroller::clearalltemplatedescriptions() 0 9 3
A ontroller::TestTaskLink() 0 4 1
A ontroller::QuickListLink() 0 4 1
A ontroller::ImagesListLink() 0 4 1
A ontroller::quicklist() 0 7 2
B ontroller::listofobjectsused() 0 22 5

How to fix   Complexity   

Complex Class

Complex classes like TemplateOverviewPage 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 TemplateOverviewPage, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *@author: nicolaas [at] sunnysideup.co.nz
4
 *@description Add a page to your site that allows you to view all the html that can be used in the typography section - if applied correctly.
5
 */
6
7
8
9
class TemplateOverviewPage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        "showmore" => true,
13
        "quicklist" => true,
14
        "listofobjectsused" => true,
15
        "clearalltemplatedescriptions" => "ADMIN"
16
    );
17
18
    public function init()
19
    {
20
        parent::init();
21
        if (!Director::is_cli() && !Director::isDev() && !Permission::check('ADMIN')) {
22
            return Security::permissionFailure();
23
        }
24
        Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
25
        Requirements::javascript('templateoverview/javascript/TemplateOverviewPage.js');
26
        Requirements::css("templateoverview/css/TemplateOverviewPage.css");
27
        if (class_exists("PrettyPhoto")) {
28
            PrettyPhoto::include_code();
29
        } else {
30
            user_error("It is recommended that you install the Sunny Side Up Pretty Photo Module", E_USER_NOTICE);
31
        }
32
    }
33
34
    public function showmore($request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
35
    {
36
        $id = $request->param("ID");
37
        $obj = SiteTree::get()->byID(intval($id));
38
        if ($obj) {
39
            $className = $obj->ClassName;
40
            $data = $className::get()
41
                ->filter(array("ClassName" => $obj->ClassName))
42
                ->limit(200);
43
            $array = array(
44
                "Results" => $data,
45
                "MoreDetail" => TemplateOverviewDescription::get()->filter(array("ClassNameLink" => $obj->ClassName))->First()
46
            );
47
        } else {
48
            $array = array();
49
        }
50
        return $this->customise($array)->renderWith("TemplateOverviewPageShowMoreList");
51
    }
52
53
54
    public function ConfigurationDetails()
55
    {
56
        $m = Member::currentUser();
57
        if ($m) {
58
            if ($m->inGroup("ADMIN")) {
59
                $baseFolder = Director::baseFolder();
60
                $myFile = $baseFolder."/".$this->project()."/_config.php";
61
                $fh = fopen($myFile, 'r');
62
                $string = '';
63
                while (!feof($fh)) {
64
                    $string .= fgets($fh, 1024);
65
                }
66
                fclose($fh);
67
                return $string;
68
            }
69
        }
70
    }
71
72
    public function clearalltemplatedescriptions()
73
    {
74
        if ($m = Member::currentUser()) {
75
            if ($m->inGroup("ADMIN")) {
76
                DB::query("DELETE FROM TemplateOverviewDescription");
77
                die("all descriptions have been deleted");
0 ignored issues
show
Coding Style Compatibility introduced by
The method clearalltemplatedescriptions() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
78
            }
79
        }
80
    }
81
82
    public function TestTaskLink()
83
    {
84
        return "/dev/tasks/CheckAllTemplates/";
85
    }
86
87
    public function QuickListLink()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
88
    {
89
        return $this->Link("quicklist");
90
    }
91
92
    public function ImagesListLink()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
93
    {
94
        return $this->Link("listofobjectsused/Image");
95
    }
96
97
    public function quicklist()
98
    {
99
        $list = $this->ListOfAllClasses();
100
        foreach ($list as $item) {
101
            DB::alteration_message($item->ClassName);
102
        }
103
    }
104
105
    public function listofobjectsused($request)
106
    {
107
        $classWeAreLookingFor = $request->param("ID");
108
        $classWeAreLookingFor = singleton($classWeAreLookingFor);
109
        if ($classWeAreLookingFor instanceof DataObject) {
110
            $list = $this->ListOfAllClasses();
111
            foreach ($list as $item) {
112
                $config = Config::inst();
113
                $listOfImages = $config->get($item->ClassName, "has_one")
114
                 + $config->get($item->ClassName, "has_many")
115
                 + $config->get($item->ClassName, "many_many");
116
                foreach ($listOfImages as $fieldName => $potentialImage) {
117
                    $innerSingleton = singleton($potentialImage);
118
                    if ($innerSingleton instanceof $classWeAreLookingFor) {
119
                        DB::alteration_message($item->ClassName.".". $fieldName);
120
                    }
121
                }
122
            }
123
        } else {
124
            user_error("Please specify the ID for the model you are looking for - e.g. /listofobjectsused/Image/", E_USER_ERROR);
125
        }
126
    }
127
}
128