Completed
Push — develop ( eb5f8c...2537a4 )
by
unknown
13:09
created

Module::onBootstrap()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 79
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 79
rs 8.4179
c 2
b 0
f 0
cc 6
eloc 47
nc 4
nop 1

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
 * YAWIK
4
 * Core Module Bootstrap
5
 *
6
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Core */
11
namespace Core;
12
13
use Zend\Mvc\MvcEvent;
14
use Core\Listener\LanguageRouteListener;
15
use Core\Listener\AjaxRenderListener;
16
use Core\Listener\LogListener;
17
use Core\Listener\EnforceJsonResponseListener;
18
use Core\Listener\StringListener;
19
use Zend\ModuleManager\Feature\ConsoleBannerProviderInterface;
20
use Zend\Console\Adapter\AdapterInterface as Console;
21
use Core\Listener\ErrorLoggerListener;
22
use Core\Listener\ErrorHandlerListener;
23
use Zend\Log\Formatter\ErrorHandler;
24
use Core\Repository\DoctrineMongoODM\PersistenceListener;
25
use Core\Listener\NotificationAjaxHandler;
26
use Core\Listener\Events\NotificationEvent;
27
use Doctrine\ODM\MongoDB\Types\Type as DoctrineType;
28
29
/**
30
 * Bootstrap class of the Core module
31
 *
32
 */
33
class Module implements ConsoleBannerProviderInterface
34
{
35
    
36
    public function getConsoleBanner(Console $console)
37
    {
38
        
39
        $version = `git describe`;
40
        $name = 'YAWIK ' . trim($version);
41
        $width = $console->getWidth();
42
        return sprintf(
43
            "==%1\$s==\n%2\$s%3\$s\n**%1\$s**\n",
44
            str_repeat('-', $width - 4),
45
            str_repeat(' ', floor(($width - strlen($name)) / 2)),
46
            $name
47
        );
48
    }
49
    
50
    /**
51
     * Sets up services on the bootstrap event.
52
     *
53
     * @internal
54
     *     Creates the translation service and a ModuleRouteListener
55
     *
56
     * @param MvcEvent $e
57
     */
58
    public function onBootstrap(MvcEvent $e)
59
    {
60
        // Register the TimezoneAwareDate type with DoctrineMongoODM
61
        // Use it in Annotions ( @Field(type="tz_date") )
62
        if (!DoctrineType::hasType('tz_date')) {
63
            DoctrineType::addType(
64
                'tz_date',
65
                '\Core\Repository\DoctrineMongoODM\Types\TimezoneAwareDate'
66
            );
67
        }
68
        
69
        $sm = $e->getApplication()->getServiceManager();
70
        $translator = $sm->get('translator'); // initialise translator!
71
        \Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
72
        $eventManager        = $e->getApplication()->getEventManager();
73
        $sharedManager       = $eventManager->getSharedManager();
74
        
75
 #       $LogListener = new LogListener();
76
 #       $LogListener->attach($eventManager);
77
        
78
        if (!\Zend\Console\Console::isConsole()) {
79
            $redirectCallback = function () use ($e) {
80
                $routeMatch = $e->getRouteMatch();
81
                $lang = $routeMatch ? $routeMatch->getParam('lang', 'en') : 'en';
82
                $uri    = $e->getRouter()->getBaseUrl() . '/' . $lang . '/error';
83
                
84
                header('Location: ' . $uri);
85
            };
86
            
87
            $errorHandlerListener = new ErrorHandlerListener($sm->get('ErrorLogger'), $redirectCallback);
88
            $errorHandlerListener->attach($eventManager);
89
            
90
            $languageRouteListener = new LanguageRouteListener();
91
            $languageRouteListener->attach($eventManager);
92
        
93
        
94
            $ajaxRenderListener = new AjaxRenderListener();
95
            $ajaxRenderListener->attach($eventManager);
96
        
97
            $enforceJsonResponseListener = new EnforceJsonResponseListener();
98
            $enforceJsonResponseListener->attach($eventManager);
99
        
100
            $stringListener = new StringListener();
101
            $stringListener->attach($eventManager);
102
103
        }
104
105
        $notificationListener = $sm->get('Core/Listener/Notification');
106
        $notificationListener->attachShared($sharedManager);
107
        $notificationAjaxHandler = new NotificationAjaxHandler();
108
        $eventManager->attach(MvcEvent::EVENT_DISPATCH, array($notificationAjaxHandler, 'injectView'), -20);
109
        $notificationListener->attach(NotificationEvent::EVENT_NOTIFICATION_HTML, array($notificationAjaxHandler, 'render'), -20);
110
        
111
        $persistenceListener = new PersistenceListener();
112
        $persistenceListener->attach($eventManager);
113
        
114
        $eventManager->attach(
115
            MvcEvent::EVENT_DISPATCH_ERROR,
116
            function ($event) {
117
                $application = $event->getApplication();
118
                if ($application::ERROR_EXCEPTION == $event->getError()) {
119
                    $ex = $event->getParam('exception');
120
                    if (404 == $ex->getCode()) {
121
                        $event->setError($application::ERROR_CONTROLLER_NOT_FOUND);
122
                    }
123
                }
124
            
125
            },
126
            500
127
        );
128
        $eventManager->attach(
129
            MvcEvent::EVENT_DISPATCH,
130
            function ($event) use ($eventManager) {
131
                $eventManager->trigger('postDispatch', $event);
132
            },
133
            -150
134
        );
135
        
136
    }
137
138
    /**
139
     * Loads module specific configuration.
140
     *
141
     * @return array
142
     */
143
    public function getConfig()
144
    {
145
        $config = include __DIR__ . '/config/module.config.php';
146
        return $config;
147
        if (\Zend\Console\Console::isConsole()) {
148
            $config['doctrine']['configuration']['odm_default']['generate_proxies'] = false;
149
            $config['doctrine']['configuration']['odm_default']['generate_hydrators'] = false;
150
            
151
        }
152
        return $config;
153
    }
154
155
    /**
156
     * Loads module specific autoloader configuration.
157
     *
158
     * @return array
159
     */
160
    public function getAutoloaderConfig()
161
    {
162
        return array(
163
            'Zend\Loader\StandardAutoloader' => array(
164
                'namespaces' => array(
165
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
166
                    'CoreTest' => __DIR__ . '/test/' . 'CoreTest'
167
                ),
168
            ),
169
        );
170
    }
171
}
172