Completed
Push — fetcher_factories ( 10a56f...fd93ea )
by David
13:44
created

HtmlResponse   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 6
c 8
b 0
f 0
lcom 1
cbo 3
dl 0
loc 72
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 4 1
A setHtmlElement() 0 4 1
A getHtmlElement() 0 4 1
A getBody() 0 12 2
1
<?php
2
3
namespace Mouf\Mvc\Splash;
4
5
use Mouf\Html\HtmlElement\HtmlElementInterface;
6
use Zend\Diactoros\Response;
7
use Zend\Diactoros\Stream;
8
9
/**
10
 * This class is a Symfony 2 response that takes in parameter a HtmlElementInterface element and will render it.
11
 *
12
 * @author David Négrier <[email protected]>
13
 */
14
class HtmlResponse extends Response
15
{
16
    /**
17
     * @var HtmlElementInterface
18
     */
19
    protected $htmlElement;
20
21
    /**
22
     * @var Stream
23
     */
24
    protected $stream;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param HtmlElementInterface $htmlElement An HtmlElement to render.
30
     * @param int                  $status      The response status code
31
     * @param array                $headers     An array of response headers
32
     */
33
    public function __construct(HtmlElementInterface $htmlElement, $status = 200, $headers = array())
34
    {
35
        parent::__construct('php://temp', $status, $headers);
36
37
        $this->htmlElement = $htmlElement;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public static function create(HtmlElementInterface $htmlElement, $status = 200, $headers = array())
44
    {
45
        return new static($htmlElement, $status, $headers);
46
    }
47
48
    /**
49
     * Sets the HtmlElement to be rendered.
50
     *
51
     * @param HtmlElementInterface $htmlElement
52
     */
53
    public function setHtmlElement(HtmlElementInterface $htmlElement)
54
    {
55
        $this->htmlElement = $htmlElement;
56
    }
57
58
    /**
59
     * Returns the HtmlElement to be rendered.
60
     *
61
     * @return \Mouf\Html\HtmlElement\HtmlElementInterface
62
     */
63
    public function getHtmlElement()
64
    {
65
        return $this->htmlElement;
66
    }
67
68
    /**
69
     * Gets the body of the message.
70
     *
71
     * @return StreamInterface Returns the body as a stream.
72
     */
73
    public function getBody()
74
    {
75
        if ($this->stream === null) {
76
            ob_start();
77
            $this->htmlElement->toHtml();
78
            $content = ob_get_clean();
79
            $this->stream = new Stream('php://memory', 'wb+');
80
            $this->stream->write($content);
81
        }
82
83
        return $this->stream;
84
    }
85
}
86