TemplateDispatcher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 View Code Duplication
        if (!is_file($this->router->route->getTarget('template'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            throw new \Exception('Template file not found : "' . $this->router->route->getTarget('template') . '"');
44
        }
45
        $this->setContentType($this->router->route->getTarget('extension'));
46
        if (isset($this->router->route->getTarget('callback')[$this->router->route->getTarget('extension')])) {
47
            $this->router->response->setContent(call_user_func_array($this->router->route->getTarget('callback')[$this->router->route->getTarget('extension')], [$this->router->route]));
48
        } else {
49
            ob_start();
50
            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...
51
            if (isset($this->router->route->getParams()['data'])) extract($this->router->route->getParams()['data']);
52
            require($this->router->route->getTarget('template'));
53
            $this->router->response->setContent(ob_get_clean());
54
        }
55
    }
56
57
    /**
58
     * @param $extension
59
     */
60
    public function setContentType($extension)
61
    {
62
        isset($this->types[$extension])
63
            ? $this->router->response->setHeaders(['Content-Type' => $this->types[$extension]])
64
            : $this->router->response->setHeaders(['Content-Type' => $this->types['html']]);
65
    }
66
67
}
68