Completed
Push — master ( 00e32a...cf71fc )
by Bram
13:26
created

TicketExtension   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 223
Duplicated Lines 16.59 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 12
dl 37
loc 223
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B updateCMSFields() 21 55 5
A onAfterWrite() 0 5 1
B createDefaultFields() 0 23 5
A updateCMSActions() 0 16 2
A getAvailability() 0 4 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 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 int                            OrderMin
30
 * @property int                            OrderMax
31
 * @property string                         SuccessMessage
32
 * @property string                         SuccessMessageMail
33
 *
34
 * @method \HasManyList Tickets()
35
 * @method \HasManyList Reservations()
36
 * @method \HasManyList Attendees()
37
 * @method \HasManyList WaitingList()
38
 * @method \HasManyList Fields()
39
 */
40
class TicketExtension extends DataExtension
41
{
42
    /**
43
     * @var CalendarEvent_Controller
44
     */
45
    protected $controller;
46
47
    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...
48
        'Capacity' => 'Int',
49
        'OrderMin' => 'Int',
50
        'OrderMax' => 'Int',
51
        'SuccessMessage' => 'HTMLText',
52
        'SuccessMessageMail' => 'HTMLText'
53
    );
54
55
    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...
56
        'Tickets' => 'Broarm\EventTickets\Ticket.Event',
57
        'Reservations' => 'Broarm\EventTickets\Reservation.Event',
58
        'Attendees' => 'Broarm\EventTickets\Attendee.Event',
59
        'WaitingList' => 'Broarm\EventTickets\WaitingListRegistration.Event',
60
        'Fields' => 'Broarm\EventTickets\AttendeeExtraField.Event'
61
    );
62
63
    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...
64
        'Capacity' => 50,
65
        'OrderMin' => 1,
66
        'OrderMax' => 5
67
    );
68
69
    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...
70
        'SuccessMessage',
71
        'SuccessMessageMail'
72
    );
73
74
    public function updateCMSFields(FieldList $fields)
75
    {
76
        $gridFieldConfig = GridFieldConfig_RecordEditor::create();
77
78
        // If the event dates are in the past remove ability to create new tickets, reservations and attendees.
79
        // This is done here instead of in the canCreate method because of the lack of context there.
80
        if (!$this->canCreateTickets()) {
81
            $gridFieldConfig->removeComponentsByType(new GridFieldAddNewButton());
82
        }
83
84
        $ticketLabel = _t('TicketExtension.Tickets', 'Tickets');
85
        $fields->addFieldsToTab(
86
            "Root.$ticketLabel", array(
87
            GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), $gridFieldConfig),
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...
88
            NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')),
89
            NumericField::create('OrderMin', _t('TicketExtension.OrderMin', 'Minimum amount of tickets required per reservation')),
90
            NumericField::create('OrderMax', _t('TicketExtension.OrderMax', 'Maximum amount of tickets allowed per reservation')),
91
            HtmlEditorField::create('SuccessMessage',
92
                _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4),
93
            HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4)
94
        ));
95
96 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...
97
            $reservationLabel = _t('TicketExtension.Reservations', 'Reservations');
98
            $fields->addFieldToTab(
99
                "Root.$reservationLabel",
100
                GridField::create('Reservations', $reservationLabel, $this->owner->Reservations(), $gridFieldConfig)
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...
101
            );
102
        }
103
104 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...
105
            $guestListLabel = _t('TicketExtension.GuestList', 'GuestList');
106
            $fields->addFieldToTab(
107
                "Root.$guestListLabel",
108
                GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), $gridFieldConfig)
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...
109
            );
110
        }
111
112 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...
113
            $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList');
114
            $fields->addFieldToTab(
115
                "Root.$waitingListLabel",
116
                GridField::create('WaitingList', $waitingListLabel, $this->owner->WaitingList(), $gridFieldConfig)
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...
117
            );
118
        }
119
120
121
        $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields');
122
        $fields->addFieldToTab(
123
            "Root.$extraFieldsLabel",
124
            GridField::create('WaitingList', $extraFieldsLabel, $this->owner->Fields(),
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...
125
                GridFieldConfig_Fields::create())
126
        );
127
128
    }
129
130
    /**
131
     * Trigger actions after write
132
     */
133
    public function onAfterWrite()
134
    {
135
        $this->createDefaultFields();
136
        parent::onAfterWrite();
137
    }
138
139
    /**
140
     * Creates and sets up the default fields
141
     */
142
    public function createDefaultFields()
143
    {
144
        $fields = Attendee::config()->get('default_fields');
145
        if (!$this->owner->Fields()->exists()) {
146
            foreach ($fields as $fieldName => $fieldType) {
147
                $field = AttendeeExtraField::create();
148
                $field->Title = _t("AttendeeField.$fieldName", $fieldName);
149
                $field->FieldName = $fieldName;
150
                $field->Required = true;
151
                $field->Editable = false;
152
153
                if (is_array($fieldType)) {
154
                    foreach ($fieldType as $property => $value) {
155
                        $field->setField($property, $value);
156
                    }
157
                } else {
158
                    $field->FieldType = $fieldType;
159
                }
160
161
                $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...
162
            }
163
        }
164
    }
165
166
    /**
167
     * Extend the page actions with an start check in action
168
     *
169
     * @param FieldList $actions
170
     */
171
    public function updateCMSActions(FieldList $actions)
172
    {
173
        $checkInButton = new LiteralField('StartCheckIn',
174
            "<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
175
                id='Edit_StartCheckIn'
176
                role='button'
177
                href='{$this->owner->Link('checkin')}'
178
                target='_blank'>
179
                Start check in
180
            </a>"
181
        );
182
183
        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...
184
            $actions->push($checkInButton);
185
        }
186
    }
187
188
    /**
189
     * Get the leftover capacity
190
     *
191
     * @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...
192
     */
193
    public function getAvailability()
194
    {
195
        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...
196
    }
197
198
    /**
199
     * Get the success message
200
     *
201
     * @return mixed|string
202
     */
203 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...
204
    {
205
        if (!empty($this->owner->SuccessMessage)) {
206
            return $this->owner->dbObject('SuccessMessage');
207
        } else {
208
            return SiteConfig::current_site_config()->dbObject('SuccessMessage');
209
        }
210
    }
211
212
    /**
213
     * Get the mail message
214
     *
215
     * @return mixed|string
216
     */
217 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...
218
    {
219
        if (!empty($this->owner->SuccessMessageMail)) {
220
            return $this->owner->dbObject('SuccessMessageMail');
221
        } else {
222
            return SiteConfig::current_site_config()->dbObject('SuccessMessageMail');
223
        }
224
    }
225
226
    /**
227
     * Get the Ticket logo
228
     *
229
     * @return \Image
230
     */
231
    public function getMailLogo()
232
    {
233
        return SiteConfig::current_site_config()->TicketLogo();
234
    }
235
236
    /**
237
     * Check if the current event can have tickets
238
     *
239
     * @return bool
240
     */
241
    public function canCreateTickets()
242
    {
243
        $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...
244
        if ($currentDate && $currentDate->exists()) {
245
            return $currentDate->dbObject('StartDate')->InFuture();
246
        }
247
248
        return false;
249
    }
250
251
    /**
252
     * Get the calendar controller
253
     *
254
     * @return CalendarEvent_Controller
255
     */
256
    public function getController()
257
    {
258
        return $this->controller
259
            ? $this->controller
260
            : $this->controller = CalendarEvent_Controller::create($this->owner);
261
    }
262
}
263