Completed
Push — master ( ea8fd7...b1104c )
by Oleg
03:59
created

MvcApplication::doException()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 3
nop 1
1
<?php
2
3
namespace Micro\Mvc;
4
5
use Micro\Base\Application;
6
use Micro\Base\Injector;
7
use Micro\base\ResolverInterface;
8
use Micro\Web\ResponseInjector;
9
use Psr\Http\Message\ResponseInterface;
10
11
// всё что нужно для MVC
12
class MvcApplication extends Application
13
{
14
    /**
15
     * @return ResolverInterface
16
     */
17
    public function getResolver()
18
    {
19
        return new MvcResolver;
20
    }
21
22
    /**
23
     * @param ResponseInterface $response
24
     */
25
    public function send(ResponseInterface $response)
26
    {
27
        header('HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
28
29
        foreach ($response->getHeaders() as $header => $values) {
30
            header($header . ': ' . implode(', ', $values));
31
        }
32
33
        parent::send($response);
34
    }
35
36
    /**
37
     * Do exception
38
     *
39
     * @access private
40
     *
41
     * @param \Exception $e Exception
42
     *
43
     * @return ResponseInterface
44
     * @throws \Micro\Base\Exception
45
     */
46
    protected function doException(\Exception $e)
0 ignored issues
show
Coding Style introduced by
doException uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
47
    {
48
        $output = (new ResponseInjector)->build();
49
50
        $errorController = (new Injector)->param('errorController');
51
        $errorAction = (new Injector)->param('errorAction');
52
53
        if (!$errorController || !$errorAction) { // render SAPI error not configured
54
            $stream = $output->getBody();
55
            $stream->write('Option `errorController` or `errorAction` not configured');
56
57
            return $output->withBody($stream);
58
        }
59
60
        // Render SAPI error
61
        $_POST['error'] = $e;
62
63
        $controller = $errorController;
64
65
        /** @var \Micro\Mvc\Controllers\IController $result */
66
        $result = new $controller(false);
67
        $result = $result->action($errorAction);
68
69
        if ($result instanceof ResponseInterface) {
70
            return $result;
71
        }
72
73
        $stream = $output->getBody();
74
        $stream->write((string)$result);
75
76
        return $output->withBody($stream);
77
    }
78
}