TwigViewEncoder::encode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Vectorface\SnappyRouter\Encoder;
4
5
use \Twig_Loader_Filesystem;
6
use \Twig_Environment;
7
use Vectorface\SnappyRouter\Exception\InternalErrorException;
8
use Vectorface\SnappyRouter\Handler\ControllerHandler;
9
use Vectorface\SnappyRouter\Response\AbstractResponse;
10
11
/**
12
 * An encoder for rendering input through a Twig view.
13
 * @copyright Copyright (c) 2014, VectorFace, Inc.
14
 * @author Dan Bruce <[email protected]>
15
 */
16
class TwigViewEncoder extends AbstractEncoder
17
{
18
19
    // the template to encode
20
    private $template;
21
22
    // the twig view environment
23
    private $viewEnvironment;
24
25
    /**
26
     * Constructor for the encoder.
27
     * @param array $viewConfig The view configuration.
28
     * @param string $template The name of the default template to render
29
     */
30 8
    public function __construct($viewConfig, $template)
31
    {
32 8
        if (!isset($viewConfig[ControllerHandler::KEY_VIEWS_PATH])) {
33 1
            throw new InternalErrorException(
34 1
                'View environment missing views path.'
35
            );
36
        }
37 7
        $loader = new Twig_Loader_Filesystem(
38 7
            $viewConfig[ControllerHandler::KEY_VIEWS_PATH]
39
        );
40 7
        $this->viewEnvironment = new Twig_Environment($loader, $viewConfig);
41 7
        $this->template = $template;
42 7
    }
43
44
    /**
45
     * Returns the Twig view environment.
46
     * @return Twig_Environment The configured twig environment.
47
     */
48 1
    public function getViewEnvironment()
49
    {
50 1
        return $this->viewEnvironment;
51
    }
52
53
    /**
54
     * @param AbstractResponse $response The response to be encoded.
55
     * @return string Returns the response encoded as a string.
56
     */
57 6
    public function encode(AbstractResponse $response)
58
    {
59 6
        $responseObject = $response->getResponseObject();
60 6
        if (is_string($responseObject)) {
61 4
            return $responseObject;
62
        } else {
63 2
            return $this->viewEnvironment->loadTemplate(
64 2
                $this->template
65 2
            )->render(
66 2
                (array)$responseObject
67
            );
68
        }
69
    }
70
71
    /**
72
     * Renders an abitrary view with arbitrary parameters.
73
     * @param string $template The template to render.
74
     * @param array $variables The variables to use.
75
     * @return string Returns the rendered template as a string.
76
     */
77 1
    public function renderView($template, $variables)
78
    {
79 1
        return $this->getViewEnvironment()->loadTemplate(
80 1
            $template
81 1
        )->render(
82 1
            (array)$variables
83
        );
84
    }
85
}
86