Completed
Branch v2.0.0 (addc15)
by Alexander
03:27
created

Application::run()   B

Complexity

Conditions 5
Paths 14

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
c 0
b 0
f 0
rs 8.1252
cc 5
nc 14
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Alexander Torosh <[email protected]>
4
 */
5
6
namespace Web;
7
8
use Core\Annotations\AnnotationsManager;
9
use Core\Assets\AssetsHelper;
10
use Core\Cache\ApcuCache;
11
use Core\Config\EnvironmentLoader;
12
use Dashboard\Module as DashboardModule;
13
use Front\Module as FrontModule;
14
use Phalcon\Debug;
15
use Phalcon\Di\FactoryDefault;
16
use Phalcon\Mvc\Application as PhalconApplication;
17
use Phalcon\Mvc\Dispatcher;
18
use Phalcon\Tag;
19
use Phalcon\Url;
20
use Web\Exceptions\AccessDeniedException;
21
22
class Application
23
{
24
    public function run()
25
    {
26
        // DI Container
27
        $container = new FactoryDefault();
28
29
        // Initialize app
30
        $app = new PhalconApplication($container);
31
32
        // Set serverCache service
33
        $container->setShared('serverCache', (new ApcuCache())->init());
34
35
        // Env configuration
36
        $configLoader = new EnvironmentLoader();
37
        $configLoader->setDI($container);
38
        $configLoader->load();
39
40
        if ('development' === getenv('APP_ENV')) {
41
            $debug = new Debug();
42
            $debug->listen();
43
        }
44
45
        // Create EventsManager Manager
46
        $webEventsManager = new EventsManager($container);
47
        $eventsManager = $webEventsManager->getEventsManager();
48
49
        // Save Events Manager to DI Container
50
        $container->setShared('eventsManager', $eventsManager);
51
52
        // Bind Events Manager to Application
53
        $app->setEventsManager($container->get('eventsManager'));
54
55
        // Router
56
        $webRouter = new Router($container, $eventsManager);
57
        $container->setShared('router', $webRouter->getRouter());
58
59
        // Register Web Modules
60
        $this->registerWebApplicationModules($app);
61
62
        // Annotations
63
        $annotationsManager = new AnnotationsManager($container);
64
        $container->setShared('annotations', $annotationsManager->getAnnotations());
65
66
        // ACL
67
        $aclManager = new AclManager($container, $eventsManager);
68
        $container->setShared('acl', $aclManager->getAcl());
69
70
        // Default View
71
        $webView = new View();
72
        $webView->setEventsManager($eventsManager);
73
        $container->setShared('view', $webView);
74
75
        // Url
76
        $url = new Url();
77
        $url->setBaseUri('/');
78
        $container->setShared('url', $url);
79
80
        // Assets Build Resolver
81
        $assetsHelper = new AssetsHelper($container);
82
        $container->setShared('assetsHelper', $assetsHelper);
83
84
        // @TODO Move it to another place
85
        // Tag
86
        Tag::setTitleSeparator(' - ');
87
        Tag::setTitle('Yona CMS');
88
89
        try {
90
            // Handle request
91
            $response = $app->handle($_SERVER['REQUEST_URI']);
92
            $response->send();
93
        } catch (AccessDeniedException $e) {
94
            // Access Denied
95
            $this->handleAccessDenied($app);
96
        } catch (Dispatcher\Exception $e) {
97
            // 404 Not Found
98
            $this->notFoundError($app);
99
        } catch (\Exception $e) {
100
            // 503 Server Error
101
            $this->serviceUnavailableError($app, $e);
102
        }
103
    }
104
105
    private function registerWebApplicationModules(PhalconApplication $app)
106
    {
107
        $app->registerModules([
108
            'front' => [
109
                'className' => FrontModule::class,
110
                'path' => __DIR__.'/../modules/front/src/Module.php',
111
            ],
112
            'dashboard' => [
113
                'className' => DashboardModule::class,
114
                'path' => __DIR__.'/../modules/dashboard/src/Module.php',
115
            ],
116
        ]);
117
    }
118
119
    private function handleAccessDenied(PhalconApplication $app)
120
    {
121
        $app->response
122
            ->redirect($app->url->get(
123
                ['for' => 'dashboardLogin'],
124
                ['redirect' => $app->request->getURI()]
125
            ))
126
            ->send()
127
        ;
128
    }
129
130
    private function notFoundError(PhalconApplication $app)
131
    {
132
        Tag::prependTitle('Page Not Found');
133
134
        $app->view->render('errors', '404-not-found');
135
        $app->response
136
            ->setStatusCode(404)
137
            ->send()
138
        ;
139
    }
140
141
    private function serviceUnavailableError(PhalconApplication $app, \Exception $e)
142
    {
143
        Tag::prependTitle('Service Unavailable');
144
145
        $app->view->render('errors', '503-service-unavailable', [
146
            'message' => $e->getMessage(),
147
        ]);
148
        $app->response
149
            ->setStatusCode(503)
150
            ->send()
151
        ;
152
    }
153
}
154