Completed
Push — master ( 433ab3...f1150e )
by Bram
02:22
created

TicketExtension   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 233
Duplicated Lines 15.88 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 13
dl 37
loc 233
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B updateCMSFields() 21 52 5
A onAfterWrite() 0 5 1
B createDefaultFields() 0 23 5
A updateCMSActions() 0 16 2
A getAvailability() 0 4 1
A getGuestList() 0 6 1
A getSuccessContent() 8 8 2
A getMailContent() 8 8 2
A getMailLogo() 0 4 1
A canCreateTickets() 0 9 3
A getController() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\AttendeeExtraField.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
        $ticketsGridFieldConfig = GridFieldConfig_RecordEditor::create();
81
        // If the event dates are in the past remove ability to create new tickets, reservations and attendees.
82
        // This is done here instead of in the canCreate method because of the lack of context there.
83
        if (!$this->canCreateTickets()) {
84
            $ticketsGridFieldConfig->removeComponentsByType(new GridFieldAddNewButton());
85
        }
86
87
        $ticketLabel = _t('TicketExtension.Tickets', 'Tickets');
88
        $fields->addFieldsToTab(
89
            "Root.$ticketLabel", array(
90
            GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), $ticketsGridFieldConfig),
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...
91
            NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')),
92
            NumericField::create('OrderMin', _t('TicketExtension.OrderMin', 'Minimum amount of tickets required per reservation')),
93
            NumericField::create('OrderMax', _t('TicketExtension.OrderMax', 'Maximum amount of tickets allowed per reservation')),
94
            HtmlEditorField::create('SuccessMessage', _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4),
95
            HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4)
96
        ));
97
98 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...
99
            $reservationLabel = _t('TicketExtension.Reservations', 'Reservations');
100
            $fields->addFieldToTab(
101
                "Root.$reservationLabel",
102
                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...
103
            );
104
        }
105
106 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...
107
            $guestListLabel = _t('TicketExtension.GuestList', 'GuestList');
108
            $fields->addFieldToTab(
109
                "Root.$guestListLabel",
110
                GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), GridFieldConfig_RecordEditor::create())
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...
111
            );
112
        }
113
114 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...
115
            $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList');
116
            $fields->addFieldToTab(
117
                "Root.$waitingListLabel",
118
                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...
119
            );
120
        }
121
122
        $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields');
123
        $fields->addFieldToTab(
124
            "Root.$extraFieldsLabel",
125
            GridField::create('WaitingList', $extraFieldsLabel, $this->owner->Fields(), GridFieldConfig_Fields::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...
126
        );
127
128
        $this->owner->extend('updateTicketExtensionFields', $fields);
129
    }
130
131
    /**
132
     * Trigger actions after write
133
     */
134
    public function onAfterWrite()
135
    {
136
        $this->createDefaultFields();
137
        parent::onAfterWrite();
138
    }
139
140
    /**
141
     * Creates and sets up the default fields
142
     */
143
    public function createDefaultFields()
144
    {
145
        $fields = Attendee::config()->get('default_fields');
146
        if (!$this->owner->Fields()->exists()) {
147
            foreach ($fields as $fieldName => $fieldType) {
148
                $field = AttendeeExtraField::create();
149
                $field->Title = _t("AttendeeField.$fieldName", $fieldName);
150
                $field->FieldName = $fieldName;
151
                $field->Required = true;
152
                $field->Editable = false;
153
154
                if (is_array($fieldType)) {
155
                    foreach ($fieldType as $property => $value) {
156
                        $field->setField($property, $value);
157
                    }
158
                } else {
159
                    $field->FieldType = $fieldType;
160
                }
161
162
                $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...
163
            }
164
        }
165
    }
166
167
    /**
168
     * Extend the page actions with an start check in action
169
     *
170
     * @param FieldList $actions
171
     */
172
    public function updateCMSActions(FieldList $actions)
173
    {
174
        $checkInButton = new LiteralField('StartCheckIn',
175
            "<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
176
                id='Edit_StartCheckIn'
177
                role='button'
178
                href='{$this->owner->Link('checkin')}'
179
                target='_blank'>
180
                Start check in
181
            </a>"
182
        );
183
184
        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...
185
            $actions->push($checkInButton);
186
        }
187
    }
188
189
    /**
190
     * Get the leftover capacity
191
     *
192
     * @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...
193
     */
194
    public function getAvailability()
195
    {
196
        return $this->owner->Capacity - $this->owner->Attendees()->count();
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...
197
    }
198
199
    /**
200
     * Get only the attendees who are certain to attend
201
     * Fixme: Using callback filter instead of join, because join gives ambiguous error on 'EventID'
202
     *
203
     * @return \ArrayList
204
     */
205
    public function getGuestList()
206
    {
207
        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...
208
            return $attendee->Reservation()->Status === 'PAID';
209
        });
210
    }
211
212
    /**
213
     * Get the success message
214
     *
215
     * @return mixed|string
216
     */
217 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...
218
    {
219
        if (!empty($this->owner->SuccessMessage)) {
220
            return $this->owner->dbObject('SuccessMessage');
221
        } else {
222
            return SiteConfig::current_site_config()->dbObject('SuccessMessage');
223
        }
224
    }
225
226
    /**
227
     * Get the mail message
228
     *
229
     * @return mixed|string
230
     */
231 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...
232
    {
233
        if (!empty($this->owner->SuccessMessageMail)) {
234
            return $this->owner->dbObject('SuccessMessageMail');
235
        } else {
236
            return SiteConfig::current_site_config()->dbObject('SuccessMessageMail');
237
        }
238
    }
239
240
    /**
241
     * Get the Ticket logo
242
     *
243
     * @return \Image
244
     */
245
    public function getMailLogo()
246
    {
247
        return SiteConfig::current_site_config()->TicketLogo();
248
    }
249
250
    /**
251
     * Check if the current event can have tickets
252
     *
253
     * @return bool
254
     */
255
    public function canCreateTickets()
256
    {
257
        $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...
258
        if ($currentDate && $currentDate->exists()) {
259
            return $currentDate->dbObject('StartDate')->InFuture();
260
        }
261
262
        return false;
263
    }
264
265
    /**
266
     * Get the calendar controller
267
     *
268
     * @return CalendarEvent_Controller
269
     */
270
    public function getController()
271
    {
272
        return $this->controller
273
            ? $this->controller
274
            : $this->controller = CalendarEvent_Controller::create($this->owner);
275
    }
276
}
277