TraceAttachment::getArrayValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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