Completed
Push — master ( 5eb8de...a6b875 )
by Tobias
14:06
created

HttpMessageMarkupExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 42
ccs 2
cts 13
cp 0.1538
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 6 1
A markup() 0 19 3
A getName() 0 4 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector\Twig;
4
5
/**
6
 * @author Tobias Nyholm <[email protected]>
7
 */
8
class HttpMessageMarkupExtension extends \Twig_Extension
9
{
10
    /**
11
     * {@inheritdoc}
12
     *
13
     * @return array
14
     */
15 1
    public function getFilters()
16
    {
17
        return [
18 1
            new \Twig_SimpleFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]),
19
        ];
20
    }
21
22
    /**
23
     * @param string $message http message
24
     */
25
    public function markup($message)
26
    {
27
        $safeMessage = htmlentities($message);
28
        $parts = preg_split('|\\r?\\n\\r?\\n|', $safeMessage, 2);
29
30
        if (!isset($parts[1])) {
31
            // This is not a HTTP message
32
            return $safeMessage;
33
        }
34
35
        if (empty($parts[1])) {
36
            $parts[1] = '(This message has no captured body)';
37
        }
38
39
        // make header names bold
40
        $headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $parts[0]);
41
42
        return sprintf("%s\n\n<div class='httplug-http-body httplug-hidden'>%s</div>", $headers, $parts[1]);
43
    }
44
45
    public function getName()
46
    {
47
        return 'httplug.message_markup';
48
    }
49
}
50