CheckoutStepController::getReservation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * CheckoutStepController.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 16/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use Page_Controller;
12
use SSViewer;
13
14
/**
15
 * Class CheckoutStepController
16
 *
17
 * @package Broarm\EventTickets
18
 */
19
abstract class CheckoutStepController extends Page_Controller
20
{
21
    protected $step = null;
22
23
    /**
24
     * @var Reservation|null
25
     */
26
    protected $reservation = null;
27
28
    /**
29
     * Init the controller and check if the current step is allowed
30
     */
31
    public function init()
32
    {
33
        // If the step is not a registered step exit
34
        if (!in_array($this->step, CheckoutSteps::getSteps())) {
35
            $this->redirect($this->Link('/'));
36
        }
37
38
        // If no ReservationSession exists redirect back to the base event controller
39
        elseif (empty(ReservationSession::get())) {
40
            $this->redirect($this->Link('/'));
41
        }
42
43
        // If the reservation has been processed end the session and redirect
44
        elseif (ReservationSession::get()->Status === 'PAID' && $this->step != 'success') {
45
            ReservationSession::end();
46
            $this->redirect($this->Link('/'));
47
        } else {
48
            $this->reservation = ReservationSession::get();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Broarm\EventTickets\ReservationSession::get() can also be of type object<DataObject>. However, the property $reservation is declared as type object<Broarm\EventTickets\Reservation>|null. 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...
49
            parent::init();
50
        }
51
    }
52
53
    /**
54
     * Force the controller action
55
     *
56
     * @param string $action
57
     *
58
     * @return SSViewer
59
     */
60
    public function getViewer($action)
61
    {
62
        if ($action === 'index') {
63
            $action = $this->step;
64
        }
65
66
        return parent::getViewer($action);
67
    }
68
69
    /**
70
     * Get a relative link to the current controller
71
     *
72
     * @param null $action
73
     *
74
     * @return string
75
     */
76
    public function Link($action = null)
77
    {
78
        if (!$action) {
79
            $action = $this->step;
80
        }
81
82
        return $this->dataRecord->RelativeLink($action);
83
    }
84
85
    /**
86
     * Get the current reservation
87
     *
88
     * @return Reservation
0 ignored issues
show
Documentation introduced by
Should the return type not be Reservation|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...
89
     */
90
    public function getReservation() {
91
        return $this->reservation;
92
    }
93
94
    /**
95
     * Get a link to the next step
96
     *
97
     * @return string
98
     */
99
    public function getNextStepLink()
100
    {
101
        return $this->Link(CheckoutSteps::nextStep($this->step));
102
    }
103
104
    /**
105
     * Get the checkout steps
106
     *
107
     * @return \ArrayList
108
     */
109
    public function CheckoutSteps()
110
    {
111
        return CheckoutSteps::get($this);
112
    }
113
}
114