Application::getHttpOutput()   B
last analyzed

Complexity

Conditions 8
Paths 3

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 57
rs 8.1635
cc 8
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework;
6
7
/**
8
* An example Application implementation.
9
*
10
* Can be replaced or extended by consumer implementations.
11
* Custom functionality in this class: use custom output.
12
*/
13
class Application extends AbstractApplication
14
{
15
    /**
16
    * CLI message to output in case of error.
17
    */
18
    protected function getCliOutput(\Throwable $throwable): string
19
    {
20
        $output = 'Boo boo' . \PHP_EOL;
21
        $output .= \WebServCo\Framework\Helpers\ErrorMessageHelper::format($throwable);
22
        $output .= \PHP_EOL;
23
        return $output;
24
    }
25
26
    /**
27
    * HTML code to output in case of error.
28
    */
29
    protected function getHttpOutput(\Throwable $throwable): string
30
    {
31
        $output = '<!doctype html>
32
            <html>
33
            <head>
34
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
35
                <title>Oops</title>
36
                <style>
37
                * {background: #f2dede; color: #a94442; overflow-wrap: break-word;}
38
                .i {margin-left: auto; margin-right: auto; text-align: center; width: auto;}
39
                small {font-size: 0.8em;}
40
                </style>
41
            </head>
42
            <body><div class="i"><br>';
43
        $output .= \sprintf('<h1>%s</h1>', 404 === $throwable->getCode() ? 'Resource not found' : 'Boo boo');
44
45
        if (
46
            \WebServCo\Framework\Values\Environment::DEVELOPMENT
47
            === \WebServCo\Framework\Environment\Config::string('APP_ENVIRONMENT')
48
        ) {
49
            $output .= \sprintf(
50
                '<p><i>%s</i></p><p>%s:%s</p>',
51
                $throwable->getMessage(),
52
                $throwable->getFile(),
53
                $throwable->getLine(),
54
            );
55
            $trace = $throwable->getTrace();
56
            if (!empty($trace)) {
57
                $output .= "<p>";
58
                $output .= "<small>";
59
                foreach ($trace as $item) {
60
                    if (!empty($item['class'])) {
61
                        $output .= \sprintf('%s%s', $item['class'], $item['type']);
62
                        $output .= "";
63
                    }
64
                    if (!empty($item['function'])) {
65
                        $output .= \sprintf('%s', $item['function']);
66
                        $output .= "";
67
                    }
68
                    if (!empty($item['file'])) {
69
                        $output .= \sprintf(
70
                            ' [%s:%s]',
71
                            \basename($item['file']),
72
                            $item['line'],
73
                        );
74
                        $output .= " ";
75
                    }
76
                    $output .= "<br>";
77
                }
78
                $output .= "</small></p>";
79
            }
80
        }
81
82
        $output .= '</div></body>';
83
        $output .= '</html>';
84
85
        return $output;
86
    }
87
88
    /**
89
    * Get Response.
90
    */
91
    protected function getResponse(): \WebServCo\Framework\Interfaces\ResponseInterface
92
    {
93
        return $this->execute();
94
    }
95
}
96