Passed
Push — master ( eece30...2edd63 )
by Radu
02:12
created

App::haltHttp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace WebServCo\Framework;
3
4
/*
5
An example App implementation.
6
Can be copied or extended by projects.
7
Add a log when an error occurs.
8
*/
9
class App extends Application
10
{
11
    /**
12
     * Handle CLI errors
13
     */
14
    protected function haltCli($errorInfo = [])
15
    {
16
        $this->logError($errorInfo, true);
17
        return parent::haltCli($errorInfo);
18
    }
19
20
    /**
21
     * Handle HTTP errors.
22
     */
23
    protected function haltHttp($errorInfo = [])
24
    {
25
        $this->logError($errorInfo, false);
26
        return parent::haltHttp($errorInfo);
27
    }
28
29
    protected function logError($errorInfo, $isCli = false)
30
    {
31
        $logger = new \WebServCo\Framework\Log\FileLogger(
32
            sprintf('error%s', $isCli ? 'CLI' : ''),
33
            $this->config()->get('app/path/log'),
34
            $this->request()
35
        );
36
        $errorMessage = sprintf('Error: %s in %s:%s', $errorInfo['message'], $errorInfo['file'], $errorInfo['line']);
37
        if ($errorInfo['exception'] instanceof \Exception) {
38
            $previous = $errorInfo['exception']->getPrevious();
39
            if ($previous instanceof \Exception) {
40
                do {
41
                    $errorMessage .= sprintf(
42
                        '%sPrevious: %s in %s:%s',
43
                        PHP_EOL,
44
                        $previous->getMessage(),
45
                        $previous->getFile(),
46
                        $previous->getLine()
47
                    );
48
                } while ($previous = $previous->getPrevious());
49
            }
50
        }
51
        $logger->error($errorMessage, $errorInfo);
52
    }
53
}
54