Completed
Push — master ( 607ba7...62e01b )
by Radu
05:38
created

App::haltCli()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace ParcelValue\ApiClient;
3
4
5
use WebServCo\Framework\Log\FileLogger;
6
7
final class App extends \WebServCo\Framework\Application
8
{
9
    public function __construct($pathPublic, $pathProject = null)
10
    {
11
        /**
12
         * Project can be located in a completely different place
13
         * than the web directory.
14
         */
15
        $pathProject = $pathProject ?: realpath($pathPublic . '/..');
16
17
        parent::__construct($pathPublic, $pathProject, __NAMESPACE__);
18
19
        $this->config()->set('app/path/log', sprintf('%svar/log/', $this->projectPath));
20
    }
21
22
    /**
23
     * Handle HTTP errors.
24
     */
25
    protected function haltHttp($errorInfo = [])
26
    {
27
        $this->logError($errorInfo, false);
28
        return parent::haltHttp($errorInfo);
29
    }
30
31
    /**
32
     * Handle CLI errors
33
     */
34
    protected function haltCli($errorInfo = [])
35
    {
36
        $this->logError($errorInfo, true);
37
        return parent::haltCli($errorInfo);
38
    }
39
40
    protected function logError($errorInfo, $isCli = false)
41
    {
42
        $logger = new FileLogger(
43
            sprintf('error%s', $isCli ? 'CLI' : ''),
44
            $this->config()->get('app/path/log'),
45
            $this->request()
46
        );
47
        $errorMessage = sprintf('Error: %s in %s:%s', $errorInfo['message'], $errorInfo['file'], $errorInfo['line']);
48
        if ($errorInfo['exception'] instanceof \Exception) {
49
            $previous = $errorInfo['exception']->getPrevious();
50
            if ($previous instanceof \Exception) {
51
                do {
52
                    $errorMessage .= sprintf(
53
                        '%sPrevious: %s in %s:%s',
54
                        PHP_EOL,
55
                        $previous->getMessage(),
56
                        $previous->getFile(),
57
                        $previous->getLine()
58
                    );
59
                } while ($previous = $previous->getPrevious());
60
            }
61
        }
62
        $logger->error($errorMessage, []);
63
    }
64
}
65