1
|
|
|
<?php |
2
|
|
|
namespace LunixREST\Server\Router; |
3
|
|
|
|
4
|
|
|
use LunixREST\Server\Router\Endpoint\Endpoint; |
5
|
|
|
use LunixREST\Server\APIRequest\APIRequest; |
6
|
|
|
use LunixREST\Server\Router\Endpoint\Exceptions\ElementConflictException; |
7
|
|
|
use LunixREST\Server\Router\Endpoint\Exceptions\ElementNotFoundException; |
8
|
|
|
use LunixREST\Server\Router\Endpoint\Exceptions\EndpointExecutionException; |
9
|
|
|
use LunixREST\Server\Router\Endpoint\Exceptions\InvalidRequestException; |
10
|
|
|
use LunixREST\Server\Router\Endpoint\Exceptions\UnsupportedMethodException; |
11
|
|
|
use LunixREST\Server\Router\EndpointFactory\EndpointFactory; |
12
|
|
|
use LunixREST\Server\APIResponse\APIResponseData; |
13
|
|
|
use LunixREST\Server\Router\EndpointFactory\Exceptions\UnableToCreateEndpointException; |
14
|
|
|
use LunixREST\Server\Router\Exceptions\MethodNotFoundException; |
15
|
|
|
use LunixREST\Server\Router\Exceptions\UnableToRouteRequestException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An implementation of a Router that uses an EndpointFactory to find an Endpoint. Maps methods based on the get/getAll pattern. |
19
|
|
|
* Class GenericRouter |
20
|
|
|
* @package LunixREST\Server\Router |
21
|
|
|
*/ |
22
|
|
|
class GenericRouter implements Router |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EndpointFactory |
26
|
|
|
*/ |
27
|
|
|
private $endpointFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* DefaultRouter constructor. |
31
|
|
|
* @param EndpointFactory $endpointFactory |
32
|
|
|
*/ |
33
|
3 |
|
public function __construct(EndpointFactory $endpointFactory) |
34
|
|
|
{ |
35
|
3 |
|
$this->endpointFactory = $endpointFactory; |
36
|
3 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param APIRequest $request |
40
|
|
|
* @return APIResponseData |
41
|
|
|
* @throws UnableToRouteRequestException |
42
|
|
|
* @throws UnableToCreateEndpointException |
43
|
|
|
* @throws MethodNotFoundException |
44
|
|
|
* @throws EndpointExecutionException |
45
|
|
|
* @throws UnsupportedMethodException |
46
|
|
|
* @throws ElementNotFoundException |
47
|
|
|
* @throws InvalidRequestException |
48
|
|
|
* @throws ElementConflictException |
49
|
|
|
*/ |
50
|
3 |
|
public function route(APIRequest $request): APIResponseData |
51
|
|
|
{ |
52
|
3 |
|
$endpoint = $this->endpointFactory->getEndpoint($request->getEndpoint(), $request->getVersion()); |
53
|
3 |
|
return $this->executeEndpoint($endpoint, $request); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Endpoint $endpoint |
58
|
|
|
* @param APIRequest $request |
59
|
|
|
* @return APIResponseData |
60
|
|
|
* @throws UnableToRouteRequestException |
61
|
|
|
* @throws MethodNotFoundException |
62
|
|
|
* @throws EndpointExecutionException |
63
|
|
|
* @throws UnsupportedMethodException |
64
|
|
|
* @throws ElementNotFoundException |
65
|
|
|
* @throws InvalidRequestException |
66
|
|
|
* @throws ElementConflictException |
67
|
|
|
*/ |
68
|
3 |
|
protected function executeEndpoint(Endpoint $endpoint, APIRequest $request): APIResponseData |
69
|
|
|
{ |
70
|
3 |
|
$method = $this->mapEndpointMethod($request); |
71
|
3 |
|
if (!method_exists($endpoint, $method)) { |
72
|
1 |
|
throw new MethodNotFoundException("The endpoint method " . $method . " was not found"); |
73
|
|
|
} |
74
|
2 |
|
return call_user_func([$endpoint, $method], $request); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param APIRequest $request |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
3 |
|
protected function mapEndpointMethod(APIRequest $request): string |
82
|
|
|
{ |
83
|
3 |
|
return strtolower($request->getMethod()) . (!$request->getElement() ? 'All': ''); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|