DiscountField   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 101
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 78 13
1
<?php
2
/**
3
 * DiscountForm.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 10/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use FieldList;
12
use FormAction;
13
use TextField;
14
15
/**
16
 * Class DiscountForm
17
 *
18
 * @package Broarm\EventTickets
19
 */
20
class DiscountField extends TextField
21
{
22
    /**
23
     * @var DiscountForm
24
     */
25
    protected $form;
26
27
    /**
28
     * Validate the discount if it is set by...
29
     * Checking if it exists
30
     * Checking if it has uses left
31
     * Checking if it has a valid date
32
     * Checking if the event is valid
33
     * Checking if the discount is valid on one of the registered members
34
     * TODO: move all these checks to the discount itself? make a method that returns a error message
35
     *
36
     * @param \Validator $validator
37
     *
38
     * @return bool
39
     *
40
     * @throws \ValidationException
41
     */
42
    public function validate($validator)
43
    {
44
        // If no discount is set continue doing default validation
45
        if (!isset($this->value) || empty($this->value)) {
46
            return parent::validate($validator);
47
        }
48
49
        /** @var Discount $discount */
50
        // Check if the discount exists
51
        if (!$discount = Discount::get()->find('Code', $this->value)) {
52
            $validator->validationError($this->name, _t(
53
                'DiscountField.VALIDATION_NOT_FOUND',
54
                'The entered coupon is not found'
55
            ), 'validation');
56
57
            return false;
58
        }
59
60
        // Check if the discount is already used
61
        if (!$discount->validateUses()) {
62
            $validator->validationError($this->name, _t(
63
                'DiscountField.VALIDATION_USED_CHECK',
64
                'The entered coupon is already used'
65
            ), 'validation');
66
67
            return false;
68
        }
69
70
        // Check if the coupon is expired
71
        if (!$discount->validateDate()) {
72
            $validator->validationError($this->name, _t(
73
                'DiscountField.VALIDATION_DATE_CHECK',
74
                'The coupon is expired'
75
            ), 'validation');
76
77
            return false;
78
        }
79
80
        // Check if the coupon is allowed on this event
81
        if (!$discount->validateEvents($this->form->getReservation()->Event())) {
82
            $validator->validationError($this->name, _t(
83
                'DiscountField.VALIDATION_EVENT_CHECK',
84
                'The coupon is not allowed on this event'
85
            ), 'validation');
86
87
            return false;
88
        }
89
90
        // If groups are required check if one of the attendees is in the required group
91
        if (!$checkMember = $discount->validateGroups()) {
92
            foreach ($this->form->getReservation()->Attendees() as $attendee) {
93
                /** @var Attendee $attendee */
94
                if ($attendee->Member()->exists() && $member = $attendee->Member()) {
95
                    if ($checkMember = $discount->validateGroups($member)) {
96
                        // If one of the member is part of the group validate the discount
97
                        break;
98
                    } else {
99
                        $checkMember = false;
100
                    }
101
                }
102
            }
103
        }
104
105
        if (!$checkMember) {
106
            $validator->validationError($this->name, _t(
107
                'DiscountField.VALIDATION_MEMBER_CHECK',
108
                'None of the attendees is allowed to use this coupon'
109
            ), 'validation');
110
111
            return false;
112
        }
113
114
        $discount->write();
115
        $this->form->getReservation()->PriceModifiers()->add($discount);
116
        $this->form->getReservation()->calculateTotal();
117
        $this->form->getReservation()->write();
118
        return true;
119
    }
120
}
121