Completed
Push — master ( 879542...3be800 )
by Bram
02:59
created

TicketExtension::getEventExpired()   A

Complexity

Conditions 3
Paths 3

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 3
eloc 5
nc 3
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 GridFieldDataColumns;
18
use GridFieldExportButton;
19
use GridFieldSortableHeader;
20
use HasManyList;
21
use HtmlEditorField;
22
use LiteralField;
23
use NumericField;
24
use SiteConfig;
25
26
/**
27
 * Class TicketExtension
28
 *
29
 * @package Broarm\EventTickets
30
 *
31
 * @property TicketExtension|\CalendarEvent $owner
32
 * @property int                            Capacity
33
 * @property int                            OrderMin
34
 * @property int                            OrderMax
35
 * @property string                         SuccessMessage
36
 * @property string                         SuccessMessageMail
37
 *
38
 * @method \HasManyList Tickets()
39
 * @method \HasManyList Reservations()
40
 * @method \HasManyList Attendees()
41
 * @method \HasManyList WaitingList()
42
 * @method \HasManyList Fields()
43
 */
44
class TicketExtension extends DataExtension
45
{
46
    /**
47
     * @var CalendarEvent_Controller
48
     */
49
    protected $controller;
50
51
    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...
52
        'Capacity' => 'Int',
53
        'OrderMin' => 'Int',
54
        'OrderMax' => 'Int',
55
        'SuccessMessage' => 'HTMLText',
56
        'SuccessMessageMail' => 'HTMLText'
57
    );
58
59
    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...
60
        'Tickets' => 'Broarm\EventTickets\Ticket.Event',
61
        'Reservations' => 'Broarm\EventTickets\Reservation.Event',
62
        'Attendees' => 'Broarm\EventTickets\Attendee.Event',
63
        'WaitingList' => 'Broarm\EventTickets\WaitingListRegistration.Event',
64
        'Fields' => 'Broarm\EventTickets\UserField.Event'
65
    );
66
67
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults 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...
68
        'Capacity' => 50,
69
        'OrderMin' => 1,
70
        'OrderMax' => 5
71
    );
72
73
    private static $translate = array(
0 ignored issues
show
Unused Code introduced by
The property $translate 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...
74
        'SuccessMessage',
75
        'SuccessMessageMail'
76
    );
77
78
    public function updateCMSFields(FieldList $fields)
79
    {
80
        $ticketLabel = _t('TicketExtension.Tickets', 'Tickets');
81
        $fields->addFieldsToTab(
82
            "Root.$ticketLabel", array(
83
            GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), TicketsGridFieldConfig::create($this->canCreateTickets())),
1 ignored issue
show
Bug introduced by
The method Tickets 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...
84
            NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')),
85
            NumericField::create('OrderMin', _t('TicketExtension.OrderMin', 'Minimum amount of tickets required per reservation')),
86
            NumericField::create('OrderMax', _t('TicketExtension.OrderMax', 'Maximum amount of tickets allowed per reservation')),
87
            HtmlEditorField::create('SuccessMessage', _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4),
88
            HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4)
89
        ));
90
91
        // Create Reservations tab
92
        if ($this->owner->Reservations()->exists()) {
93
            $reservationLabel = _t('TicketExtension.Reservations', 'Reservations');
94
            $fields->addFieldToTab(
95
                "Root.$reservationLabel",
96
                GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), GridFieldConfig_RecordEditor::create())
1 ignored issue
show
Bug introduced by
The method Reservations 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...
97
            );
98
        }
99
100
        // Create Attendees tab
101 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...
102
            $guestListLabel = _t('TicketExtension.GuestList', 'GuestList');
103
            $fields->addFieldToTab(
104
                "Root.$guestListLabel",
105
                GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), GuestListGridFieldConfig::create($this->owner))
1 ignored issue
show
Bug introduced by
The method Attendees 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...
106
            );
107
        }
108
109
        // Create WaitingList tab
110 View Code Duplication
        if ($this->owner->WaitingList()->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...
111
            $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList');
112
            $fields->addFieldToTab(
113
                "Root.$waitingListLabel",
114
                GridField::create('WaitingList', $waitingListLabel, $this->owner->WaitingList(), GridFieldConfig_RecordEditor::create())
1 ignored issue
show
Bug introduced by
The method WaitingList 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...
115
            );
116
        }
117
118
        // Create Fields tab
119
        $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields');
120
        $fields->addFieldToTab(
121
            "Root.$extraFieldsLabel",
122
            GridField::create('ExtraFields', $extraFieldsLabel, $this->owner->Fields(), UserFieldsGridFieldConfig::create())
1 ignored issue
show
Bug introduced by
The method Fields 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...
123
        );
