Issues (1019)

src/Illuminate/LogTrait.php (17 issues)

1
<?php
2
0 ignored issues
show
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Illuminate;
4
5
use Hhxsv5\LaravelS\LaravelS;
6
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
7
8
trait LogTrait
0 ignored issues
show
Missing doc comment for trait LogTrait
Loading history...
9
{
10
    public function logException(\Exception $e)
0 ignored issues
show
Missing doc comment for function logException()
Loading history...
11
    {
12
        $this->log(
13
            sprintf(
14
                'Uncaught exception \'%s\': [%d]%s called in %s:%d%s%s',
15
                get_class($e),
16
                $e->getCode(),
17
                $e->getMessage(),
18
                $e->getFile(),
19
                $e->getLine(),
20
                PHP_EOL,
21
                $e->getTraceAsString()
22
            ),
23
            'ERROR'
24
        );
25
    }
26
27
    public function log($msg, $type = 'INFO')
0 ignored issues
show
Missing doc comment for function log()
Loading history...
28
    {
29
        $outputStyle = LaravelS::getOutputStyle();
30
        $msg = sprintf('[%s] [%s] %s', date('Y-m-d H:i:s'), $type, $msg);
31
        if ($outputStyle) {
0 ignored issues
show
$outputStyle is of type Symfony\Component\Console\Style\OutputStyle, thus it always evaluated to true.
Loading history...
32
            switch (strtoupper($type)) {
33
                case 'INFO':
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
34
                    $outputStyle->writeln("<info>{$msg}</info>");
35
                    break;
36
                case 'WARNING':
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
37
                    if (!$outputStyle->getFormatter()->hasStyle('warning')) {
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
38
                        $style = new OutputFormatterStyle('yellow');
39
                        $outputStyle->getFormatter()->setStyle('warning', $style);
40
                    }
0 ignored issues
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
41
                    $outputStyle->writeln("<warning>{$msg}</warning>");
42
                    break;
43
                case 'ERROR':
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
44
                    $outputStyle->writeln("<error>{$msg}</error>");
45
                    break;
46
                case 'TRACE':
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
47
                default:
0 ignored issues
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
48
                    $outputStyle->writeln($msg);
49
                    break;
50
            }
51
        } else {
52
            echo $msg, PHP_EOL;
53
        }
54
    }
55
56
    public function trace($msg)
0 ignored issues
show
Missing doc comment for function trace()
Loading history...
57
    {
58
        $this->log($msg, 'TRACE');
59
    }
60
61
    public function info($msg)
0 ignored issues
show
Missing doc comment for function info()
Loading history...
62
    {
63
        $this->log($msg, 'INFO');
64
    }
65
66
    public function warning($msg)
0 ignored issues
show
Missing doc comment for function warning()
Loading history...
67
    {
68
        $this->log($msg, 'WARNING');
69
    }
70
71
    public function error($msg)
0 ignored issues
show
Missing doc comment for function error()
Loading history...
72
    {
73
        $this->log($msg, 'ERROR');
74
    }
75
76
    public function callWithCatchException(callable $callback, array $args = [], $tries = 1)
0 ignored issues
show
Missing doc comment for function callWithCatchException()
Loading history...
77
    {
78
        $try = 0;
79
        do {
80
            $try++;
81
            try {
82
                return call_user_func_array($callback, $args);
83
            } catch (\Exception $e) {
84
                $this->logException($e);
85
            }
86
        } while ($try < $tries);
87
        return null;
88
    }
89
}