Issues (10)

src/Controller/ErrorController.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tebe\Pvc\Controller;
6
7
use Tebe\Pvc\Exception\SystemException;
8
use Throwable;
9
10
class ErrorController extends BaseController
11
{
12
    /**
13
     * @var Throwable
14
     */
15
    private $error;
16
17
    /**
18
     * @param Throwable $error
19
     * @return ErrorController
20
     */
21
    public function setError(Throwable $error)
22
    {
23
        $this->error = $error;
24
        return $this;
25
    }
26
27
    /**
28
     * @return Throwable
29
     */
30
    public function getError()
31
    {
32
        return $this->error;
33
    }
34
35
    /**
36
     * @return string
37
     * @throws SystemException
38
     */
39
    public function errorAction()
40
    {
41
        $acceptHeaders = $this->getRequest()->getHeaderLine('Accept');
42
43
        // json output
44
        if (strpos($acceptHeaders, 'application/json') !== false) {
45
            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('code' => $...or->getTraceAsString()) returns the type array<string,integer|string> which is incompatible with the documented return type string.
Loading history...
46
                'code' => $this->error->getCode(),
47
                'file' => $this->error->getFile(),
48
                'line' => $this->error->getLine(),
49
                'message' => $this->error->getMessage(),
50
                'trace' => $this->error->getTraceAsString(),
51
            ];
52
        }
53
54
        // user view file
55
        $viewName = $this->getViewName($this->error->getCode());
56
        if (!empty($viewName)) {
57
            return $this->render($viewName, [
58
                'error' => $this->error
59
            ]);
60
        }
61
        return
62
            $this->error->getMessage()
63
            . '<br>'
64
            . $this->error->getTraceAsString();
65
    }
66
67
    /**
68
     * @param int $status
69
     * @return string
70
     */
71
    private function getViewName(int $status): string
72
    {
73
        $viewNames = ['error/' . $status, 'error/error'];
74
        foreach ($viewNames as $viewName) {
75
            if ($this->getView()->fileExist($viewName)) {
76
                return $viewName;
77
            }
78
        }
79
        return '';
80
    }
81
}
82