Completed
Branch FET-9222-rest-api-writes (1e6651)
by
unknown
81:25 queued 66:40
created

EE_Venue_Shortcodes   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 258
Duplicated Lines 10.85 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
dl 28
loc 258
rs 6.8
c 0
b 0
f 0
wmc 55
lcom 2
cbo 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B _init_props() 28 28 1
C _parser() 0 66 16
F _venue() 0 102 33
B get_map_attributes() 0 20 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like EE_Venue_Shortcodes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EE_Venue_Shortcodes, and based on these observations, apply Extract Interface, too.

1
<?php
2
defined('EVENT_ESPRESSO_VERSION') || exit('No direct access allowed.');
3
4
/**
5
 * EE_Venue_Shortcodes
6
 * this is a child class for the EE_Shortcodes library.  The EE_Venue_Shortcodes lists all shortcodes related to venue
7
 * specific info. NOTE: if a method doesn't have any phpdoc commenting the details can be found in the comments in
8
 * EE_Shortcodes parent class.
9
 *
10
 * @package        Event Espresso
11
 * @subpackage     libraries/shortcodes/EE_Venue_Shortcodes.lib.php
12
 * @author         Darren Ethier
13
 * ------------------------------------------------------------------------
14
 */
15
class EE_Venue_Shortcodes extends EE_Shortcodes
16
{
17
18
19
    /**
20
     * Will hold the EE_Event if available
21
     *
22
     * @var EE_Event
23
     */
24
    protected $_event;
25
26
27
    /**
28
     * Initialize properties
29
     */
30 View Code Duplication
    protected function _init_props()
31
    {
32
        $this->label       = esc_html__('Venue Shortcodes', 'event_espresso');
33
        $this->description = esc_html__('All shortcodes specific to venue related data', 'event_espresso');
34
        $this->_shortcodes = array(
35
            '[VENUE_TITLE]'             => esc_html__('The title for the event venue', 'event_espresso'),
36
            '[VENUE_DESCRIPTION]'       => esc_html__('The description for the event venue', 'event_espresso'),
37
            '[VENUE_URL]'               => esc_html__('A url to a webpage for the venue', 'event_espresso'),
38
            '[VENUE_IMAGE]'             => esc_html__('An image representing the event venue', 'event_espresso'),
39
            '[VENUE_PHONE]'             => esc_html__('The phone number for the venue', 'event_espresso'),
40
            '[VENUE_ADDRESS]'           => esc_html__('The address for the venue', 'event_espresso'),
41
            '[VENUE_ADDRESS2]'          => esc_html__('Address 2 for the venue', 'event_espresso'),
42
            '[VENUE_CITY]'              => esc_html__('The city the venue is in', 'event_espresso'),
43
            '[VENUE_STATE]'             => esc_html__('The state the venue is located in', 'event_espresso'),
44
            '[VENUE_COUNTRY]'           => esc_html__('The country the venue is located in', 'event_espresso'),
45
            '[VENUE_FORMATTED_ADDRESS]' => esc_html__(
46
                'This just outputs the venue address in a semantic address format.',
47
                'event_espresso'
48
            ),
49
            '[VENUE_ZIP]'               => esc_html__('The zip code for the venue address', 'event_espresso'),
50
            '[GOOGLE_MAP_URL]'          => esc_html__(
51
                'URL for the google map associated with the venue.',
52
                'event_espresso'
53
            ),
54
            '[GOOGLE_MAP_LINK]'         => esc_html__('Link to a google map for the venue', 'event_espresso'),
55
            '[GOOGLE_MAP_IMAGE]'        => esc_html__('Google map for venue wrapped in image tags', 'event_espresso'),
56
        );
57
    }
58
59
60
    /**
61
     * Parse incoming shortcode
62
     * @param string $shortcode
63
     * @return string
64
     */
65
    protected function _parser($shortcode)
66
    {
67
68
        switch ($shortcode) {
69
            case '[VENUE_TITLE]':
70
                return $this->_venue('title');
71
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72
73
            case '[VENUE_DESCRIPTION]':
74
                return $this->_venue('description');
75
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
76
77
            case '[VENUE_URL]':
78
                return $this->_venue('url');
79
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
80
81
            case '[VENUE_IMAGE]':
82
                return $this->_venue('image');
83
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
84
85
            case '[VENUE_PHONE]':
86
                return $this->_venue('phone');
87
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
88
89
            case '[VENUE_ADDRESS]':
90
                return $this->_venue('address');
91
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
92
93
            case '[VENUE_ADDRESS2]':
94
                return $this->_venue('address2');
95
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
96
97
            case '[VENUE_CITY]':
98
                return $this->_venue('city');
99
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
100
101
            case '[VENUE_COUNTRY]':
102
                return $this->_venue('country');
103
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
104
105
            case '[VENUE_STATE]':
106
                return $this->_venue('state');
107
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
108
109
            case '[VENUE_ZIP]':
110
                return $this->_venue('zip');
111
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
112
113
            case '[VENUE_FORMATTED_ADDRESS]':
114
                return $this->_venue('formatted_address');
115
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
116
117
            case '[GOOGLE_MAP_URL]':
118
                return $this->_venue('gmap_url');
119
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
120
121
            case '[GOOGLE_MAP_LINK]':
122
                return $this->_venue('gmap_link');
123
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
124
125
            case '[GOOGLE_MAP_IMAGE]':
126
                return $this->_venue('gmap_link_img');
127
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
128
129
        }
130
    }
131
132
133
    /**
134
     * This retrieves the specified venue information
135
     *
136
     * @param string $field  What Venue field to retrieve
137
     * @return string What was retrieved!
138
     * @throws EE_Error
139
     * @throws \EventEspresso\core\exceptions\EntityNotFoundException
140
     */
141
    private function _venue($field)
142
    {
143
        //we need the EE_Event object to get the venue.
144
        $this->_event = $this->_data instanceof EE_Event ? $this->_data : null;
145
146
        //if no event, then let's see if there is a reg_obj.  If there IS, then we'll try and grab the event from the reg_obj instead.
147
        if (empty($this->_event)) {
148
            $aee = $this->_data instanceof EE_Messages_Addressee ? $this->_data : null;
149
            $aee = $this->_extra_data instanceof EE_Messages_Addressee ? $this->_extra_data : $aee;
150
151
            $this->_event = $aee instanceof EE_Messages_Addressee && $aee->reg_obj instanceof EE_Registration ? $aee->reg_obj->event() : null;
152
153
            //if still empty do we have a ticket data item?
154
            $this->_event = empty($this->_event)
155
                            && $this->_data instanceof EE_Ticket
156
                            && $this->_extra_data['data'] instanceof EE_Messages_Addressee
157
                ? $this->_extra_data['data']->tickets[$this->_data->ID()]['EE_Event']
158
                : $this->_event;
159
160
            //if STILL empty event, let's try to get the first event in the list of events via EE_Messages_Addressee and use that.
161
            $event        = $aee instanceof EE_Messages_Addressee ? reset($aee->events) : array();
162
            $this->_event = empty($this->_event) && ! empty($events) ? $event : $this->_event;
0 ignored issues
show
Bug introduced by
The variable $events does not exist. Did you mean $event?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
163
        }
164
165
166
        //If there is no event objecdt by now then get out.
167
        if (! $this->_event instanceof EE_Event) {
168
            return '';
169
        }
170
171
        /** @var EE_Venue $venue */
172
        $venue = $this->_event->get_first_related('Venue');
173
174
        if (empty($venue)) {
175
            return '';
176
        } //no venue so get out.
177
178
        switch ($field) {
179
            case 'title':
180
                return $venue->get('VNU_name');
181
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
182
183
            case 'description':
184
                return $venue->get('VNU_desc');
185
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
186
187
            case 'url':
188
                $url = $venue->get('VNU_url');
189
                return empty($url) ? $venue->get_permalink() : $url;
190
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
191
192
            case 'image':
193
                return '<img src="' . $venue->feature_image_url(array(200, 200,))
194
                       . '" alt="' . sprintf(
195
                           esc_attr__('%s Feature Image', 'event_espresso'),
196
                           $venue->get('VNU_name')
197
                       ) . '" />';
198
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
199
200
            case 'phone':
201
                return $venue->get('VNU_phone');
202
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
203
204
            case 'address':
205
                return $venue->get('VNU_address');
206
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
207
208
            case 'address2':
209
                return $venue->get('VNU_address2');
210
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
211
212
            case 'city':
213
                return $venue->get('VNU_city');
214
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
215
216
            case 'state':
217
                $state = $venue->state_obj();
218
                return is_object($state) ? $state->get('STA_name') : '';
219
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
220
221
            case 'country':
222
                $country = $venue->country_obj();
223
                return is_object($country) ? $country->get('CNT_name') : '';
224
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
225
226
            case 'zip':
227
                return $venue->get('VNU_zip');
228
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
229
230
            case 'formatted_address':
231
                return EEH_Address::format($venue);
232
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
233
234
            case 'gmap_link':
235
            case 'gmap_url':
236
            case 'gmap_link_img':
237
                $atts = $this->get_map_attributes($venue, $field);
238
                return EEH_Maps::google_map_link($atts);
239
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
240
        }
241
        return '';
242
    }
243
244
245
    /**
246
     * Generates the attributes for retrieving a google_map artifact.
247
     * @param EE_Venue $venue
248
     * @param string   $field
249
     * @return array
250
     * @throws EE_Error
251
     */
252
    protected function get_map_attributes(EE_Venue $venue, $field = 'gmap_link')
253
    {
254
        $state   = $venue->state_obj();
255
        $country = $venue->country_obj();
256
        $atts    = array(
257
            'id'      => $venue->ID(),
258
            'address' => $venue->get('VNU_address'),
259
            'city'    => $venue->get('VNU_city'),
260
            'state'   => is_object($state) ? $state->get('STA_name') : '',
261
            'zip'     => $venue->get('VNU_zip'),
262
            'country' => is_object($country) ? $country->get('CNT_name') : '',
263
            'type'    => $field === 'gmap_link' ? 'url' : 'map',
264
            'map_w'   => 200,
265
            'map_h'   => 200,
266
        );
267
        if ($field === 'gmap_url') {
268
            $atts['type'] = 'url_only';
269
        }
270
        return $atts;
271
    }
272
}
273