Completed
Push — master ( 71f716...1a0423 )
by jelmer
02:36
created

TraceAttachment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack\Attachment;
4
5
use JsonSerializable;
6
use Monolog\Logger;
7
use Pageon\SlackWebhookMonolog\General\Url;
8
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface;
9
use Pageon\SlackWebhookMonolog\Slack\StringFormat;
10
11
/**
12
 * An attachment containing basic info about the error.
13
 *
14
 * @author Jelmer Prins <[email protected]>
15
 *
16
 * @since 0.4.0
17
 */
18
class TraceAttachment extends Attachment
19
{
20
    /**
21
     * Extra error data.
22
     *
23
     * @var ErrorInterface
24
     */
25
    private $error;
26
27
    /**
28
     * @var StringFormat
29
     */
30
    private $formatter;
31
32
    /**
33
     * TraceAttachment constructor.
34
     *
35
     * @param ErrorInterface $error
36
     * @param StringFormat $formatter
37
     */
38 6
    public function __construct(ErrorInterface $error, StringFormat $formatter)
39
    {
40 6
        $this->error = $error;
41 6
        $this->formatter = $formatter;
42
43 6
        $this->setTitle(new Title('Trace'));
44 6
        $this->setText(
45 6
            $formatter->arrayToNumberedList(
46 6
                array_map(
47
                    function ($traceItem) {
48 6
                        return $this->parseTraceItem($traceItem);
49 6
                    },
50 6
                    $this->error->getTrace()
51 6
                )
52 6
            )
53 6
        );
54 6
    }
55
56 6
    private function parseTraceItem(array $traceItem)
57
    {
58 6
        $text = '';
59
        $info = [
60 6
            'file' => isset($traceItem['file']) ? $traceItem['file'] : 'unknown',
61 6
            'function' => isset($traceItem['function']) ? $traceItem['function'] : 'unknown',
62 6
            'line' => isset($traceItem['line']) ? $traceItem['line'] : 'unknown',
63 6
            'class' => isset($traceItem['class']) ? $traceItem['class'] : 'unknown',
64 6
            'type' => isset($traceItem['type']) ? $traceItem['type'] : 'unknown',
65 6
            'arguments' => isset($traceItem['args']) ?
66 6
                "\n" . $this->formatter->arrayToKeyValueList(
67 6
                    array_map(
68
                        function ($item) {
69 6
                            return print_r($item, true);
70 6
                        },
71 6
                        array_filter(
72 6
                            $traceItem['args'],
73 6
                            function ($item) {
74 6
                                return !empty($item);
75
                            }
76 6
                        )
77 6
                    )
78 6
                ) : 'unknown',
79 6
        ];
80 6
        $text .= $this->formatter->arrayToKeyValueList($info);
81
82 6
        return "\n" . $this->formatter->indent($text);
83
    }
84
}
85