Endpoint   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 3
dl 0
loc 139
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A get_route() 0 3 2
A get_options() 0 16 3
A set_guard() 0 5 1
A set_filter() 0 5 1
A set_prefix() 0 9 3
1
<?php
2
namespace Intraxia\Jaxion\Http;
3
4
use Intraxia\Jaxion\Contract\Http\Guard as GuardContract;
5
use Intraxia\Jaxion\Contract\Http\Filter as FilterContract;
6
use Intraxia\Jaxion\Utility\Str;
7
8
/**
9
 * Class Endpoint
10
 *
11
 * Manages the data for a single endpoint.
12
 *
13
 * @package Intraxia\Jaxion
14
 * @subpackage Http
15
 */
16
class Endpoint {
17
	/**
18
	 * Endpoint's route.
19
	 *
20
	 * @var string
21
	 */
22
	protected $route;
23
24
	/**
25
	 * Endpoint's HTTP verb(s).
26
	 *
27
	 * @var string
28
	 */
29
	protected $method;
30
31
	/**
32
	 * Endpoint's callback.
33
	 *
34
	 * @var callable
35
	 */
36
	protected $callback;
37
38
	/**
39
	 * Endpoint's permission guard.
40
	 *
41
	 * @var GuardContract
42
	 */
43
	protected $guard;
44
45
	/**
46
	 * Endpoint's arguments filter.
47
	 *
48
	 * @var FilterContract
49
	 */
50
	protected $filter;
51
52
	/**
53
	 * Endpoint's route prefix.
54
	 *
55
	 * @var string
56
	 */
57
	protected $prefix;
58
59
	/**
60
	 * Instantiate a new endpoint with a provided route, method, and callback.
61
	 *
62
	 * @param string   $route
63
	 * @param string   $method
64
	 * @param callable $callback
65
	 *
66
	 * @throws MalformedRouteException
67
	 */
68 45
	public function __construct( $route, $method, $callback ) {
69 45
		if ( ! Str::starts_with( $route, '/' ) || Str::ends_with( $route, '/' ) ) {
70 3
			throw new MalformedRouteException;
71
		}
72
73 42
		$this->route    = $route;
74 42
		$this->method   = $method;
75 42
		$this->callback = $callback;
76 42
	}
77
78
	/**
79
	 * Generates the endpoint's route.
80
	 *
81
	 * Combines the prefix with the route to generate the full route string.
82
	 *
83
	 * @return string
84
	 */
85 39
	public function get_route() {
86 39
		return ( $this->prefix ?: '' ) . $this->route;
87
	}
88
89
	/**
90
	 * Generates the endpoint's WP-API options array.
91
	 *
92
	 * @return array
93
	 */
94 39
	public function get_options() {
95
		$options = array(
96 39
			'methods'  => $this->method,
97 39
			'callback' => $this->callback,
98 26
		);
99
100 39
		if ( $this->guard ) {
101 6
			$options['permission_callback'] = array( $this->guard, 'authorized' );
102 4
		}
103
104 39
		if ( $this->filter ) {
105 6
			$options['args'] = $this->filter->rules();
106 4
		}
107
108 39
		return $options;
109
	}
110
111
	/**
112
	 * Sets the endpoint's permission guard.
113
	 *
114
	 * @param GuardContract $guard
115
	 *
116
	 * @return $this
117
	 */
118 6
	public function set_guard( GuardContract $guard ) {
119 6
		$this->guard = $guard;
120
121 6
		return $this;
122
	}
123
124
	/**
125
	 * Sets the endpoint's arguments filter.
126
	 *
127
	 * @param FilterContract $filter
128
	 *
129
	 * @return $this
130
	 */
131 6
	public function set_filter( FilterContract $filter ) {
132 6
		$this->filter = $filter;
133
134 6
		return $this;
135
	}
136
137
	/**
138
	 * Sets the endpoint's prefix.
139
	 *
140
	 * @param string $prefix
141
	 *
142
	 * @return $this
143
	 * @throws MalformedRouteException
144
	 */
145 6
	public function set_prefix( $prefix ) {
146 6
		if ( ! Str::starts_with( $prefix, '/' ) || Str::ends_with( $prefix, '/' ) ) {
147 3
			throw new MalformedRouteException;
148
		}
149
150 3
		$this->prefix = $prefix;
151
152 3
		return $this;
153
	}
154
}
155