124
125
        $this->owner->extend('updateTicketExtensionFields', $fields);
126
    }
127
128
    /**
129
     * Trigger actions after write
130
     */
131
    public function onAfterWrite()
132
    {
133
        $this->createDefaultFields();
134
        parent::onAfterWrite();
135
    }
136
137
    /**
138
     * Creates and sets up the default fields
139
     */
140
    public function createDefaultFields()
141
    {
142
        $fields = Attendee::config()->get('default_fields');
143
        if (!$this->owner->Fields()->exists()) {
144
            foreach ($fields as $fieldName => $config) {
145
                $field = UserField::createDefaultField($fieldName, $config);
146
                $this->owner->Fields()->add($field);
1 ignored issue
show
Bug introduced by
The method Fields 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...
147
            }
148
        }
149
    }
150
151
    /**
152
     * Extend the page actions with an start check in action
153
     *
154
     * @param FieldList $actions
155
     */
156
    public function updateCMSActions(FieldList $actions)
157
    {
158
        $checkInButton = new LiteralField('StartCheckIn',
159
            "<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
160
                id='Edit_StartCheckIn'
161
                role='button'
162
                href='{$this->owner->Link('checkin')}'
163
                target='_blank'>
164
                Start check in
165
            </a>"
166
        );
167
168
        if ($this->owner->Attendees()->exists()) {
1 ignored issue
show
Bug introduced by
The method Attendees 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...
169
            $actions->push($checkInButton);
170
        }
171
    }
172
173
    /**
174
     * Get the leftover capacity
175
     *
176
     * @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...
177
     */
178
    public function getAvailability()
179
    {
180
        return $this->owner->Capacity - $this->owner->getGuestList()->count();
1 ignored issue
show
Bug introduced by
The method getGuestList 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...
181
    }
182
183
    /**
184
     * Check if the event is expired, either by unavailable tickets or because the date has passed
185
     *
186
     * @return bool
187
     */
188
    public function getEventExpired()
189
    {
190
        $expired = true;
191
        /** @var Ticket $ticket */
192
        foreach ($this->owner->Tickets() as $ticket) {
1 ignored issue
show
Bug introduced by
The method Tickets 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...
193
            $expired = (!$ticket->validateDate() && $expired);
194
        }
195
        return $expired;
196
    }
197
198
    /**
199
     * Get only the attendees who are certain to attend
200
     * Fixme: Using callback filter instead of join, because join gives ambiguous error on 'EventID'
201
     *
202
     * @return \ArrayList
203
     */
204
    public function getGuestList()
205
    {
206
        return $this->owner->Attendees()->filterByCallback(function(Attendee $attendee, HasManyList $list) {
1 ignored issue
show
Bug introduced by
The method Attendees 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...
Unused Code introduced by
The parameter $list 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...
207
            return $attendee->Reservation()->Status === 'PAID';
208
        });
209
    }
210
211
    /**
212
     * Get the success message
213
     *
214
     * @return mixed|string
215
     */
216 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...
217
    {
218
        if (!empty($this->owner->SuccessMessage)) {
219
            return $this->owner->dbObject('SuccessMessage');
220
        } else {
221
            return SiteConfig::current_site_config()->dbObject('SuccessMessage');
222
        }
223
    }
224
225
    /**
226
     * Get the mail message
227
     *
228
     * @return mixed|string
229
     */
230 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...
231
    {
232
        if (!empty($this->owner->SuccessMessageMail)) {
233
            return $this->owner->dbObject('SuccessMessageMail');
234
        } else {
235
            return SiteConfig::current_site_config()->dbObject('SuccessMessageMail');
236
        }
237
    }
238
239
    /**
240
     * Get the Ticket logo
241
     *
242
     * @return \Image
243
     */
244
    public function getMailLogo()
245
    {
246
        return SiteConfig::current_site_config()->TicketLogo();
247
    }
248
249
    /**
250
     * Check if the current event can have tickets
251
     *
252
     * @return bool
253
     */
254
    public function canCreateTickets()
255
    {
256
        $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...
257
        if ($currentDate && $currentDate->exists()) {
258
            return $currentDate->dbObject('StartDate')->InFuture();
259
        }
260
261
        return false;
262
    }
263
264
    /**
265
     * Get the calendar controller
266
     *
267
     * @return CalendarEvent_Controller
268
     */
269
    public function getController()
270
    {
271
        return $this->controller
272
            ? $this->controller
273
            : $this->controller = CalendarEvent_Controller::create($this->owner);
274
    }
275
}
276