Completed
Branch EDTR/master (da238f)
by
unknown
09:58 queued 01:23
created

RouteHandler   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 322
Duplicated Lines 18.01 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 58
loc 322
rs 5.5199
c 0
b 0
f 0
wmc 56
lcom 1
cbo 8

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B handleAssetManagerRequest() 0 21 7
A canLoadBlocks() 0 8 3
A handleControllerRequest() 0 11 2
A handleAdminRequest() 0 22 4
A handleFrontendRequest() 13 13 5
A isGQLRequest() 0 11 4
A handleGQLRequest() 0 18 4
A handlePersonalDataRequest() 16 16 5
A handlePueRequest() 0 12 4
A handleSessionRequest() 11 11 5
A handleShortcodesRequest() 18 18 5
A handleWordPressHeartbeatRequest() 0 11 3
A handleWordPressPluginsPage() 0 27 4

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 RouteHandler 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 RouteHandler, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace EventEspresso\core\services\routing;
4
5
use EE_Config;
6
use EE_Dependency_Map;
7
use EE_Maintenance_Mode;
8
use EventEspresso\core\domain\services\admin\PluginUpsells;
9
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay;
10
use EventEspresso\core\services\loaders\LoaderInterface;
11
use EventEspresso\core\services\request\RequestInterface;
12
use Exception;
13
14
/**
15
 * Class RouteHandler
16
 * Description
17
 *
18
 * @package EventEspresso\core\domain\services\admin
19
 * @author  Brent Christensen
20
 * @since   $VID:$
21
 */
