Completed
Push — master ( 6f3d30...b7621c )
by jelmer
02:34
created

ParametersAttachment::parseArrayParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 1
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 parameters of the error.
10
 *
11
 * @author Jelmer Prins <[email protected]>
12
 *
13
 * @since 0.4.1
14
 */
15
class ParametersAttachment 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
     * @param ErrorInterface $error
31
     * @param StringFormat $formatter
32
     */
33 6
    public function __construct(ErrorInterface $error, StringFormat $formatter)
34
    {
35 6
        parent::__construct('Parameters');
36 6
        $this->error = $error;
37 6
        $this->formatter = $formatter;
38
39 6
        $this->setTitle(new Title('Parameters'));
40
41 6
        foreach ((array) $this->error->getParameters() as $name => $parameter) {
42 6
            $this->addField(new Field($name, $this->parseParameter($parameter)));
43 6
        }
44 6
    }
45
46
    /**
47
     * Parse all the data into the markup of slack.
48
     *
49
     * @param mixed $parameter
50
     *
51
     * @return string
52
     */
53 6
    private function parseParameter($parameter)
54
    {
55 6
        if (!is_array($parameter)) {
56
            return print_r($parameter, true);
57
        }
58
59 6
        return $this->parseArrayParameter($parameter);
60
    }
61
62
    /**
63
     * @param array $parameter
64
     *
65
     * @return string
66
     */
67 6
    private function parseArrayParameter(array $parameter)
68
    {
69 6
        return "\n" . $this->formatter->arrayToKeyValueList(
70 6
            array_map(
71 6
                function ($item) {
72 6
                    return print_r($item, true);
73 6
                },
74
                $parameter
75 6
            )
76 6
        );
77
    }
78
}
79