Route_Manager   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 106
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A from_route() 0 7 1
A create_base_route_from_group() 0 13 3
A execute() 0 2 1
A unpack_group() 0 27 5
A __construct() 0 3 1
A from_group() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Handle the parsing and registering of route with WordPress.
7
 *
8
 * @package PinkCrab\Route\Route
9
 * @author Glynn Quelch [email protected]
10
 * @since 0.0.1
11
 */
12
13
namespace PinkCrab\Route\Registration;
14
15
use PinkCrab\Route\Route\Route;
16
use PinkCrab\Loader\Hook_Loader;
17
use PinkCrab\Route\Route_Exception;
18
use PinkCrab\Route\Route\Route_Group;
19
use PinkCrab\Route\Registration\WP_Rest_Registrar;
20
21
class Route_Manager {
22
23
	protected Hook_Loader $loader;
24
	protected WP_Rest_Registrar $registrar;
25
26
	public function __construct( WP_Rest_Registrar $registrar, Hook_Loader $hook_loader ) {
27
		$this->loader    = $hook_loader;
28
		$this->registrar = $registrar;
29
	}
30
31
	/**
32
	 * Registers a WP Rest Route from a passed Route model.
33
	 *
34
	 * @param Route $route
35
	 * @return self
36
	 */
37
	public function from_route( Route $route ): self {
38
		$this->loader->action(
39
			'rest_api_init',
40
			$this->registrar->create_callback( $route )
41
		);
42
43
		return $this;
44
	}
45
46
	/**
47
	 * Registers all routes from a group.
48
	 *
49
	 * @param \PinkCrab\Route\Route\Route_Group $group
50
	 * @return self
51
	 */
52
	public function from_group( Route_Group $group ): self {
53
		foreach ( $this->unpack_group( $group ) as $route ) {
54
			$this->from_route( $route );
55
		}
56
57
		return $this;
58
	}
59
60
	/**
61
	 * Unpacks a group into its routes with all shared (Auth & Argument) properties from the group
62
	 *
63
	 * @param \PinkCrab\Route\Route\Route_Group $group
64
	 * @return Route[]
65
	 * @throws Route_Exception code 102
66
	 */
67
	protected function unpack_group( Route_Group $group ): array {
68
69
		$routes = array();
70
		// Loop through each group
71
		foreach ( $group->get_rest_routes() as $method => $route ) {
72
			$populated_route = $this->create_base_route_from_group( $method, $group );
73
74
			// Replace args if set.
75
			foreach ( $route->get_arguments() as $key => $argument ) {
76
				$populated_route->argument( $argument );
77
			}
78
79
			// Extends any group based authentication with a route based.
80
			foreach ( $route->get_authentication() as $key => $auth_callback ) {
81
				$populated_route->authentication( $auth_callback );
82
			}
83
84
			// If we have no callback defined for route, throw.
85
			if ( is_null( $route->get_callback() ) ) {
86
				throw Route_Exception::callback_not_defined( $route );
87
			}
88
89
			$populated_route->callback( $route->get_callback() );
90
91
			$routes[ $method ] = $populated_route;
92
		}
93
		return $routes;
94
	}
95
96
	/**
97
	 * Generates a base route from a group for a defined method.
98
	 * Populates the groups namespace, authentication and arguemnts
99
	 * as inital values on the route.
100
	 *
101
	 * @param string $method
102
	 * @param Route_Group $group
103
	 * @return Route
104
	 */
105
	protected function create_base_route_from_group( string $method, Route_Group $group ): Route {
106
		$route = new Route( \strtoupper( $method ), $group->get_route() );
107
		$route->namespace( $group->get_namespace() );
108
109
		foreach ( $group->get_authentication() as $auth_callback ) {
110
			$route->authentication( $auth_callback );
111
		}
112
113
		foreach ( $group->get_arguments() as $argument ) {
114
			$route->argument( $argument );
115
		}
116
117
		return $route;
118
	}
119
120
	/**
121
	 * Registers all routes added to the loader
122
	 *
123
	 * @return void
124
	 */
125
	public function execute(): void {
126
		$this->loader->register_hooks();
127
	}
128
129
}
130