Completed
Pull Request — master (#84)
by Tobias
26:04 queued 16:05
created

HttpMessageMarkupExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 3
c 2
b 1
f 1
lcom 0
cbo 2
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 6 1
A markup() 0 10 1
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
    public function getFilters()
16
    {
17
        return [
18
            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
        list($headers, $body) = preg_split('|\\r?\\n\\r?\\n|', $safeMessage, 2);
29
30
        // make header names bold
31
        $headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $headers);
32
33
        return sprintf("%s\n\n<div class='httplug-http-body'>%s</div>", $headers, $body);
34
    }
35
36
    public function getName()
37
    {
38
        return 'httplug.message_markup';
39
    }
40
}
41