Completed
Push — master ( 996639...c6213a )
by Bram
02:34
created

SuccessController::sendTickets()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 48
rs 8.7396
cc 4
eloc 32
nc 6
nop 0
1
<?php
2
/**
3
 * SuccessController.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 16/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use Config;
12
use Email;
13
use SiteConfig;
14
15
/**
16
 * Class SuccessController
17
 *
18
 * @package Broarm\EventTickets
19
 */
20
class SuccessController extends CheckoutStepController
21
{
22
    protected $step = 'success';
23
24
    /**
25
     * @var Reservation
26
     */
27
    protected $reservation;
28
29
    /**
30
     * The address to whom the ticket notifications are sent
31
     * By default the admin email is used
32
     *
33
     * @config
34
     * @var string
35
     */
36
    private static $mail_sender;
1 ignored issue
show
Unused Code introduced by
The property $mail_sender 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...
37
38
    /**
39
     * The address from where the ticket mails are sent
40
     * By default the admin email is used
41
     *
42
     * @config
43
     * @var string
44
     */
45
    private static $mail_receiver;
0 ignored issues
show
Unused Code introduced by
The property $mail_receiver 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
47
    public function init()
48
    {
49
        parent::init();
50
51
        if ($this->reservation = ReservationSession::get()) {
0 ignored issues
show
Documentation Bug introduced by
It seems like \Broarm\EventTickets\ReservationSession::get() can also be of type object<DataObject>. However, the property $reservation is declared as type object<Broarm\EventTickets\Reservation>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
            // If we get to the success controller form any state except PENDING or PAID
53
            // This would mean someone would be clever and change the url from summary to success bypassing the payment
54
            // End the session, thus removing the reservation, and redirect back
55
            if (!in_array($this->reservation->Status, array('PENDING', 'PAID'))) {
56
                ReservationSession::end();
57
                $this->redirect($this->Link('/'));
58
            } else {
59
                $this->reservation->createFiles();
60
                if ($this->sendReservation()) {
61
                    $this->reservation->changeState('PAID');
62
                    $this->sendTickets();
63
                    $this->sendNotification();
64
                    $this->extend('afterPaymentComplete', $this->reservation);
65
                    $this->reservation->write();
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * Send the reservation mail
73
     *
74
     * @return bool
75
     */
76
    public function sendReservation()
77
    {
78
        // State changes after the files have been sent
79
        // this check makes sure the files aren't sent again after a refresh
80
        if ($this->reservation->Status === 'PAID') {
81
            return false;
82
        }
83
84
        // Get the mail sender or fallback to the admin email
85
        if (empty($from = self::config()->get('mail_sender'))) {
86
            $from = Config::inst()->get('Email', 'admin_email');
87
        }
88
89
        // Create the email with given template and reservation data
90
        $email = new Email();
91
        $email->setSubject(_t(
92
            'ReservationMail.TITLE',
93
            'Your order at {sitename}',
94
            null,
95
            array(
0 ignored issues
show
Documentation introduced by
array('sitename' => \Sit...t_site_config()->Title) is of type array<string,?,{"sitename":"?"}>, 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...
96
                'sitename' => SiteConfig::current_site_config()->Title
97
            )
98
        ));
99
        $email->setFrom($from);
100
        $email->setTo($this->reservation->MainContact()->Email);
0 ignored issues
show
Bug introduced by
The method MainContact() does not exist on Broarm\EventTickets\Reservation. Did you maybe mean setMainContact()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
101
        $email->setTemplate('ReservationMail');
102
        $email->populateTemplate($this->reservation);
103
        $this->extend('updateReservationMail', $email);
104
        $email->send();
105
        return true;
106
    }
107
108
    /**
109
     * Send the reservation mail
110
     */
111
    public function sendTickets()
112
    {
113
        // Get the mail sender or fallback to the admin email
114
        if (empty($from = self::config()->get('mail_sender'))) {
115
            $from = Config::inst()->get('Email', 'admin_email');
116
        }
117
118
        // Send the tickets to the main contact
119
        $email = new Email();
120
        $email->setSubject(_t(
121
            'MainContactMail.TITLE',
122
            'Uw tickets voor {event}',
123
            null,
124
            array(
0 ignored issues
show
Documentation introduced by
array('event' => $this->...vation->Event()->Title) is of type array<string,?,{"event":"?"}>, 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...
125
                'event' => $this->reservation->Event()->Title
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Reservation>? 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...
126
            )
127
        ));
128
        $email->setFrom($from);
129
        $email->setTo($this->reservation->MainContact()->Email);
0 ignored issues
show
Bug introduced by
The method MainContact() does not exist on Broarm\EventTickets\Reservation. Did you maybe mean setMainContact()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
130
        $email->setTemplate('MainContactMail');
131
        $email->populateTemplate($this->reservation);
132
        $this->extend('updateMainContactMail', $email);
133
        $email->send();
134
135
136
        // Get the attendees for this event that are checked as receiver
137
        $ticketReceivers = $this->reservation->Attendees()->filter('TicketReceiver', 1)->exclude('ID', $this->reservation->MainContactID);
0 ignored issues
show
Documentation Bug introduced by
The method Attendees does not exist on object<Broarm\EventTickets\Reservation>? 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...
138
        if ($ticketReceivers->exists()) {
139
            /** @var Attendee $ticketReceiver */
140
            foreach ($ticketReceivers as $ticketReceiver) {
141
                $email = new Email();
142
                $email->setSubject(_t(
143
                    'AttendeeMail.TITLE',
144
                    'Your ticket for {event}',
145
                    null,
146
                    array(
0 ignored issues
show
Documentation introduced by
array('event' => $this->...vation->Event()->Title) is of type array<string,?,{"event":"?"}>, 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...
147
                        'event' => $this->reservation->Event()->Title
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Reservation>? 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...
148
                    )
149
                ));
150
                $email->setFrom($from);
151
                $email->setTo($ticketReceiver->Email);
152
                $email->setTemplate('AttendeeMail');
153
                $email->populateTemplate($ticketReceiver);
154
                $this->extend('updateTicketMail', $email);
155
                $email->send();
156
            }
157
        }
158
    }
159
160
161
    /**
162
     * Send a booking notification tot the ticket mail sender or the site admin
163
     */
164
    public function sendNotification()
165
    {
166
        if (empty($from = self::config()->get('mail_sender'))) {
167
            $from = Config::inst()->get('Email', 'admin_email');
168
        }
169
170
        if (empty($to = self::config()->get('mail_receiver'))) {
171
            $to = Config::inst()->get('Email', 'admin_email');
172
        }
173
174
        $email = new Email();
175
        $email->setSubject(_t(
176
            'NotificationMail.TITLE',
177
            'Nieuwe reservering voor {event}',
178
            null, array('event' => $this->reservation->Event()->Title)
0 ignored issues
show
Documentation Bug introduced by
The method Event does not exist on object<Broarm\EventTickets\Reservation>? 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...
Documentation introduced by
array('event' => $this->...vation->Event()->Title) is of type array<string,?,{"event":"?"}>, 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...
179
        ));
180
181
        $email->setFrom($from);
182
        $email->setTo($to);
183
        $email->setTemplate('NotificationMail');
184
        $email->populateTemplate($this->reservation);
185
        $this->extend('updateNotificationMail', $email);
186
        $email->send();
187
    }
188
189
    /**
190
     * Get the download link
191
     *
192
     * @return string
193
     */
194
    public function getDownloadLink()
195
    {
196
        return $this->reservation->Attendees()->first()->TicketFile()->Link();
0 ignored issues
show
Documentation Bug introduced by
The method Attendees does not exist on object<Broarm\EventTickets\Reservation>? 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...
197
    }
198
}
199