OrderStepController::name_of_controller_class()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
3
/**
4
 * This call can be used when you need input from the customer
5
 * in the order process.
6
 *
7
 * To use
8
 *
9
 * 1. create class that extends OrderStepController
10
 * 2. make sure the class has a $url_segment static var
11
 * 3. create content and/or form for page
12
 * 4. make sure you set up route (route.yml) to get to the
13
 */
14
abstract class OrderStepController extends Controller
15
{
16
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
17
        'error' => true,
18
    );
19
20
    /**
21
     * @var string
22
     */
23
    protected $alternativeContent = "";
24
25
    /**
26
     * when no action is selected
27
     * this action runs...
28
     */
29
    public function index($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        $this->alternativeContent = '<p class="message bad">Sorry, we can not find the page you are looking for.</p>';
32
33
        return $this->renderWith('Page');
34
    }
35
36
    /**
37
     * there is an error ...
38
     */
39
    public function error($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        $this->alternativeContent = '<p class="message bad">Sorry, an error occurred, please contact us for more information....</p>';
42
43
        return $this->renderWith('Page');
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    protected static function name_of_controller_class()
50
    {
51
        return get_called_class();
52
    }
53
54
    /**
55
     * @param Order $order
56
     *
57
     * @return string
58
     */
59
    protected static function secure_hash($order)
60
    {
61
        $obj = Injector::inst()->get(self::name_of_controller_class());
62
63
        return $obj->secureHash($order);
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    protected function nameOfControllerClass()
70
    {
71
        return self::name_of_controller_class();
72
    }
73
74
    /**
75
     * related OrderStatusLog class.
76
     *
77
     * @return string
78
     */
79
    abstract protected function nameOfLogClass();
80
81
    /**
82
     * main content ...
83
     *
84
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
     */
86
    public function Content($order = null)
87
    {
88
        if ($this->alternativeContent) {
89
            return $this->alternativeContent;
90
        }
91
        return $this->standardContent($order);
92
    }
93
94
    /**
95
     * @return string ($html)
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    protected function standardContent($order = null)
0 ignored issues
show
Unused Code introduced by
The parameter $order is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        user_error("Make sure to put some content here in classes that extend ".$this->class);
100
    }
101
102
    /**
103
     * the form on the field.
104
     *
105
     * @return Form
106
     */
107
    protected function Form()
108
    {
109
        return $this->Form;
0 ignored issues
show
Documentation introduced by
The property Form does not exist on object<OrderStepController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
    }
111
112
    /**
113
     * code of related order step.
114
     *
115
     * @return string
116
     */
117
    abstract protected function codeOfRelevantOrderStep();
118
119
    /**
120
     * used to secure page.
121
     *
122
     * @param Order $order
123
     *
124
     * @return string
125
     */
126
    abstract protected function secureHash($order);
127
128
    /**
129
     * @oaram string $action
130
     *
131
     * @return string
132
     */
133
    public function Link($action = null)
134
    {
135
        $link = '/'.Config::inst()->get($this->nameOfControllerClass(), 'url_segment').'/';
136
        if ($action) {
137
            $link = $link.$action.'/';
138
        }
139
140
        return $link.$this->getOrderGetParams();
141
    }
142
143
    public function errorLink()
144
    {
145
        return $this->Link('error');
146
    }
147
148
    /**
149
     * is the order valid?
150
     *
151
     * @return bool
152
     */
153
    protected function checkOrder($dataOrRequest = null)
154
    {
155
        $order = $this->Order($dataOrRequest);
156
        if ($order && $order->exists()) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $order && $order->exists();.
Loading history...
157
            return true;
158
        } else {
159
            return false;
160
        }
161
    }
162
163
    /**
164
     * @var Order
165
     */
166
    private static $_order = null;
167
168
    /**
169
     * finds the order ...
170
     *
171
     * @param mixed
172
     *
173
     * @return Order
0 ignored issues
show
Documentation introduced by
Should the return type not be Order|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
174
     */
175
    protected function Order($dataOrRequest = null)
176
    {
177
        if (!self::$_order) {
178
            if (
179
                is_array($dataOrRequest) &&
180
                isset($dataOrRequest['OrderID']) &&
181
                isset($dataOrRequest['OrderSessionID'])
182
            ) {
183
                $id = intval($dataOrRequest['OrderID']);
184
                $sessionID = Convert::raw2sql($dataOrRequest['OrderSessionID']);
185
            } elseif (isset($_POST['OrderID']) && isset($_POST['OrderSessionID'])) {
186
                $id = intval($_POST['OrderID']);
187
                $sessionID = Convert::raw2sql($_POST['OrderSessionID']);
188
            } elseif (isset($_GET['OrderID']) && isset($_GET['OrderSessionID'])) {
189
                $id = intval($_GET['OrderID']);
190
                $sessionID = Convert::raw2sql($_GET['OrderSessionID']);
191
            } elseif ($dataOrRequest instanceof SS_HTTPRequest) {
192
                $id = intval($dataOrRequest->param('ID'));
193
                $sessionID = Convert::raw2sql($dataOrRequest->param('OtherID'));
194
            } else {
195
                $id = intval($this->request->param('ID'));
196
                $sessionID = Convert::raw2sql($this->request->param('OtherID'));
197
            }
198
            self::$_order = Order::get()->byID($id);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Order::get()->byID($id) can also be of type object<DataObject>. However, the property $_order is declared as type object<Order>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
199
            if (self::$_order) {
200
                if ($this->secureHash(self::$_order) != $sessionID) {
0 ignored issues
show
Compatibility introduced by
self::$_order of type object<DataObject> is not a sub-type of object<Order>. It seems like you assume a child class of the class DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
201
                    self::$_order = null;
202
                }
203
            }
204
        }
205
206
        return self::$_order;
207
    }
208
209
    /**
210
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
211
     */
212
    protected function getOrderGetParams()
213
    {
214
        if ($order = $this->Order()) {
215
            return '?OrderID='.$order->ID.'&OrderSessionID='.self::secure_hash($order);
216
        }
217
    }
218
219
    /**
220
     * @return OrderStep
221
     */
222
    protected function orderStep()
223
    {
224
        return DataObject::get_one(
225
            'OrderStep',
226
            array('Code' => $this->codeOfRelevantOrderStep())
227
        );
228
    }
229
}
230