Completed
Push — master ( 0cb7f2...c782d0 )
by Bram
02:42
created

Ticket::canEdit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Ticket.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 09/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use CalendarEvent;
12
use CalendarEvent_Controller;
13
use DataObject;
14
use Date;
15
use DateField;
16
use FieldList;
17
use LiteralField;
18
use NumericField;
19
use Tab;
20
use TabSet;
21
use TextField;
22
23
/**
24
 * Class Ticket
25
 *
26
 * @package Broarm\EventTickets
27
 *
28
 * @property string       Title
29
 * @property float        Price
30
 * @property string       AvailableFromDate
31
 * @property string       AvailableTillDate
32
 * @property NumericField AmountField the amount field is set on the TicketForm
33
 *
34
 * @method CalendarEvent|TicketExtension Event
35
 */
36
class Ticket extends DataObject
37
{
38
    /**
39
     * The default sale start date
40
     * This defaults to the event start date '-1 week'
41
     *
42
     * @var string
43
     */
44
    private static $sale_start_threshold = '-1 week';
0 ignored issues
show
Unused Code introduced by
The property $sale_start_threshold 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...
45
46
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
47
        'Title' => 'Varchar(255)',
48
        'Price' => 'Currency',
49
        'AvailableFromDate' => 'Date',
50
        'AvailableTillDate' => 'Date',
51
        'Sort' => 'Int'
52
    );
53
54
    private static $default_sort = 'Sort ASC, AvailableFromDate DESC';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $default_sort 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...
55
56
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one 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...
57
        'Event' => 'CalendarEvent'
58
    );
59
60
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields 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...
61
        'Title' => 'Title',
62
        'Price.NiceDecimalPoint' => 'Price',
63
        'AvailableFrom' => 'Available from',
64
        'AvailableTill' => 'Available till',
65
        'AvailableSummary' => 'Available'
66
    );
67
68
    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...
69
70
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
71
    {
72
        $fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main')));
73
74
        $fields->addFieldsToTab('Root.Main', array(
75
            TextField::create('Title', _t('Ticket.TITLE_LABEL', 'Title for the ticket')),
76
            NumericField::create('Price', _t('Ticket.PRICE_LABEL', 'Ticket price')),
77
            $saleStart = DateField::create('AvailableFromDate',
78
                _t('Ticket.SALE_START_LABEL', 'Ticket sale starts from')),
79
            $saleEnd = DateField::create('AvailableTillDate', _t('Ticket.SALE_END_LABEL', 'Ticket sale ends on'))
80
        ));
81
82
        $saleStart->setConfig('showcalendar', true);
83
        $saleStart->setDescription(_t(
84
            'Ticket.SALE_START_DESCRIPTION',
85
            'If no date is given the following date will be used: {date}', null,
86
            array('date' => $this->getAvailableFrom()->Nice())
0 ignored issues
show
Documentation introduced by
array('date' => $this->g...vailableFrom()->Nice()) is of type array<string,?,{"date":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
        ));
88
89
        $saleEnd->setConfig('showcalendar', true);
90
        $saleEnd->setDescription(_t(
91
            'Ticket.SALE_END_DESCRIPTION',
92
            'If no date is given the event start date will be used: {date}', null,
93
            array('date' => $this->getAvailableTill()->Nice())
0 ignored issues
show
Documentation introduced by
array('date' => $this->g...vailableTill()->Nice()) is of type array<string,?,{"date":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
        ));
95
96
        $this->extend('updateCMSFields', $fields);
97
        return $fields;
98
    }
99
100
    public function singular_name()
101
    {
102
        $name = explode('\\', parent::singular_name());
103
        return trim(end($name));
104
    }
105
106
    /**
107
     * Get the formatted price
108
     * @deprecated Use the "Price.NiceDecimalPoint" method instead
109
     *
110
     * @return string
111
     * /
112
    public function getPriceNice()
113
    {
114
        return $this->dbObject('Price')->NiceDecimalPoint();
115
    }*/
116
117
    /**
118
     * Get the available form date if it is set,
119
     * otherwise get it from the parent
120
     *
121
     * @return \SS_DateTime|Date|\DBField|null
122
     */
123
    public function getAvailableFrom()
124
    {
125
        if ($this->AvailableFromDate) {
126
            return $this->dbObject('AvailableFromDate');
127
        } elseif ($startDate = $this->getEventStartDate()) {
128
            $lastWeek = new Date();
129
            $lastWeek->setValue(strtotime(self::config()->get('sale_start_threshold'), strtotime($startDate->value)));
130
            return $lastWeek;
131
        }
132
133
        return null;
134
    }
135
136
    /**
137
     * Get the available till date if it is set,
138
     * otherwise get it from the parent
139
     * Use the event start date as last sale possibility
140
     *
141
     * @return \SS_DateTime|Date|\DBField|null
142
     */
143
    public function getAvailableTill()
144
    {
145
        if ($this->AvailableTillDate) {
146
            return $this->dbObject('AvailableTillDate');
147
        } elseif ($startDate = $this->getEventStartDate()) {
148
            return $startDate;
149
        }
150
151
        return null;
152
    }
153
154
    /**
155
     * Validate if the start and end date are in the past and the future
156
     *
157
     * @return bool
158
     */
159
    private function validateDate()
160
    {
161
        if ($this->getAvailableFrom() && $this->getAvailableTill()) {
162
            if ($this->getAvailableFrom()->InPast() && $this->getAvailableTill()->InFuture()) {
163
                return true;
164
            }
165
        }
166
167
        return false;
168
    }
169
170
    /**
171
     * Validate the available capacity
172
     *
173
     * @return bool
174
     */
175
    private function validateAvailability()
176
    {
177
        return $this->Event()->getAvailability() > 0;
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
178
    }
179
180
    /**
181
     * Return if the ticket is available or not
182
     *
183
     * @return bool
184
     */
185
    public function getAvailable()
186
    {
187
        if (!$this->getAvailableFrom() && !$this->getAvailableTill()) {
188
            return false;
189
        } elseif ($this->validateDate() && $this->validateAvailability()) {
190
            return true;
191
        }
192
193
        return false;
194
    }
195
196
    /**
197
     * Return availability for use in grid fields
198
     *
199
     * @return LiteralField
200
     */
201
    public function getAvailableSummary()
202
    {
203
        $available = $this->getAvailable()
204
            ? '<span style="color: #3adb76;">' . _t('Ticket.AVAILABLE', 'Tickets available') . '</span>'
205
            : '<span style="color: #cc4b37;">' . _t('Ticket.UNAVAILABLE', 'Not for sale') . '</span>';
206
207
        return new LiteralField('Available', $available);
208
    }
209
210
    /**
211
     * Get the event start date
212
     *
213
     * @return Date
214
     */
215
    private function getEventStartDate()
216
    {
217
        if ($this->Event()->getController()->CurrentDate()->exists()) {
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
218
            return $this->Event()->getController()->CurrentDate()->obj('StartDate');
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
219
        } else {
220
            return null;
221
        }
222
    }
223
224
    public function canView($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
225
    {
226
        return $this->Event()->canView($member);
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
227
    }
228
229
    public function canEdit($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
230
    {
231
        return $this->Event()->canEdit($member);
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
232
    }
233
234
    public function canDelete($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
235
    {
236
        return $this->Event()->canDelete($member);
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
237
    }
238
239
    public function canCreate($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
240
    {
241
        return $this->Event()->canCreate($member);
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
242
    }
243
}
244