PreviewTicketTask::run()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.7057
c 0
b 0
f 0
cc 6
nc 8
nop 1
1
<?php
2
/**
3
 * PreviewTicketTask.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 27/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use BuildTask;
12
use Director;
13
use Dompdf\Dompdf;
14
use SSViewer;
15
16
/**
17
 * Class PreviewTicketTask
18
 * Based of the ShopEmailPreviewTask by Anselm Christophersen
19
 *
20
 * @package Broarm\EventTickets
21
 */
22
class PreviewTicketTask extends BuildTask
23
{
24
    protected $title = 'Preview Ticket email or pdf';
25
26
    protected $description = 'Preview Ticket email or pdf';
27
28
    protected $previews = array(
29
        'PrintableTicket',
30
        'ReservationMail',
31
        'NotificationMail',
32
        'AttendeeMail',
33
        'MainContactMail'
34
    );
35
36
    /**
37
     * @param \SS_HTTPRequest $request
38
     */
39
    public function run($request)
40
    {
41
        $preview = $request->remaining();
42
        $params = $request->allParams();
43
        $url = Director::absoluteURL("dev/{$params['Action']}/{$params['TaskName']}", true);
44
45
        if ($preview && in_array($preview, $this->previews)) {
46
            //$reservation = Reservation::get()->filter('ReservationCode:not', 'NULL')->last();
47
            $attendee = Attendee::get()->filter('TicketFileID:not', 0)->last();
48
49
            switch ($preview) {
50
                case 'AttendeeMail':
51
                case 'PrintableTicket':
52
                    $data = $attendee;
53
                    $template = new SSViewer($preview);
54
                    $html = $template->process($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $attendee on line 52 can be null; however, SSViewer::process() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
55
                    echo $html->getValue();
56
                    break;
57
                // TODO: preview a real pdf
58
                // TODO: preview in a iframe ?
59
                default:
60
                    $template = new SSViewer($preview);
61
                    $html = $template->process($attendee->Reservation());
62
                    \Requirements::block('app.css');
63
                    echo $html->getValue();
64
            }
65
        }
66
67
        echo '<hr><h2>Choose Preview</h2>';
68
        echo '<ul>';
69
        foreach ($this->previews as $preview) {
70
            echo '<li><a href="' . $url . '/' . $preview . '">' . $preview . '</a></li>';
71
        }
72
        echo '</ul><hr>';
73
74
        exit();
75
    }
76
}
77