Completed
Pull Request — master (#84)
by Tobias
09:11
created

HttpMessageMarkupExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
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
 * 
7
 *
8
 * @author Tobias Nyholm <[email protected]>
9
 */
10
class HttpMessageMarkupExtension extends \Twig_Extension
11
{
12
    /**
13
     * {@inheritdoc}
14
     *
15
     * @return array
16
     */
17
    public function getFilters()
18
    {
19
        return array(
20
            new \Twig_SimpleFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]),
21
        );
22
    }
23
24
    /**
25
     * @param string $message http message
26
     */
27
    public function markup($message)
28
    {
29
        $safeMessage = htmlentities($message);
30
        list($headers, $body) = preg_split('|\\r?\\n\\r?\\n|', $safeMessage, 2);
31
32
        // make header names bold
33
        $headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $headers);
34
        
35
        return sprintf("%s\n\n<div class='httplug-http-body'>%s</div>", $headers, $body);
36
    }
37
38
    public function getName()
39
    {
40
        return 'httplug.message_markup';
41
    }
42
}