Completed
Push — master ( 22f31a...c89319 )
by Bram
02:59
created

TicketExtension::onBeforeWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * TicketExtension.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 09/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use CalendarEvent_Controller;
12
use DataExtension;
13
use FieldList;
14
use GridField;
15
use GridFieldAddNewButton;
16
use GridFieldConfig_RecordEditor;
17
use HtmlEditorField;
18
use LiteralField;
19
use NumericField;
20
use SiteConfig;
21
22
/**
23
 * Class TicketExtension
24
 *
25
 * @package Broarm\EventTickets
26
 *
27
 * @property TicketExtension|\CalendarEvent $owner
28
 * @property int                            Capacity
29
 * @property string                         SuccessMessage
30
 * @property string                         SuccessMessageMail
31
 *
32
 * @method \HasManyList Tickets()
33
 * @method \HasManyList Reservations()
34
 * @method \HasManyList Attendees()
35
 * @method \HasManyList WaitingList()
36
 * @method \HasManyList Fields()
37
 */
38
class TicketExtension extends DataExtension
39
{
40
    /**
41
     * @var CalendarEvent_Controller
42
     */
43
    protected $controller;
44
45
    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...
46
        'Capacity' => 'Int',
47
        'SuccessMessage' => 'HTMLText',
48
        'SuccessMessageMail' => 'HTMLText'
49
    );
50
51
    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...
52
        'Tickets' => 'Broarm\EventTickets\Ticket.Event',
53
        'Reservations' => 'Broarm\EventTickets\Reservation.Event',
54
        'Attendees' => 'Broarm\EventTickets\Attendee.Event',
55
        'WaitingList' => 'Broarm\EventTickets\WaitingListRegistration.Event',
56
        'Fields' => 'Broarm\EventTickets\AttendeeExtraField.Event'
57
    );
58
59
    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...
60
        'SuccessMessage',
61
        'SuccessMessageMail'
62
    );
63
64
    public function updateCMSFields(FieldList $fields)
65
    {
66
        $gridFieldConfig = GridFieldConfig_RecordEditor::create();
67
68
        // If the event dates are in the past remove ability to create new tickets, reservations and attendees.
69
        // This is done here instead of in the canCreate method because of the lack of context there.
70
        if (!$this->canCreateTickets()) {
71
            $gridFieldConfig->removeComponentsByType(new GridFieldAddNewButton());
72
        }
73
74
        $ticketLabel = _t('TicketExtension.Tickets', 'Tickets');
75
        $fields->addFieldsToTab(
76
            "Root.$ticketLabel", array(
77
            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...
78
            NumericField::create('Capacity', _t('TicketExtension.Capacity', 'Capacity')),
79
            HtmlEditorField::create('SuccessMessage',
80
                _t('TicketExtension.SuccessMessage', 'Success message'))->setRows(4),
81
            HtmlEditorField::create('SuccessMessageMail', _t('TicketExtension.MailMessage', 'Mail message'))->setRows(4)
82
        ));
83
84 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...
85
            $reservationLabel = _t('TicketExtension.Reservations', 'Reservations');
86
            $fields->addFieldToTab(
87
                "Root.$reservationLabel",
88
                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...
89
            );
90
        }
91
92 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...
93
            $guestListLabel = _t('TicketExtension.GuestList', 'GuestList');
94
            $fields->addFieldToTab(
95
                "Root.$guestListLabel",
96
                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...
97
            );
98
        }
99
100 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...
101
            $waitingListLabel = _t('TicketExtension.WaitingList', 'WaitingList');
102
            $fields->addFieldToTab(
103
                "Root.$waitingListLabel",
104
                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...
105
            );
106
        }
107
108
109
        $extraFieldsLabel = _t('TicketExtension.ExtraFields', 'Attendee fields');
110
        $fields->addFieldToTab(
111
            "Root.$extraFieldsLabel",
112
            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...
113
                GridFieldConfig_Fields::create())
114
        );
115
116
    }
117
118
    public function onBeforeWrite()
119
    {
120
        $this->createDefaultFieldsBlob();
121
        //parent::onAfterWrite();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
122
    }
123
124
    public function createDefaultFieldsBlob()
125
    {
126
        $fields = Attendee::config()->get('default_fields');
127
        if (!$this->owner->Fields()->exists()) {
128
            foreach ($fields as $fieldName => $fieldType) {
129
                $field = AttendeeExtraField::create();
130
                $field->Title = _t("AttendeeField.$fieldName", $fieldName);
131
                $field->FieldName = $fieldName;
132
                $field->FieldType = $fieldType;
133
                $field->Required = true;
134
                $field->Editable = false;
135
                $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...
136
            }
137
        }
138
    }
139
140
    /**
141
     * Extend the page actions with an start check in action
142
     *
143
     * @param FieldList $actions
144
     */
145
    public function updateCMSActions(FieldList $actions)
146
    {
147
        $checkInButton = new LiteralField('StartCheckIn',
148
            "<a class='action ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
149
                id='Edit_StartCheckIn'
150
                role='button'
151
                href='{$this->owner->Link('checkin')}'
152
                target='_blank'>
153
                Start check in
154
            </a>"
155
        );
156
157
        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...
158
            $actions->push($checkInButton);
159
        }
160
    }
161
162
    /**
163
     * Get the leftover capacity
164
     *
165
     * @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...
166
     */
167
    public function getAvailability()
168
    {
169
        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...
170
    }
171
172
    /**
173
     * Get the success message
174
     *
175
     * @return mixed|string
176
     */
177 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...
178
    {
179
        if (!empty($this->owner->SuccessMessage)) {
180
            return $this->owner->dbObject('SuccessMessage');
181
        } else {
182
            return SiteConfig::current_site_config()->dbObject('SuccessMessage');
183
        }
184
    }
185
186
    /**
187
     * Get the mail message
188
     *
189
     * @return mixed|string
190
     */
191 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...
192
    {
193
        if (!empty($this->owner->SuccessMessageMail)) {
194
            return $this->owner->dbObject('SuccessMessageMail');
195
        } else {
196
            return SiteConfig::current_site_config()->dbObject('SuccessMessageMail');
197
        }
198
    }
199
200
    /**
201
     * Get the Ticket logo
202
     *
203
     * @return \Image
204
     */
205
    public function getMailLogo()
206
    {
207
        return SiteConfig::current_site_config()->TicketLogo();
208
    }
209
210
    /**
211
     * Check if the current event can have tickets
212
     *
213
     * @return bool
214
     */
215
    public function canCreateTickets()
216
    {
217
        $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...
218
        if ($currentDate && $currentDate->exists()) {
219
            return $currentDate->dbObject('StartDate')->InFuture();
220
        }
221
222
        return false;
223
    }
224
225
    /**
226
     * Get the calendar controller
227
     *
228
     * @return CalendarEvent_Controller
229
     */
230
    public function getController()
231
    {
232
        return $this->controller
233
            ? $this->controller
234
            : $this->controller = CalendarEvent_Controller::create($this->owner);
235
    }
236
}
237