Completed
Push — master ( e2e30a...b18a5e )
by Konstantinos
04:00
created

AppKernel::handleRaw()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0186
Metric Value
dl 0
loc 33
ccs 17
cts 19
cp 0.8947
rs 8.5806
cc 4
eloc 19
nc 6
nop 3
crap 4.0186
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use BZIon\Cache\ModelCache;
4
use BZIon\Session\DatabaseSessionHandler;
5
use Symfony\Component\Config\Loader\LoaderInterface;
6
use Symfony\Component\Debug\Debug;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpFoundation\Session\Session;
10
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
11
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
12
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
13
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
15
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
16
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
17
use Symfony\Component\HttpKernel\Kernel;
18
use Symfony\Component\HttpKernel\KernelEvents;
19
20
require_once __DIR__ . '/../bzion-load.php';
21
22
class AppKernel extends Kernel
23
{
24
    private $request = null;
25
26 1
    public function registerContainerConfiguration(LoaderInterface $loader)
27
    {
28 1
        $loader->load(__DIR__ . '/Resource/symfony_' . $this->getEnvironment() . '.yml');
29 1
    }
30
31 1
    public function registerBundles()
32
    {
33
        $bundles = array(
34 1
            new BZIon\Config\ConfigBundle(),
35 1
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
36 1
            new Symfony\Bundle\MonologBundle\MonologBundle(),
37 1
            new Symfony\Bundle\TwigBundle\TwigBundle(),
38 1
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
39 1
            new Liip\ImagineBundle\LiipImagineBundle(),
40 1
            new FOS\ElasticaBundle\FOSElasticaBundle(),
41 1
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
42 1
            new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
43
        );
44
45 1
        if ($this->getEnvironment() == 'profile') {
46
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
47
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
48
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
49
        }
50
51 1
        return $bundles;
52
    }
53
54 1
    public function boot()
55
    {
56 1
        Service::setKernel($this);
57
58 1
        parent::boot();
59
60 1
        if (!$this->container->getParameter('bzion.miscellaneous.development')) {
61
            if ($this->getEnvironment() != 'prod' || $this->isDebug()) {
62
                throw new ForbiddenDeveloperAccessException(
63
                    'You are not allowed to access this page in a non-production ' .
64
                    'environment. Please change the "development" configuration ' .
65
                    'value and clear the cache before proceeding.'
66
                );
67
            }
68
        }
69
70 1
        if (in_array($this->getEnvironment(), array('profile', 'dev'), true)) {
71
            Debug::enable();
72
        }
73
74 1
        Service::setGenerator($this->container->get('router')->getGenerator());
75 1
        Service::setEnvironment($this->getEnvironment());
76 1
        Service::setModelCache(new ModelCache());
77
78
        // Ratchet doesn't support PHP's native session storage, so use our own
79
        // if we need it
80 1
        if (Service::getParameter('bzion.features.websocket.enabled') &&
81 1
            $this->getEnvironment() !== 'test') {
82
            $storage = new NativeSessionStorage(array(), new DatabaseSessionHandler());
83
            $session = new Session($storage);
84
            Service::getContainer()->set('session', $session);
85
        }
86
87 1
        Notification::initializeAdapters();
88 1
    }
89
90
    /**
91
     * Find out whether the `dev` or the `profile` environment should be used
92
     * for development, depending on the existance of the profiler bundle
93
     *
94
     * @return string The suggested kernel environment
95
     */
96
    public static function guessDevEnvironment()
97
    {
98
        // If there is a profiler, use the environment with the profiler
99
        if (class_exists('Symfony\Bundle\WebProfilerBundle\WebProfilerBundle')) {
100
            return 'profile';
101
        }
102
103
        return 'dev';
104
    }
105
106 1
    public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
107
    {
108 1
        if (false === $this->booted) {
109 1
            $this->boot();
110
        }
111
112 1
        if ($catch && !$this->isDebug()) {
113
            try {
114 1
                return $this->handleRaw($request, $type, $catch);
115 1
            } catch (Exception $e) {
116 1
                return $this->handleException($e, $request, $type);
117
            }
118
        } else {
119 1
            return $this->handleRaw($request, $type, $catch);
120
        }
121
    }
122
123 1
    private function handleRaw(Request $request, $type = self::MASTER_REQUEST, $catch = true)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
124
    {
125 1
        $this->container->enterScope('request');
126 1
        $this->container->set('request', $request, 'request');
127 1
        $this->container->get('request_stack')->push($request);
128
129 1
        if ($type === self::MASTER_REQUEST) {
130 1
            $this->request = $request;
131
        }
132
133 1
        Service::setRequest($request);
134
135 1
        $event = new GetResponseEvent($this, $request, $type);
136 1
        $this->container->get('event_dispatcher')->dispatch(KernelEvents::REQUEST, $event);
137
138 1
        if ($request->attributes->get('_defaultHandler')) {
139
            return parent::handle($request, $type, $catch);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (handle() instead of handleRaw()). Are you sure this is correct? If so, you might want to change this to $this->handle().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
140
        }
141
142
        // An event may have given a response
143 1
        if ($event->hasResponse()) {
144
            return $this->filterResponse($event->getResponse(), $request, $type);
145
        }
146
147 1
        $session = $this->container->get('session');
148 1
        $session->start();
149 1
        Service::setFormFactory($this->container->get('form.factory'));
150
151 1
        $con = Controller::getController($request->attributes);
152 1
        $response = $con->callAction();
153
154 1
        return $this->filterResponse($response, $request, $type);
155
    }
156
157
    /**
158
     * Filters a response object.
159
     *
160
     * @param Response $response A Response instance
161
     * @param Request  $request  An error message in case the response is not a Response object
162
     * @param int      $type     The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
163
     *
164
     * @return Response The filtered Response instance
165
     */
166 1
    private function filterResponse(Response $response, Request $request, $type)
167
    {
168 1
        $event = new FilterResponseEvent($this, $request, $type, $response);
169 1
        $this->container->get('event_dispatcher')->dispatch(KernelEvents::RESPONSE, $event);
170
171 1
        $requestEvent = new FinishRequestEvent($this, $request, $type);
172 1
        $this->container->get('event_dispatcher')->dispatch(KernelEvents::FINISH_REQUEST, $requestEvent);
173
174 1
        return $event->getResponse();
175
    }
176
177 1
    public function terminate(Request $request, Response $response)
178
    {
179 1
        $this->container->get('event_dispatcher')->dispatch(
180 1
            KernelEvents::TERMINATE,
181 1
            new PostResponseEvent($this, $request, $response)
182
        );
183 1
    }
184
185
    public function terminateWithException(Exception $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        return false;
188
    }
189
190 1
    private function handleException(Exception $e, $request, $type)
191
    {
192 1
        $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
193 1
        $this->container->get('event_dispatcher')->dispatch(KernelEvents::EXCEPTION, $event);
194
195
        // a listener might have replaced the exception
196 1
        $e = $event->getException();
197 1
        if (!$event->hasResponse()) {
198
            throw $e;
199
        }
200
201 1
        $response = $event->getResponse();
202
203 1
        if ($response->headers->has('X-Status-Code')) {
204
            // the developer asked for a specific status code
205
            $response->setStatusCode($response->headers->get('X-Status-Code'));
206
            $response->headers->remove('X-Status-Code');
207 1
        } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
208
            // ensure that we actually have an error response
209 1
            if ($e instanceof HttpExceptionInterface) {
210
                // keep the HTTP status code and headers
211 1
                $response->setStatusCode($e->getStatusCode());
212 1
                $response->headers->add($e->getHeaders());
213
            } else {
214
                $response->setStatusCode(500);
215
            }
216
        }
217
218 1
        return $response;
219
    }
220
}
221