Completed
Push — master ( 028a2f...d99482 )
by Andrew
02:00
created

src/AppWrapper.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace PHPFastCGI\Adapter\Slim;
4
5
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
6
use PHPFastCGI\FastCGIDaemon\KernelInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Slim\App;
10
use Slim\Container;
11
use Slim\Exception\Exception as SlimException;
12
use Slim\Http\Headers;
13
use Slim\Http\Response;
14
15
/**
16
 * Wraps a Slim v3 application object as an implementation of the kernel
17
 * interface.
18
 */
19
class AppWrapper implements KernelInterface
20
{
21
    /**
22
     * @var App
23
     */
24
    private $app;
25
26
    /**
27
     * @var Container
28
     */
29
    private $container;
30
31
    /**
32
     * Constructor.
33
     * 
34
     * @param App $app The Slim v3 application object to wrap
35
     */
36
    public function __construct(App $app)
37
    {
38
        $this->app       = $app;
39
        $this->container = $app->getContainer();
40
41
        $this->container->get('router')->finalize();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function handleRequest(RequestInterface $request)
48
    {
49
        $serverRequest = $request->getServerRequest();
50
51
        $headers  =  new Headers(['Content-Type' => 'text/html']);
52
        $response = (new Response(200, $headers))->withProtocolVersion('1.1');
53
54
        try {
55
            $response = $this->app->callMiddlewareStack($serverRequest, $response);
56
        } catch (SlimException $exception) {
0 ignored issues
show
The class Slim\Exception\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
57
            $response = $exception->getResponse();
58
        } catch (\Exception $exception) {
59
            $errorHandler = $this->container->get('errorHandler');
60
            $response = $errorHandler($serverRequest, $response, $exception);
61
        }
62
63
        return $this->finalizeResponse($response);
64
    }
65
66
    /**
67
     * Finalizes the applications response, similar to Slim\App::respond()
68
     * 
69
     * @param ResponseInterface $response
70
     * 
71
     * @return ResponseInterface
72
     */
73
    private function finalizeResponse(ResponseInterface $response)
74
    {
75
        $statusCode = $response->getStatusCode();
76
77
        if ($statusCode !== 204 && $statusCode !== 304) {
78
            $bodySize = $response->getBody()->getSize();
79
80
            if (null !== $bodySize) {
81
                $response = $response->withHeader('Content-Length', (string) $bodySize);
82
            }
83
        } else {
84
            $response = $response->withoutHeader('Content-Type')->withoutHeader('Content-Length');
85
        }
86
87
        return $response;
88
    }
89
}
90