EVCSelectPage_Controller   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 8
dl 0
loc 155
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 1
A SelectForm() 0 20 1
A doselectform() 0 6 1
A nextstep() 0 21 4
A getValue() 0 10 4
A getMakes() 0 15 3
A getModels() 0 16 4
B getTypes() 0 21 7
A pleaseSelectPhrase() 0 4 1
1
<?php
2
3
4
class EVCSelectPage extends Page
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...
5
{
6
}
7
8
9
class EVCSelectPage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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
        "SelectForm",
13
        "nextstep"
14
    );
15
16
    protected $steps = array(
17
        "Year",
18
        "Make",
19
        "Model",
20
        "Type",
21
        "ODO"
22
    );
23
24
    public function init()
25
    {
26
        parent::init();
27
        Requirements::themedCSS('ElectricVehicleCalculatorSelectPage', 'electric-vehicle-calculator');
28
        Requirements::javascript("framework/thirdparty/jquery/jquery.js");
29
        Requirements::javascript('electric-vehicle-calculator/javascript/EVCSelectPage.js');
30
        Requirements::customScript("EVCSelectPage.baseLink = '".$this->AbsoluteLink()."';", "EVCSelectPageBasics");
31
    }
32
33
    public function SelectForm()
34
    {
35
        $fieldList = new FieldList(
36
            NumericField::create("Year", "Manufacturing Year"),
37
            DropdownField::create("Make", "Make", array(0 => $this->pleaseSelectPhrase())),
38
            DropdownField::create("Model", "Model", array(0 => $this->pleaseSelectPhrase())),
39
            DropdownField::create("Type", "Type", array(0 => $this->pleaseSelectPhrase())),
40
            NumericField::create("ODO", "Current Odometer (overall distance travelled - as shown in your dashboard")
41
        );
42
        $actions = new FieldList(
43
            FormAction::create("doselectform")->setTitle("Start Calculation")
44
        );
45
        $form = Form::create(
46
            $this,
47
            "SelectForm",
48
            $fieldList,
49
            $actions
50
        );
51
        return $form;
52
    }
53
54
    public function doselectform($data, $form)
55
    {
56
        $params = array();
57
        print_r(EdmundsAPI::get_data("/api/tco/newtruecosttoownbystyleidandzip/".$data["Type"]."/90019", $params));
58
        die("------------");
0 ignored issues
show
Coding Style Compatibility introduced by
The method doselectform() 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...
59
    }
60
61
62
    /**
63
     * action
64
     * set value and returns next list
65
     * @param HTTPRequest
66
     */
67
    public function nextstep($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...
68
    {
69
        $key = $request->param("ID");
70
        if (!in_array($key, $this->steps)) {
71
            return user_error("setting unknown value: $key");
72
        } else {
73
            $index = array_search($key, $this->steps);
74
            $nextIndex = $index + 1;
75
        }
76
        $value = $request->param("OtherID");
77
        if ($key == "Year") {
78
            $value = intval($value);
79
        }
80
        Session::set("EVCSelectPage_".$key, $value);
81
        $nextKey = $this->steps[$nextIndex];
82
        $method = "get".$nextKey."s";
83
        if ($this->hasMethod($method)) {
84
            return json_encode($this->$method());
85
        }
86
        return json_encode(array("0" => "sorry an error occured with $method, please reload page ..."));
87
    }
88
89
90
    public function getValue($key)
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...
91
    {
92
        $value = Session::get("EVCSelectPage_".$key);
93
        if ($key == "Year") {
94
            if ($value < 1900 || $value > Date("Year", strtotime("Now"))) {
95
                $value = 0;
96
            }
97
        }
98
        return $value;
99
    }
100
101
102
    protected function getMakes()
103
    {
104
        $returnArray = array(0 => $this->pleaseSelectPhrase());
105
        $year = $this->getValue("Year");
106
        if ($year) {
107
            $params = array("view" => "full");
108
            $params["year"] = $year;
109
            $makes = EdmundsAPI::get_data("/api/vehicle/v2/makes/count", $params);
110
            foreach ($makes->makes as $make) {
111
                $returnArray[$make->niceName] = $make->niceName;
112
            }
113
            asort($returnArray);
114
        }
115
        return $returnArray;
116
    }
117
118
119
    protected function getModels()
120
    {
121
        $returnArray = array(0 => $this->pleaseSelectPhrase());
122
        $makeNiceName = $this->getValue("Make");
123
        $year = $this->getValue("Year");
124
        if ($makeNiceName && $year) {
125
            $params = array();
126
            $params["year"] = $year;
127
            $models = EdmundsAPI::get_data("/api/vehicle/v2/".$makeNiceName, $params);
128
            foreach ($models->models as $model) {
129
                $returnArray[$model->niceName] = $model->name;
130
            }
131
            asort($returnArray);
132
        }
133
        return $returnArray;
134
    }
135
136
137
    protected function getTypes()
138
    {
139
        $returnArray = array(0 => $this->pleaseSelectPhrase());
140
        $year = $this->getValue("Year");
141
        $makeNiceName = $this->getValue("Make");
142
        $modelNiceName = $this->getValue("Model");
143
        if ($year && $makeNiceName && $modelNiceName) {
144
            $params = array("view" => "full");
145
            $params["year"] = $year;
146
            $types = EdmundsAPI::get_data("/api/vehicle/v2/".$makeNiceName."/".$modelNiceName, $params);
147
            foreach ($types->years as $loopYear) {
148
                if (intval($loopYear->year) == intval($year)) {
149
                    foreach ($loopYear->styles as $style) {
150
                        $returnArray[$style->id] = $style->name;
151
                    }
152
                }
153
            }
154
            asort($returnArray);
155
        }
156
        return $returnArray;
157
    }
158
159
    public function pleaseSelectPhrase()
160
    {
161
        return "-- please answer all previous questions --";
162
    }
163
}
164