Completed
Push — master ( 0ff821...f6e4b7 )
by Sinnarasa
04:40
created

TemplateDispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 9
Bugs 2 Features 3
Metric Value
wmc 8
c 9
b 2
f 3
lcom 1
cbo 2
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B call() 0 14 5
A setContentType() 0 6 2
1
<?php
2
3
namespace JetFire\Routing\Dispatcher;
4
5
6
use JetFire\Routing\ResponseInterface;
7
use JetFire\Routing\Route;
8
9
/**
10
 * Class TemplateDispatcher
11
 * @package JetFire\Routing\Dispatcher
12
 */
13
class TemplateDispatcher implements DispatcherInterface
14
{
15
16
    /**
17
     * @var Route
18
     */
19
    private $route;
20
21
    /**
22
     * @var ResponseInterface
23
     */
24
    private $response;
25
26
    /**
27
     * @var array
28
     */
29
    protected $types = [
30
        'json' => 'application/json',
31
        'xml' => 'application/xml',
32
        'txt' => 'text/plain',
33
        'html' => 'text/html'
34
    ];
35
36
    /**
37
     * @param Route $route
38
     */
39
    public function __construct(Route $route,ResponseInterface $response)
40
    {
41
        $this->route = $route;
42
        $this->response = $response;
43
    }
44
45
    /**
46
     * @description call template file
47
     */
48
    public function call()
49
    {
50
        if ($this->response->getStatusCode() == 202)
51
            $this->setContentType($this->route->getTarget('extension'));
52
        if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')]))
53
            $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route]));
54
        else {
55
            ob_start();
56
            if(isset($this->route->getTarget()['data']))extract($this->route->getTarget('data'));
0 ignored issues
show
Bug introduced by
$this->route->getTarget('data') cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
57
            if(isset($this->route->getParams()['data']))extract($this->route->getParams()['data']);
0 ignored issues
show
Documentation Bug introduced by
The method getParams does not exist on object<JetFire\Routing\Route>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
58
            require($this->route->getTarget('template'));
59
            $this->response->setContent(ob_get_clean());
60
        }
61
    }
62
63
    /**
64
     * @param $extension
65
     */
66
    public function setContentType($extension){
67
        $this->response->setStatusCode(200);
68
        isset($this->types[$extension])
69
            ? $this->response->setHeaders(['Content-Type' => $this->types[$extension]])
70
            : $this->response->setHeaders(['Content-Type' => $this->types['html']]);
71
    }
72
73
}
74