ReservationSession::end()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Session.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 14/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use CalendarEvent;
12
use DataObject;
13
use Session;
14
15
/**
16
 * Class ReservationSession
17
 *
18
 * @package Broarm\EventTickets
19
 */
20
class ReservationSession
21
{
22
    const KEY = 'EventTickets';
23
24
    /**
25
     * Get the session variable
26
     *
27
     * @return Reservation|DataObject
0 ignored issues
show
Documentation introduced by
Should the return type not be DataObject|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...
28
     */
29
    public static function get()
30
    {
31
        return Reservation::get()->byID(
32
            Session::get(self::KEY)
33
        );
34
    }
35
36
    /**
37
     * Set the session variable
38
     *
39
     * @param Reservation $reservation
40
     */
41
    public static function set(Reservation $reservation)
42
    {
43
        Session::set(self::KEY, $reservation->ID);
44
    }
45
46
    /**
47
     * Start the ticket session
48
     *
49
     * @param CalendarEvent $event
50
     *
51
     * @return Reservation
52
     */
53
    public static function start(CalendarEvent $event)
54
    {
55
        $reservation = Reservation::create();
56
        $reservation->EventID = $event->ID;
57
        $reservation->write();
58
        self::set($reservation);
59
        return $reservation;
60
    }
61
62
    /**
63
     * End the Ticket session
64
     */
65
    public static function end()
66
    {
67
        // If the session is ended while in cart or pending state, remove the reservation.
68
        // The session is only ended in these states when iffy business is going on.
69
        if (in_array(self::get()->Status, array('CART', 'PENDING'))) {
70
            self::get()->delete();
71
        }
72
73
        Session::set(self::KEY, null);
74
        Session::clear(self::KEY);
75
    }
76
}