Completed
Push — master ( 45609c...0607d7 )
by Pavel
02:11
created

Starter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 11
nc 2
nop 4
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2016 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\ApiDocu;
10
11
use Nette;
12
use Nette\Application\IRouter;
13
use Nette\Application\Request;
14
use Nette\Http;
15
use Ublaboo\ApiRouter\ApiRoute;
16
use Nette\Application\Routers\RouteList;
17
18
class Starter extends Nette\Object
19
{
20
21
	const API_DOCU_STARTER_QUERY_KEY_TARGET   = '__apiDocu';
22
	const API_DOCU_STARTER_QUERY_KEY_GENERATE = '__apiDocuGenerate';
23
24
	/**
25
	 * @var Generator
26
	 */
27
	private $generator;
28
29
	/**
30
	 * @var IRouter
31
	 */
32
	private $router;
33
34
	/**
35
	 * @var Http\Response
36
	 */
37
	private $response;
38
39
	/**
40
	 * @var Http\Request
41
	 */
42
	private $httpRequest;
43
44
45
	/**
46
	 * @param Generator     $generator
47
	 * @param IRouter       $router
48
	 * @param Http\Response $response
49
	 * @param Http\Request  $httpRequest
50
	 */
51
	public function __construct(
52
		Generator $generator,
53
		IRouter $router,
54
		Http\Response $response,
55
		Http\Request $httpRequest
56
	) {
57
		$this->generator = $generator;
58
		$this->router = $router;
59
60
		$this->response = $response;
61
		$this->httpRequest = $httpRequest;
62
63
		if ($router instanceof RouteList) {
64
			$this->attachEvents();
65
		}
66
	}
67
68
69
	/**
70
	 * Event thatis firex when particular ApiRoute is matched
71
	 * @param  ApiRoute $route
72
	 * @param  Request  $request
73
	 * @return void
74
	 */
75
	public function routeMatched(ApiRoute $route, Request $request)
76
	{
77
		if (NULL !== ($format = $request->getParameter(self::API_DOCU_STARTER_QUERY_KEY_GENERATE))) {
78
			$this->generator->generateAll($this->router);
79
80
			exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method routeMatched() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
81
		}
82
83
		if (NULL !== ($format = $request->getParameter(self::API_DOCU_STARTER_QUERY_KEY_TARGET))) {
84
			$this->generator->generateTarget($route, $request);
85
86
			exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method routeMatched() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
87
		}
88
	}
89
90
91
	/**
92
	 * Find ApiRoutes and add listener to each ApiRoute::onMatch event
93
	 * @return void
94
	 */
95
	protected function attachEvents()
96
	{
97
		foreach ($this->router as $route) {
0 ignored issues
show
Bug introduced by
The expression $this->router of type object<Nette\Application\IRouter> is not traversable.
Loading history...
98
			if ($route instanceof ApiRoute) {
99
				$route->onMatch[] = [$this, 'routeMatched'];
100
			}
101
		}
102
	}
103
104
}
105