Completed
Push — master ( b95ba0...2412b9 )
by Bram
02:35
created

TicketExtension::createDefaultFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
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\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
        $ticketLabel = _t('TicketExtension.Tickets', 'Tickets');
81
        $fields->addFieldsToTab(
82
            "Root.$ticketLabel", array(
83
            GridField::create('Tickets', $ticketLabel, $this->owner->Tickets(), $ticketsGridFieldConfig = GridFieldConfig_RecordEditor::create()),
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
        // If the event dates are in the past remove ability to create new tickets, reservations and attendees.
92
        // This is done here instead of in the canCreate method because of the lack of context there.
93
        if (!$this->canCreateTickets()) {
94
            $ticketsGridFieldConfig->removeComponentsByType(new GridFieldAddNewButton());
95
        }
96
97
        // Create Reservations tab
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
        // Create Attendees tab
107
        if ($this->owner->Attendees()->exists()) {
108
            $guestListLabel = _t('TicketExtension.GuestList', 'GuestList');
109
            $fields->addFieldToTab(
110
                "Root.$guestListLabel",
111
                GridField::create('Attendees', $guestListLabel, $this->owner->Attendees(), $attendeesGridFieldConfig = 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...
112
            );
113
            $attendeesGridFieldConfig->addComponent(new GuestListExportButton($this->owner, 'Footer'));
0 ignored issues
show
Bug introduced by
It seems like $this->owner can also be of type object<Broarm\EventTickets\TicketExtension>; however, Broarm\EventTickets\Gues...rtButton::__construct() does only seem to accept object<CalendarEvent>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
        }
115
116
        // Create WaitingList tab
117 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...
118
            $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList');
119
            $fields->addFieldToTab(
120
                "Root.$waitingListLabel",
121
                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...
122
            );
123
        }
124
125
        // Create Fields tab
126
        $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields');
127
        $fields->addFieldToTab(
128
            "Root.$extraFieldsLabel",
129
            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...
130
        );
131
132
        $this->owner->extend('updateTicketExtensionFields', $fields);
133
    }
134
135
    /**
136
     * Trigger actions after write
137
     */
138
    public function onAfterWrite()
139
    {
140
        $this->createDefaultFields();
141
        parent::onAfterWrite();
142
    }
143
144
    /**
145
     * Creates and sets up the default fields
146
     */
147
    public function createDefaultFields()
148
    {
149
        $fields = Attendee::config()->get('default_fields');
150
        if (!$this->owner->Fields()->exists()) {
151
            foreach ($fields as $fieldName => $fieldConfig) {
152
                $field = AttendeeExtraField::createFromConfig($fieldName, $fieldConfig);
153
                $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...
154
            }
155
        }
156
    }
157
158
    /**
159
     * Extend the page actions with an start check in action
160
     *
161
     * @param FieldList $actions
162
     */
163
    public function updateCMSActions(FieldList $actions)
164
    {
165
        $checkInButton = new LiteralField('StartCheckIn',
166
            "<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
167
                id='Edit_StartCheckIn'
168
                role='button'
169
                href='{$this->owner->Link('checkin')}'
170
                target='_blank'>
171
                Start check in
172
            </a>"
173
        );
174
175
        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...
176
            $actions->push($checkInButton);
177
        }
178
    }
179
180
    /**
181
     * Get the leftover capacity
182
     *
183
     * @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...
184
     */
185
    public function getAvailability()
186
    {
187
        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...
188
    }
189
190
    /**
191
     * Get only the attendees who are certain to attend
192
     * Fixme: Using callback filter instead of join, because join gives ambiguous error on 'EventID'
193
     *
194
     * @return \ArrayList
195
     */
196
    public function getGuestList()
197
    {
198
        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...
199
            return $attendee->Reservation()->Status === 'PAID';
200
        });
201
    }
202
203
    /**
204
     * Get the success message
205
     *
206
     * @return mixed|string
207
     */
208 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...
209
    {
210
        if (!empty($this->owner->SuccessMessage)) {
211
            return $this->owner->dbObject('SuccessMessage');
212
        } else {
213
            return SiteConfig::current_site_config()->dbObject('SuccessMessage');
214
        }
215
    }
216
217
    /**
218
     * Get the mail message
219
     *
220
     * @return mixed|string
221
     */
222 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...
223
    {
224
        if (!empty($this->owner->SuccessMessageMail)) {
225
            return $this->owner->dbObject('SuccessMessageMail');
226
        } else {
227
            return SiteConfig::current_site_config()->dbObject('SuccessMessageMail');
228
        }
229
    }
230
231
    /**
232
     * Get the Ticket logo
233
     *
234
     * @return \Image
235
     */
236
    public function getMailLogo()
237
    {
238
        return SiteConfig::current_site_config()->TicketLogo();
239
    }
240
241
    /**
242
     * Check if the current event can have tickets
243
     *
244
     * @return bool
245
     */
246
    public function canCreateTickets()
247
    {
248
        $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...
249
        if ($currentDate && $currentDate->exists()) {
250
            return $currentDate->dbObject('StartDate')->InFuture();
251
        }
252
253
        return false;
254
    }
255
256
    /**
257
     * Get the calendar controller
258
     *
259
     * @return CalendarEvent_Controller
260
     */
261
    public function getController()
262
    {
263
        return $this->controller
264
            ? $this->controller
265
            : $this->controller = CalendarEvent_Controller::create($this->owner);
266
    }
267
}
268