Completed
Push — master ( 6acfe1...0042b2 )
by Radu
11:51 queued 10s
created

App   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 4
b 0
f 0
dl 0
loc 43
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework;
6
7
/*
8
An example App implementation.
9
Can be copied or extended by projects.
10
Custom functionality in this class: add a log when an error occurs.
11
*/
12
class App extends Application
13
{
14
15
    /**
16
     * Handle CLI errors
17
     *
18
     * @param array<string,mixed> $errorInfo
19
     */
20
    protected function haltCli(array $errorInfo = []): bool
21
    {
22
        $this->logError($errorInfo, true);
23
        return parent::haltCli($errorInfo);
24
    }
25
26
    /**
27
     * Handle HTTP errors.
28
<<<<<<< HEAD
29
     */
30
    protected function haltHttp($errorInfo = [])
31
=======
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_IS_IDENTICAL, expecting '{' or ';' on line 31 at column 0
Loading history...
32
     *
33
     * @param array<string,mixed> $errorInfo
34
     */
35
    protected function haltHttp(array $errorInfo = []): bool
36
>>>>>>> v10
37
    {
38
        $this->logError($errorInfo, false);
39
        return parent::haltHttp($errorInfo);
40
    }
41
42
    /**
43
    * @param array<string,mixed> $errorInfo
44
    */
45
    protected function logError(array $errorInfo, bool $isCli = false): void
46
    {
47
        $logger = new \WebServCo\Framework\Log\FileLogger(
48
            \sprintf('error%s', $isCli ? 'CLI' : ''),
49
            $this->config()->get('app/path/log'),
50
        );
51
        $errorMessage = \sprintf('Error: %s in %s:%s', $errorInfo['message'], $errorInfo['file'], $errorInfo['line']);
52
        if ($errorInfo['exception'] instanceof \Exception) {
53
            $previous = $errorInfo['exception']->getPrevious();
54
            if ($previous instanceof \Exception) {
55
                do {
56
                    $errorMessage .= \sprintf(
57
                        '%sPrevious: %s in %s:%s',
58
                        \PHP_EOL,
59
                        $previous->getMessage(),
60
                        $previous->getFile(),
61
                        $previous->getLine(),
62
                    );
63
                // phpcs:ignore SlevomatCodingStandard.ControlStructures.AssignmentInCondition.AssignmentInCondition
64
                } while ($previous = $previous->getPrevious());
65
            }
66
        }
67
        $logger->error($errorMessage, $errorInfo);
68
    }
69
}
70