Failed Conditions
Pull Request — master (#7)
by Sergey
02:55
created

Twig   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 93.48%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 10
dl 0
loc 167
ccs 43
cts 46
cp 0.9348
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
getRequest() 0 1 ?
getResponse() 0 1 ?
A setViewVariable() 0 8 2
A setViewFunction() 0 12 3
A setViewExtension() 0 6 1
A view() 0 13 2
A getTwig() 0 21 4
A getTwigLoader() 0 4 1
A getTwigEnvironment() 0 4 1
A createTwigFunction() 0 6 3
A createTwigFilter() 0 6 3
1
<?php
2
3
namespace Jasny\Controller\View;
4
5
use Jasny\Flash;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * View using Twig
11
 */
12
trait Twig
13
{
14
    /**
15
     * Twig environment
16
     * @var \Twig_Environment
17
     */
18
    protected $twig = null;
19
20
    /**
21
     * Get server request
22
     * @return ServerRequestInterface
23
     */
24
    abstract public function getRequest();
25
26
    /**
27
     * Get server response
28
     * @return ResponseInterface
29
     */
30
    abstract public function getResponse();
31
32
    /**
33
     * Add a global variable to the view.
34
     * 
35
     * @param string $name   Variable name
36
     * @param mixed  $value
37
     * @return $this
38
     */
39 3
    public function setViewVariable($name, $value)
40
    {
41 3
        if (!$name) throw new \InvalidArgumentException("Name should not be empty");        
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
42
43 2
        $this->getTwig()->addGlobal($name, $value);
44
45 2
        return $this;
46
    }
47
    
48
    /**
49
     * Expose a function to the view.
50
     * 
51
     * @param string $name       Variable name
52
     * @param mixed  $function
53
     * @param string $as        'function' or 'filter'
54
     * @return $this
55
     */
56 3
    public function setViewFunction($name, $function = null, $as = 'function')
57
    {
58 3
        if ($as === 'function') {
59 1
            $this->getTwig()->addFunction($this->createTwigFunction($name, $function));
60 3
        } elseif ($as === 'filter') {
61 1
            $this->getTwig()->addFilter($this->createTwigFilter($name, $function));
62 1
        } else {
63 1
            throw new \InvalidArgumentException("You should create either function or filter, not '$as'");
64
        }
65
        
66 2
        return $this;
67
    }
68
69
    /**
70
     * Add extension to view
71
     *
72
     * @param object $extension
73
     * @return $this
74
     */
75 1
    public function setViewExtension($extension)
76
    {
77 1
        $this->getTwig()->addExtension($extension);
78
79 1
        return $this;
80
    }
81
82
    /**
83
     * View rendered template
84
     *
85
     * @param string $name   Template name
86
     * @param array $context Template context
87
     * @return ResponseInterface
88
     */
89 2
    public function view($name, array $context = [])
90
    {
91 2
        if (!pathinfo($name, PATHINFO_EXTENSION)) $name .= '.html.twig';
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
92
93 2
        $twig = $this->getTwig();
94 2
        $tmpl = $twig->loadTemplate($name);
95
96 2
        $response = $this->getResponse();
97 2
        $response = $response->withHeader('Content-Type', 'text/html; charset=' . $twig->getCharset());
98 2
        $response->getBody()->write($tmpl->render($context));            
99
100 2
        return $response;
101
    }
102
103
    /**
104
     * Get twig environment
105
     *
106
     * @return \Twig_Environment
107
     */
108 2
    public function getTwig()
109
    {
110 1
        if ($this->twig) return $this->twig;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
111
112 1
        $loader = $this->getTwigLoader();
113 1
        $this->twig = $this->getTwigEnvironment($loader);
114
115 1
        $extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension'];
116 1
        foreach ($extensions as $name) {
117 1
            $class = "Jasny\Twig\\$name";
118
119 1
            if (class_exists($class)) $this->setViewExtension(new $class());
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
120 1
        }
121
        
122 2
        $uri = $this->getRequest()->getUri()->getPath();
123
        
124 2
        $this->setViewVariable('current_url', $uri);
125 1
        $this->setViewVariable('flash', new Flash());
126
127 1
        return $this->twig;
128
    }
129
130
    /**
131
     * Get twig loasder for current working directory
132
     *
133
     * @return \Twig_Loader_Filesystem
134
     */
135 1
    public function getTwigLoader()
136
    {
137 1
        return new \Twig_Loader_Filesystem(getcwd());
138
    }
139
140
    /**
141
     * Get twig environment instance
142
     *
143
     * @param \Twig_Loader_Filesystem $loader
144
     * @return \Twig_Environment
145
     */
146 1
    public function getTwigEnvironment(\Twig_Loader_Filesystem $loader)
147
    {
148 1
        return new \Twig_Environment($loader);
149
    }
150
151
    /**
152
     * Create twig function
153
     *
154
     * @param string $name          Name of function in view
155
     * @param callable|null $function 
156
     * @return \Twig_SimpleFunction
157
     */
158 6
    public function createTwigFunction($name, $function)
159
    {
160 6
        if (!$name) throw new \InvalidArgumentException("Function name should not be empty");
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
161
162 4
        return new \Twig_SimpleFunction($name, $function ?: $name);
163
    }
164
165
    /**
166
     * Create twig filter
167
     *
168
     * @param string $name          Name of filter in view
169
     * @param callable|null $function 
170
     * @return \Twig_SimpleFilter
171
     */
172
    public function createTwigFilter($name, $function)
173
    {
174
        if (!$name) throw new \InvalidArgumentException("Filter name should not be empty");
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
175
176
        return new \Twig_SimpleFilter($name, $function ?: $name);
177
    }
178
}
179