Completed
Push — master ( dead21...224450 )
by Bram
02:17
created

TicketExtension::canCreateTickets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/**
3
 * TicketExtension.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 09/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use CalendarEvent_Controller;
12
use DataExtension;
13
use FieldList;
14
use GridField;
15
use GridFieldAddNewButton;
16
use GridFieldConfig_RecordEditor;
17
use HtmlEditorField;
18
use LiteralField;
19
use NumericField;
20
use SiteConfig;
21
22
/**
23
 * Class TicketExtension
24
 *
25
 * @package Broarm\EventTickets
26
 *
27
 * @property TicketExtension|\CalendarEvent $owner
28
 * @property int                            Capacity
29
 * @property string                         SuccessMessage
30
 * @property string                         SuccessMessageMail
31
 *
32
 * @method \HasManyList Tickets
33
 * @method \HasManyList Reservations
34
 * @method \HasManyList Attendees
35
 */
36
class TicketExtension extends DataExtension
37
{
38
    /**
39
     * @var CalendarEvent_Controller
40
     */
41
    protected $controller;
42
43
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db 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...
44
        'Capacity' => 'Int',
45
        'SuccessMessage' => 'HTMLText',
46
        'SuccessMessageMail' => 'HTMLText'
47
    );
48
49
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many 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...
50
        'Tickets' => 'Broarm\EventTickets\Ticket.Event',
51
        'Reservations' => 'Broarm\EventTickets\Reservation.Event',
52
        'Attendees' => 'Broarm\EventTickets\Attendee.Event'
53
    );
54
55
    public function updateCMSFields(FieldList $fields)
56
    {
57
        $gridFieldConfig = GridFieldConfig_RecordEditor::create();
58
59
        // If the event dates are in the past remove ability to create new tickets, reservations and attendees.
60
        // This is done here instead of in the canCreate method because of the lack of context there.
61
        if (!$this->canCreateTickets()) {
62
            $gridFieldConfig->removeComponentsByType(new GridFieldAddNewButton());
63
        }
64
65
        $ticketLabel = _t('TicketExtension.Tickets', 'Tickets');
66
        $fields->addFieldsToTab(
67
            "Root.$ticketLabel", array(
68
            GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), $gridFieldConfig),
69
            NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')),
70
            HtmlEditorField::create('SuccessMessage', 'Success message')->setRows(4),
71
            HtmlEditorField::create('SuccessMessageMail', 'Mail message')->setRows(4)
72
        ));
73
74 View Code Duplication
        if ($this->owner->Reservations()->exists()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            $reservationLabel = _t('TicketExtension.Reservations', 'Reservations');
76
            $fields->addFieldToTab(
77
                "Root.$reservationLabel",
78
                GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), $gridFieldConfig)
79
            );
80
        }
81
82 View Code Duplication
        if ($this->owner->Attendees()->exists()) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            $guestListLabel = _t('TicketExtension.GuestList', 'GuestList');
84
            $fields->addFieldToTab(
85
                "Root.$guestListLabel",
86
                GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), $gridFieldConfig)
87
            );
88
        }
89
90
        return $fields;
91
    }
92
93
    /**
94
     * Extend the page actions with an start check in action
95
     *
96
     * @param FieldList $actions
97
     */
98
    public function updateCMSActions(FieldList $actions)
99
    {
100
        $checkInButton = new LiteralField('StartCheckIn',
101
            "<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
102
                id='Edit_StartCheckIn'
103
                role='button'
104
                href='{$this->owner->Link('checkin')}'
105
                target='_blank'>
106
                Start check in
107
            </a>"
108
        );
109
110
        if ($this->owner->Attendees()->exists()) {
111
            $actions->push($checkInButton);
112
        }
113
    }
114
115
    /**
116
     * Get the leftover capacity
117
     *
118
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

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...
119
     */
120
    public function getAvailability()
121
    {
122
        return $this->owner->Capacity - $this->owner->Attendees()->count();
123
    }
124
125
    /**
126
     * Get the success message
127
     *
128
     * @return mixed|string
129
     */
130 View Code Duplication
    public function getSuccessContent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        if (!empty($this->owner->SuccessMessage)) {
133
            return $this->owner->dbObject('SuccessMessage');
134
        } else {
135
            return SiteConfig::current_site_config()->dbObject('SuccessMessage');
136
        }
137
    }
138
139
    /**
140
     * Get the mail message
141
     *
142
     * @return mixed|string
143
     */
144 View Code Duplication
    public function getMailContent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        if (!empty($this->owner->SuccessMessageMail)) {
147
            return $this->owner->dbObject('SuccessMessageMail');
148
        } else {
149
            return SiteConfig::current_site_config()->dbObject('SuccessMessageMail');
150
        }
151
    }
152
153
    /**
154
     * Check if the current event can have tickets
155
     *
156
     * @return bool
157
     */
158
    public function canCreateTickets()
159
    {
160
        $currentDate = $this->owner->getController()->CurrentDate();
1 ignored issue
show
Bug introduced by
The method getController does only exist in Broarm\EventTickets\TicketExtension, but not in CalendarEvent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
161
        if ($currentDate->exists()) {
162
            return $currentDate->dbObject('StartDate')->InFuture();
163
        }
164
165
        return false;
166
    }
167
168
    /**
169
     * Get the calendar controller
170
     *
171
     * @return CalendarEvent_Controller
172
     */
173
    public function getController()
174
    {
175
        return $this->controller
176
            ? $this->controller
177
            : $this->controller = CalendarEvent_Controller::create($this->owner);
178
    }
179
}
180