Completed
Branch Gutenberg/form-system (1dc3a2)
by
unknown
88:07 queued 79:28
created

EventAttendeesBlockRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\blocks;
4
5
use DomainException;
6
use EE_Error;
7
use EEH_Template;
8
use EEM_Attendee;
9
use EventEspresso\core\domain\DomainInterface;
10
use EventEspresso\core\services\blocks\BlockRenderer;
11
12
/**
13
 * EventAttendeesBlockRenderer
14
 *
15
 *
16
 * @package EventEspresso\core\domain\services\blocks
17
 * @author  Darren Ethier
18
 * @since   $VID:$
19
 */
20
class EventAttendeesBlockRenderer extends BlockRenderer
21
{
22
23
    /**
24
     * @var EEM_Attendee
25
     */
26
    private $attendee_model;
27
28
    public function __construct(DomainInterface $domain, EEM_Attendee $attendee_model)
29
    {
30
        $this->attendee_model = $attendee_model;
31
        parent::__construct($domain);
32
    }
33
34
35
    /**
36
     * Renders the block.
37
     *
38
     * @param array $attributes  Expect already validated and sanitized array of attributes for use in generating the
39
     *                           query and the template output.
40
     * @return string
41
     * @throws DomainException
42
     * @throws EE_Error
43
     */
44
    public function render(array $attributes)
45
    {
46
        $template_args['attributes'] = $attributes;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$template_args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $template_args = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
47
        $template_args['attendees'] = $this->attendee_model->get_all($this->getQueryParams($attributes));
48
        return EEH_Template::display_template(
49
            $this->templateRootPath() . 'event-attendees.php',
50
            $template_args,
51
            true
52
        );
53
    }
54
55
56
    /**
57
     * Get query parameters for model query.
58
     *
59
     * @param array $attributes
60
     * @return array
61
     */
62
    private function getQueryParams(array $attributes)
63
    {
64
        return array(
65
            0 => $this->getWhereQueryPart($attributes),
66
            'default_where_conditions' => 'this_model_only',
67
            'limit' => $attributes['limit'],
68
            'group_by' => array('ATT_ID'),
69
            'order_by' => $this->getOrderByQueryPart($attributes)
70
        );
71
    }
72
73
74
    /**
75
     * Get where query part for query parameters for model query.
76
     *
77
     * @param array $attributes
78
     * @return array
79
     */
80
    private function getWhereQueryPart(array $attributes)
81
    {
82
        $where = array();
83
        if ($attributes['ticketId'] > 0) {
84
            $where['Registration.TKT_ID'] = $attributes['ticketId'];
85
        } elseif ($attributes['datetimeId'] > 0) {
86
            $where['Registration.Ticket.Datetime.DTT_ID'] = $attributes['datetimeId'];
87
        } else {
88
            $where['Registration.EVT_ID'] = $attributes['eventId'];
89
        }
90
        $where['Registration.STS_ID'] = $attributes['status'];
91
        return $where;
92
    }
93
94
95
    /**
96
     * Get order by query part for query parameters for model query.
97
     *
98
     * @param array $attributes
99
     * @return array
100
     */
101
    private function getOrderByQueryPart(array $attributes)
102
    {
103
        $order = $attributes['order'];
104
        switch ($attributes['orderBy']) {
105
            case 'id':
106
                $order_by = array('ATT_ID' => $order);
107
                break;
108
            case 'lastNameOnly':
109
                $order_by = array('ATT_lname' => $order);
110
                break;
111
            case 'firstNameOnly':
112
                $order_by = array('ATT_fname' => $order);
113
                break;
114
            case 'firstThenLastName':
115
                $order_by = array('ATT_fname' => $order, 'ATT_lname' => $order);
116
                break;
117
            default:
118
                $order_by = array('ATT_lname' => $order, 'ATT_fname' => $order);
119
                break;
120
        }
121
        return $order_by;
122
    }
123
}
124