Completed
Push — master ( 8af507...d2cd75 )
by Konstantinos
07:28
created

AppKernel::registerContainerConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function registerContainerConfiguration(LoaderInterface $loader)
27
    {
28
        $loader->load(__DIR__ . '/Resource/symfony_' . $this->getEnvironment() . '.yml');
29
    }
30
31
    public function registerBundles()
32
    {
33
        $bundles = array(
34
            new BZIon\Config\ConfigBundle(),
35
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
36
            new Symfony\Bundle\MonologBundle\MonologBundle(),
37
            new Symfony\Bundle\TwigBundle\TwigBundle(),
38
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
39
            new Liip\ImagineBundle\LiipImagineBundle(),
40
            new FOS\ElasticaBundle\FOSElasticaBundle(),
41
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
42 1
            new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
43
        );
44 1
45 1
        if ($this->getEnvironment() == 'profile') {
46
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
47 1
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
48
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
49
        }
50 1
51 1
        return $bundles;
52 1
    }
53 1
54 1
    public function boot()
55 1
    {
56 1
        Service::setKernel($this);
57 1
58 1
        parent::boot();
59
60
        if (!$this->container->getParameter('bzion.miscellaneous.development')) {
61 1
            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 1
            }
68
        }
69
70 1
        if (in_array($this->getEnvironment(), array('profile', 'dev'), true)) {
71
            Debug::enable();
72 1
        }
73
74 1
        Service::setGenerator($this->container->get('router')->getGenerator());
75
        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
        if (Service::getParameter('bzion.features.websocket.enabled') &&
81
            $this->getEnvironment() !== 'test') {
82
            $storage = new NativeSessionStorage(array(), new DatabaseSessionHandler());
83
            $session = new Session($storage);
84
            Service::getContainer()->set('session', $session);
85
        }
86 1
87
        Notification::initializeAdapters();
88
    }
89
90 1
    /**
91 1
     * Find out whether the `dev` or the `profile` environment should be used
92 1
     * for development, depending on the existance of the profiler bundle
93 1
     *
94
     * @return string The suggested kernel environment
95
     */
96
    public static function guessDevEnvironment()
97 1
    {
98 1
        // 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 1
    }
105 1
106
    public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
107
    {
108
        if (false === $this->booted) {
109
            $this->boot();
110
        }
111
112
        if ($catch && !$this->isDebug()) {
113
            try {
114
                return $this->handleRaw($request, $type, $catch);
115
            } catch (Exception $e) {
116
                return $this->handleException($e, $request, $type);
117
            }
118
        } else {
119
            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
        $this->container->set('request', $request, 'request');
127
        $this->container->get('request_stack')->push($request);
128 1
129
        if ($type === self::MASTER_REQUEST) {
130 1
            $this->request = $request;
131 1
        }
132 1
133
        Service::setRequest($request);
134
135
        $event = new GetResponseEvent($this, $request, $type);
136 1
        $this->container->get('event_dispatcher')->dispatch(KernelEvents::REQUEST, $event);
137 1
138 1
        if ($request->attributes->get('_defaultHandler')) {
139 1
            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 1
        }
141
142 1
        // An event may have given a response
143 1
        if ($event->hasResponse()) {
144
            return $this->filterResponse($event->getResponse(), $request, $type);
145 1
        }
146 1
147
        $session = $this->container->get('session');
148 1
        $session->start();
149 1
        Service::setFormFactory($this->container->get('form.factory'));
150
151
        $con = Controller::getController($request->attributes);
152 1
        $response = $con->callAction();
153
154
        return $this->filterResponse($response, $request, $type);
155
    }
156 1
157 1
    /**
158 1
     * Filters a response object.
159 1
     *
160 1
     * @param Response $response A Response instance
161 1
     * @param Request  $request  An error message in case the response is not a Response object
162 1
     * @param int      $type     The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
163 1
     *
164 1
     * @return Response The filtered Response instance
165
     */
166
    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
        $this->container->get('event_dispatcher')->dispatch(KernelEvents::FINISH_REQUEST, $requestEvent);
173 1
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
    }
184 1
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 1
    }
189
190 1
    private function handleException(Exception $e, $request, $type)
191 1
    {
192 1
        $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
193
        $this->container->get('event_dispatcher')->dispatch(KernelEvents::EXCEPTION, $event);
194 1
195 1
        // a listener might have replaced the exception
196
        $e = $event->getException();
197
        if (!$event->hasResponse()) {
198 1
            throw $e;
199
        }
200 1
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
        } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
208 1
            // ensure that we actually have an error response
209
            if ($e instanceof HttpExceptionInterface) {
210
                // keep the HTTP status code and headers
211
                $response->setStatusCode($e->getStatusCode());
212 1
                $response->headers->add($e->getHeaders());
213 1
            } else {
214 1
                $response->setStatusCode(500);
215
            }
216 1
        }
217 1
218
        return $response;
219 1
    }
220
}
221