Completed
Push — master ( f4b826...4d0daa )
by Chris
04:52 queued 02:17
created

ErrorHandlerService::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
namespace Darya\Foundation\Providers;
3
4
use Darya\Http\Request;
5
use Darya\Http\Response;
6
use Darya\Routing\Router;
7
use Darya\Service\Contracts\Container;
8
use Darya\Service\Contracts\Provider;
9
use Darya\Smarty\ViewResolver;
10
11
/**
12
 * A service provider that provides its own method as a routing error handler.
13
 * 
14
 * @author Chris Andrew <[email protected]>
15
 */
16
class ErrorHandlerService implements Provider
17
{
18
    /**
19
     * @var ViewResolver
20
     */
21
    protected $view;
22
    
23
    public function __construct(ViewResolver $view)
24
    {
25
        $this->view = $view;
26
    }
27
    
28
    public function handle(Request $request, Response $response)
29
    {
30
        $status = $response->status();
31
        
32
        if ($this->view->exists("error/$status")) {
33
			$response->content($this->view->create("error/$status", array(
34
				'http_host' => $request->host(),
35
				'request_uri' => $request->path(),
36
				'signature' => $request->server('server_signature')
37
			)));
38
        } else {
39
        	$response->content("$status error.");
40
        }
41
        
42
        return $response;
43
    }
44
    
45
    public function register(Container $container)
46
    {
47
    }
48
    
49
    public function boot(Router $router)
50
    {
51
        $router->setErrorHandler(array($this, 'handle'));
52
    }
53
}
54