Completed
Push — master ( 7a1429...168b0d )
by Chris
02:39
created

ErrorHandlerService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 38
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 16 2
A register() 0 3 1
A boot() 0 4 1
1
<?php
2
namespace Darya\Service\Provider;
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