Completed
Push — master ( 3176c6...202b1b )
by Carsten
08:08 queued 10s
created

TwigRequestHandler::setContextAttributeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Germania\TwigRequestHandler;
3
4
use Psr\Http\Message\ServerRequestInterface;
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ResponseFactoryInterface;
7
use Psr\Http\Server\RequestHandlerInterface;
8
use Twig\Environment as TwigEnvironment;
9
10
class TwigRequestHandler implements RequestHandlerInterface
11
{
12
13
    /**
14
     * @var ResponseFactoryInterface
15
     */
16
    public $response_factory;
17
18
19
    /**
20
     * @var TwigEnvironment|null
21
     */
22
    public $twig;
23
24
25
    /**
26
     * Default response status code
27
     * @var integer
28
     */
29
    public $response_status_code = 200;
30
31
32
    /**
33
     * Default response content type
34
     * @var integer
35
     */
36
    public $response_content_type = "text/html";
37
38
39
    /**
40
     * Request attribute name for the Twig template
41
     * @var string
42
     */
43
    public $template_attribute_name = 'template';
44
45
46
    /**
47
     * Request attribute name for the Twig template context variables
48
     * @var string
49
     */
50
    public $context_attribute_name = 'context';
51
52
53
    /**
54
     * @param TwigEnvironment          $twig             Twig Environment
55
     * @param ResponseFactoryInterface $response_factory PSR-17 Response Factory
56
     */
57 2
    public function __construct(TwigEnvironment $twig, ResponseFactoryInterface $response_factory)
58
    {
59 2
        $this->setTwig($twig);
60 2
        $this->setResponseFactory($response_factory);
61 2
    }
62
63
64
65
    /**
66
     * @inheritDoc
67
     */
68 14
    public function handle(ServerRequestInterface $request) : ResponseInterface
69
    {
70 14
        $context = $request->getAttribute($this->context_attribute_name, null);
71
72 14
        if ($context instanceOf \ArrayObject) {
73
            $context = $context->getArrayCopy();
74
        }
75
76 14
        if (!is_array($context)) {
77 6
            $msg = sprintf(
78 6
                "Expected Request attribute '%s' to be array, got '%s'.",
79 6
                $this->context_attribute_name,
80 6
                gettype($context)
81
            );
82 6
            throw new \RuntimeException($msg);
83
        }
84
85 8
        $template = $request->getAttribute($this->template_attribute_name, null);
86 8
        if (!is_string($template) or empty($template)) {
87 6
            $msg = sprintf(
88 6
                "Expected Request attribute '%s' to be non-empty string, got '%s'.",
89 6
                $this->template_attribute_name,
90 6
                gettype($template)
91
            );
92 6
            throw new \RuntimeException($msg);
93
        }
94
95
96 2
        $html = $this->twig->render($template, $context);
97
98 2
        $response = $this->response_factory
99 2
                    ->createResponse($this->response_status_code)
100 2
                    ->withHeader('Content-Type', $this->response_content_type);
101
102 2
        $response->getBody()->write($html);
103
104 2
        return $response;
105
    }
106
107
108
109
    /**
110
     * Sets the Twig Environment.
111
     *
112
     * @param TwigEnvironment $twig
113
     */
114 18
    public function setTwig(TwigEnvironment $twig) : self
115
    {
116 18
        $this->twig = $twig;
117 18
        return $this;
118
    }
119
120
121
    /**
122
     * Sets the Response Factory.
123
     *
124
     * @param ResponseFactoryInterface $response_factory PSR-17 ResponseFactory
125
     */
126 4
    public function setResponseFactory(ResponseFactoryInterface $response_factory) : self
127
    {
128 4
        $this->response_factory = $response_factory;
129 4
        return $this;
130
    }
131
132
133
    /**
134
     * Sets the Status code for generated response.
135
     *
136
     * @param int $response_status_code HTTP Status Code
137
     */
138 4
    public function setResponseStatusCode(int $response_status_code) : self
139
    {
140 4
        $this->response_status_code = $response_status_code;
141 4
        return $this;
142
    }
143
144
145
    /**
146
     * Sets the content-type for generated response.
147
     *
148
     * @param string $response_content_type Response Content-type
149
     */
150 4
    public function setResponseContentType(string $response_content_type) : self
151
    {
152 4
        $this->response_content_type = $response_content_type;
0 ignored issues
show
Documentation Bug introduced by
The property $response_content_type was declared of type integer, but $response_content_type is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
153 4
        return $this;
154
    }
155
156
157
    /**
158
     * Sets the request attribute that carries the template.
159
     *
160
     * @param string $attr Request attribute name
161
     */
162 16
    public function setTemplateAttributeName(string $attr) : self
163
    {
164 16
        $this->template_attribute_name = $attr;
165 16
        return $this;
166
    }
167
168
169
    /**
170
     * Sets the request attribute that carries the context array.
171
     *
172
     * @param string $attr Request attribute name
173
     */
174 16
    public function setContextAttributeName(string $attr) : self
175
    {
176 16
        $this->context_attribute_name = $attr;
177 16
        return $this;
178
    }
179
}
180