22
class RouteHandler
23
{
24
25
    /**
26
     * @var LoaderInterface
27
     */
28
    private $loader;
29
30
    /**
31
     * @var EE_Maintenance_Mode $maintenance_mode
32
     */
33
    private $maintenance_mode;
34
35
    /**
36
     * @var RequestInterface $request
37
     */
38
    private $request;
39
40
    /**
41
     * @var RouteMatchSpecificationManager $route_manager
42
     */
43
    private $route_manager;
44
45
    /**
46
     * AdminRouter constructor.
47
     *
48
     * @param LoaderInterface  $loader
49
     * @param EE_Maintenance_Mode $maintenance_mode
50
     * @param RequestInterface $request
51
     * @param RouteMatchSpecificationManager $route_manager
52
     */
53
    public function __construct(
54
        LoaderInterface $loader,
55
        EE_Maintenance_Mode $maintenance_mode,
56
        RequestInterface $request,
57
        RouteMatchSpecificationManager $route_manager
58
    ) {
59
        $this->loader = $loader;
60
        $this->maintenance_mode = $maintenance_mode;
61
        $this->request = $request;
62
        $this->route_manager = $route_manager;
63
    }
64
65
66
    /**
67
     * @throws Exception
68
     * @since $VID:$
69
     */
70
    public function handleAssetManagerRequest()
71
    {
72
        try {
73
            if (! $this->request->isAdmin()
74
                && ! $this->request->isFrontend()
75
                && ! $this->request->isIframe()
76
                && ! $this->request->isWordPressApi()
77
            ) {
78
                return;
79
            }
80
            $this->loader->getShared('EventEspresso\core\services\assets\Registry');
81
            $this->loader->getShared('EventEspresso\core\domain\services\assets\CoreAssetManager');
82
            if ($this->canLoadBlocks()) {
83
                $this->loader->getShared(
84
                    'EventEspresso\core\services\editor\BlockRegistrationManager'
85
                );
86
            }
87
        } catch (Exception $exception) {
88
            new ExceptionStackTraceDisplay($exception);
89
        }
90
    }
91
92
93
    /**
94
     * Return whether blocks can be registered/loaded or not.
95
     *
96
     * @return bool
97
     * @since $VID:$
98
     */
99
    private function canLoadBlocks()
100
    {
101
        return apply_filters('FHEE__EE_System__canLoadBlocks', true)
102
               && function_exists('register_block_type')
103
               // don't load blocks if in the Divi page builder editor context
104
               // @see https://github.com/eventespresso/event-espresso-core/issues/814
105
               && ! $this->request->getRequestParam('et_fb', false);
106
    }
107
108
109
    /**
110
     * @throws Exception
111
     * @since $VID:$
112
     */
113
    public function handleControllerRequest()
114
    {
115
        try {
116
            $this->handleAdminRequest();
117
            $this->handleFrontendRequest();
118
            $this->handleWordPressHeartbeatRequest();
119
            $this->handleWordPressPluginsPage();
120
        } catch (Exception $exception) {
121
            new ExceptionStackTraceDisplay($exception);
122
        }
123
    }
124
125
126
    /**
127
     * @throws Exception
128
     * @since $VID:$
129
     */
130
    private function handleAdminRequest()
131
    {
132
        try {
133
            if (! $this->request->isAdmin() || $this->request->isAdminAjax()) {
134
                return;
135
            }
136
            do_action('AHEE__EE_System__load_controllers__load_admin_controllers');
137
            $this->loader->getShared('EE_Admin');
138
139
            EE_Dependency_Map::register_dependencies(
140
                'EventEspresso\core\domain\services\assets\EspressoAdminAssetManager',
141
                [
142
                    'EventEspresso\core\domain\Domain'                   => EE_Dependency_Map::load_from_cache,
143
                    'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache,
144
                    'EventEspresso\core\services\assets\Registry'        => EE_Dependency_Map::load_from_cache,
145
                ]
146
            );
147
            $this->loader->getShared('EventEspresso\core\domain\services\assets\EspressoAdminAssetManager');
148
        } catch (Exception $exception) {
149
            new ExceptionStackTraceDisplay($exception);
150
        }
151
    }
152
153
154
    /**
155
     * @throws Exception
156
     * @since $VID:$
157
     */
158 View Code Duplication
    private function handleFrontendRequest()
159
    {
160
        try {
161
            // don't load frontend if M-Mode is active or request is not browser HTTP
162
            if ($this->maintenance_mode->level() || ! $this->request->isFrontend() || ! $this->request->isFrontAjax()) {
163
                return;
164
            }
165
            do_action('AHEE__EE_System__load_controllers__load_front_controllers');
166
            $this->loader->getShared('EE_Front_Controller');
167
        } catch (Exception $exception) {
168
            new ExceptionStackTraceDisplay($exception);
169
        }
170
    }
171
172
173
    /**
174
     * @return bool
175
     * @since $VID:$
176
     */
177
    private function isGQLRequest()
178
    {
179
        return PHP_VERSION_ID > 70000
180
               && (
181
                   $this->request->isGQL()
182
                   || $this->request->isUnitTesting()
183
                   || $this->route_manager->routeMatchesCurrentRequest(
184
                       'EventEspresso\core\domain\entities\routing\specifications\admin\EspressoEventEditor'
185
                   )
186
               );
187
    }
188
189
190
    /**
191
     * @throws Exception
192
     * @since $VID:$
193
     */
194
    public function handleGQLRequest()
195
    {
196
        try {
197
            if (! $this->isGQLRequest()) {
198
                return;
199
            }
200
            if (! class_exists('WPGraphQL')) {
201
                require_once EE_THIRD_PARTY . 'wp-graphql/wp-graphql.php';
202
            }
203
            // load handler for EE GraphQL requests
204
            $graphQL_manager = $this->loader->getShared(
205
                'EventEspresso\core\services\graphql\GraphQLManager'
206
            );
207
            $graphQL_manager->init();
208
        } catch (Exception $exception) {
209
            new ExceptionStackTraceDisplay($exception);
210
        }
211
    }
212
213
214
    /**
215
     * @throws Exception
216
     * @since $VID:$
217
     */
218 View Code Duplication
    public function handlePersonalDataRequest()
219
    {
220
        try {
221
            // don't load frontend if M-Mode is active or request is not browser HTTP
222
            if (! $this->request->isAdmin()
223
                || ! $this->request->isAjax()
224
                || ! $this->maintenance_mode->models_can_query()
225
            ) {
226
                return;
227
            }
228
            $this->loader->getShared('EventEspresso\core\services\privacy\erasure\PersonalDataEraserManager');
229
            $this->loader->getShared('EventEspresso\core\services\privacy\export\PersonalDataExporterManager');
230
        } catch (Exception $exception) {
231
            new ExceptionStackTraceDisplay($exception);
232
        }
233
    }
234
235
236
    /**
237
     * @throws Exception
238
     * @since $VID:$
239
     */
240
    public function handlePueRequest()
241
    {
242
        try {
243
            if (is_admin() && apply_filters('FHEE__EE_System__brew_espresso__load_pue', true)) {
244
                // pew pew pew
245
                $this->loader->getShared('EventEspresso\core\services\licensing\LicenseService');
246
                do_action('AHEE__EE_System__brew_espresso__after_pue_init');
247
            }
248
        } catch (Exception $exception) {
249
            new ExceptionStackTraceDisplay($exception);
250
        }
251
    }
252
253
254
    /**
255
     * @throws Exception
256
     * @since $VID:$
257
     */
258 View Code Duplication
    public function handleSessionRequest()
259
    {
260
        try {
261
            if (! $this->request->isAdmin() && ! $this->request->isEeAjax() && ! $this->request->isFrontend()) {
262
                return;
263
            }
264
            $this->loader->getShared('EE_Session');
265
        } catch (Exception $exception) {
266
            new ExceptionStackTraceDisplay($exception);
267
        }
268
    }
269
270
271
    /**
272
     * @throws Exception
273
     * @since $VID:$
274
     */
275 View Code Duplication
    public function handleShortcodesRequest()
276
    {
277
        try {
278
            if (! $this->request->isFrontend() && ! $this->request->isIframe() && ! $this->request->isAjax()) {
279
                return;
280
            }
281
            // load, register, and add shortcodes the new way
282
            $this->loader->getShared(
283
                'EventEspresso\core\services\shortcodes\ShortcodesManager',
284
                [
285
                    // and the old way, but we'll put it under control of the new system
286
                    EE_Config::getLegacyShortcodesManager(),
287
                ]
288
            );
289
        } catch (Exception $exception) {
290
            new ExceptionStackTraceDisplay($exception);
291
        }
292
    }
293
294
295
    /**
296
     * @throws Exception
297
     * @since $VID:$
298
     */
299
    public function handleWordPressHeartbeatRequest()
300
    {
301
        try {
302
            if (! $this->request->isWordPressHeartbeat()) {
303
                return;
304
            }
305
            $this->loader->getShared('EventEspresso\core\domain\services\admin\ajax\WordpressHeartbeat');
306
        } catch (Exception $exception) {
307
            new ExceptionStackTraceDisplay($exception);
308
        }
309
    }
310
311
312
    /**
313
     * @throws Exception
314
     * @since $VID:$
315
     */
316
    public function handleWordPressPluginsPage()
317
    {
318
        try {
319
            if (! $this->request->isAdmin()
320
                || ! $this->route_manager->routeMatchesCurrentRequest(
321
                    'EventEspresso\core\domain\entities\routing\specifications\admin\WordPressPluginsPage'
322
                )
323
            ) {
324
                return;
325
            }
326
            EE_Dependency_Map::register_dependencies(
327
                'EventEspresso\core\domain\services\assets\WordpressPluginsPageAssetManager',
328
                [
329
                    'EventEspresso\core\domain\Domain'                   => EE_Dependency_Map::load_from_cache,
330
                    'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache,
331
                    'EventEspresso\core\services\assets\Registry'        => EE_Dependency_Map::load_from_cache,
332
                    'EventEspresso\core\domain\services\admin\ExitModal' => EE_Dependency_Map::load_from_cache,
333
                ]
334
            );
335
            $this->loader->getShared('EventEspresso\core\domain\services\assets\WordpressPluginsPageAssetManager');
336
            /** @var PluginUpsells $plugin_upsells */
337
            $plugin_upsells = $this->loader->getShared('EventEspresso\core\domain\services\admin\PluginUpsells');
338
            $plugin_upsells->decafUpsells();
339
        } catch (Exception $exception) {
340
            new ExceptionStackTraceDisplay($exception);
341
        }
342
    }
343
}
344