Passed
Push — master ( 685651...ff2960 )
by Atanas
02:52
created

HasRoutesTrait::group()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 2
dl 0
loc 3
c 0
b 0
f 0
cc 1
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Routing;
11
12
use Closure;
13
use WPEmerge\Controllers\WordPressController;
14
15
/**
16
 * Allow objects to have routes
17
 */
18
trait HasRoutesTrait {
19
	/**
20
	 * Array of registered routes
21
	 *
22
	 * @var RouteInterface[]
23
	 */
24
	protected $routes = [];
25
26
	/**
27
	 * Create a route instance.
28
	 *
29
	 * @param  string[]       $methods
30
	 * @param  mixed          $condition
31
	 * @param  string|Closure $handler
32
	 * @return RouteInterface
33
	 */
34
	public abstract function makeRoute( $methods, $condition, $handler );
35
36
	/**
37
	 * Get registered routes
38
	 *
39
	 * @return RouteInterface[]
40
	 */
41 1
	public function getRoutes() {
42 1
		return $this->routes;
43
	}
44
45
	/**
46
	 * Add a route
47
	 *
48
	 * @param  RouteInterface $route
49
	 * @return RouteInterface
50
	 */
51 1
	public function addRoute( $route ) {
52 1
		$this->routes[] = $route;
53 1
		return $route;
54
	}
55
56
	/**
57
	 * Create and add a new route
58
	 *
59
	 * @param  string[] $methods
60
	 * @param  mixed $condition
61
	 * @param  string|Closure|null $handler
62
	 * @return RouteInterface
63
	 * @throws \WPEmerge\Exceptions\Exception
64
	 */
65 2
	public function route( $methods, $condition, $handler = null ) {
66 2
		if ( $handler === null ) {
67 1
			$handler = WordPressController::class . '@handle';
68 1
		}
69
70 2
		$route = $this->makeRoute( $methods, $condition, $handler );
71
72 2
		return $this->addRoute( $route );
73
	}
74
75
	/**
76
	 * Create and add a route for the GET and HEAD methods
77
	 *
78
	 * @param  mixed               $condition
79
	 * @param  string|Closure|null $handler
80
	 * @return RouteInterface
81
	 */
82 1
	public function get( $condition, $handler = null ) {
83 1
		return $this->route( ['GET', 'HEAD'], $condition, $handler );
84
	}
85
86
	/**
87
	 * Create and add a route for the POST method
88
	 *
89
	 * @param  mixed               $condition
90
	 * @param  string|Closure|null $handler
91
	 * @return RouteInterface
92
	 */
93 1
	public function post( $condition, $handler = null ) {
94 1
		return $this->route( ['POST'], $condition, $handler );
95
	}
96
97
	/**
98
	 * Create and add a route for the PUT method
99
	 *
100
	 * @param  mixed               $condition
101
	 * @param  string|Closure|null $handler
102
	 * @return RouteInterface
103
	 */
104 1
	public function put( $condition, $handler = null ) {
105 1
		return $this->route( ['PUT'], $condition, $handler );
106
	}
107
108
	/**
109
	 * Create and add a route for the PATCH method
110
	 *
111
	 * @param  mixed               $condition
112
	 * @param  string|Closure|null $handler
113
	 * @return RouteInterface
114
	 */
115 1
	public function patch( $condition, $handler = null ) {
116 1
		return $this->route( ['PATCH'], $condition, $handler );
117
	}
118
119
	/**
120
	 * Create and add a route for the DELETE method
121
	 *
122
	 * @param  mixed               $condition
123
	 * @param  string|Closure|null $handler
124
	 * @return RouteInterface
125
	 */
126 1
	public function delete( $condition, $handler = null ) {
127 1
		return $this->route( ['DELETE'], $condition, $handler );
128
	}
129
130
	/**
131
	 * Create and add a route for the OPTIONS method
132
	 *
133
	 * @param  mixed               $condition
134
	 * @param  string|Closure|null $handler
135
	 * @return RouteInterface
136
	 */
137 1
	public function options( $condition, $handler = null ) {
138 1
		return $this->route( ['OPTIONS'], $condition, $handler );
139
	}
140
141
	/**
142
	 * Create and add a route for all supported methods
143
	 *
144
	 * @param  mixed               $condition
145
	 * @param  string|Closure|null $handler
146
	 * @return RouteInterface
147
	 */
148 1
	public function any( $condition, $handler = null ) {
149 1
		return $this->route( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $condition, $handler );
150
	}
151
}
152