Completed
Push — master ( 77abb4...5b1cd3 )
by Chris
02:37
created

ErrorHandlerService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 16 2
A register() 0 4 1
A boot() 0 4 1
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\View;
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 View\Resolver
20
	 */
21
	protected $view;
22
	
23
	/**
24
	 * Instantiate a new error handler service.
25
	 * 
26
	 * @param View\Resolver $view
27
	 */
28
	public function __construct(View\Resolver $view)
29
	{
30
		$this->view = $view;
31
	}
32
	
33
	/**
34
	 * Handle the given request and response in the case of a routing error.
35
	 * 
36
	 * @param Request  $request
37
	 * @param Response $response
38
	 * @return mixed
39
	 */
40
	public function handle(Request $request, Response $response)
41
	{
42
		$status = $response->status();
43
		
44
		$response->content("$status error.");
45
		
46
		if ($this->view->exists("errors/$status")) {
47
			$response->content($this->view->create("errors/$status", array(
48
				'http_host'   => $request->host(),
49
				'request_uri' => $request->path(),
50
				'signature'   => $request->server('server_signature')
51
			)));
52
		}
53
		
54
		return $response;
55
	}
56
	
57
	/**
58
	 * Register services with the container.
59
	 * 
60
	 * @param Container $container
61
	 */
62
	public function register(Container $container)
63
	{
64
		// No implementation needed.
65
	}
66
	
67
	/**
68
	 * Associate the error handler method with the router.
69
	 * 
70
	 * @param Router $router
71
	 */
72
	public function boot(Router $router)
73
	{
74
		$router->setErrorHandler(array($this, 'handle'));
75
	}
76
}
77