Test Failed
Push — master ( 4c92de...3b4c18 )
by Sinnarasa
04:16
created

TemplateDispatcher::call()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 13
rs 9.2
c 1
b 1
f 0
cc 4
eloc 10
nc 5
nop 0
1
<?php
2
3
namespace JetFire\Routing\Dispatcher;
4
5
use JetFire\Routing\Router;
6
7
/**
8
 * Class TemplateDispatcher
9
 * @package JetFire\Routing\Dispatcher
10
 */
11
class TemplateDispatcher implements DispatcherInterface
12
{
13
14
    /**
15
     * @var Router
16
     */
17
    private $router;
18
19
    /**
20
     * @var array
21
     */
22
    protected $types = [
23
        'json' => 'application/json',
24
        'xml' => 'application/xml',
25
        'txt' => 'text/plain',
26
        'html' => 'text/html'
27
    ];
28
29
    /**
30
     * @param Router $router
31
     */
32
    public function __construct(Router $router)
33
    {
34
        $this->router = $router;
35
    }
36
37
    /**
38
     * @description call template file
39
     */
40
    public function call()
41
    {
42
        $this->setContentType($this->router->route->getTarget('extension'));
43
        if (isset($this->router->route->getTarget('callback')[$this->router->route->getTarget('extension')])) {
44
            $this->router->response->setContent(call_user_func_array($this->router->route->getTarget('callback')[$this->router->route->getTarget('extension')], [$this->router->route]));
45
        } else {
46
            ob_start();
47
            if (isset($this->router->route->getTarget()['data'])) extract($this->router->route->getTarget('data'));
0 ignored issues
show
Bug introduced by
$this->router->route->getTarget('data') cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
48
            if (isset($this->router->route->getParams()['data'])) extract($this->router->route->getParams()['data']);
49
            require($this->router->route->getTarget('template'));
50
            $this->router->response->setContent(ob_get_clean());
51
        }
52
    }
53
54
    /**
55
     * @param $extension
56
     */
57
    public function setContentType($extension)
58
    {
59
        isset($this->types[$extension])
60
            ? $this->router->response->setHeaders(['Content-Type' => $this->types[$extension]])
61
            : $this->router->response->setHeaders(['Content-Type' => $this->types['html']]);
62
    }
63
64
}
65