12
12
13
13
class RouteApi
14
14
{
15
- private $ container ;
16
- private $ recordAction ;
17
- private $ getDetails ;
18
- private $ hostList ;
19
- private $ fetchAllowedProjects ;
20
- private $ fetchUserDetails ;
21
- private $ fetchUserProject ;
22
- private $ invalidateToken ;
15
+ private Container $ container ;
16
+ private RecordAction $ recordAction ;
17
+ private GetDetails $ getDetails ;
18
+ private HostList $ hostList ;
19
+ private FetchAllowedProjects $ fetchAllowedProjects ;
20
+ private FetchUserDetails $ fetchUserDetails ;
21
+ private FetchUserProject $ fetchUserProject ;
22
+ private InvalidateToken $ invalidateToken ;
23
23
24
- private $ project ;
25
- private $ userId ;
24
+ private ? string $ project ;
25
+ private int $ userId ;
26
26
27
27
28
28
public function __construct (
@@ -50,12 +50,12 @@ public function getRequestedProject()
50
50
return $ this ->project ;
51
51
}
52
52
53
- public function getUserId ()
53
+ public function getUserId () : int
54
54
{
55
55
return $ this ->userId ;
56
56
}
57
57
58
- public function route ($ pathParts , $ headers , $ returnResult = false )
58
+ public function route (array $ pathParts , array $ headers , bool $ returnResult = false )
59
59
{
60
60
$ userId = $ headers ["userid " ];
61
61
@@ -75,7 +75,7 @@ public function route($pathParts, $headers, $returnResult = false)
75
75
76
76
unset($ pathParts [$ methodkey ]);
77
77
78
- $ controllerStr = "dhope0000 \\LXDClient \\Controllers \\" . implode ($ pathParts , "\\" );
78
+ $ controllerStr = "dhope0000 \\LXDClient \\Controllers \\" . implode ("\\" , $ pathParts );
79
79
if (!class_exists ($ controllerStr )) {
80
80
throw new \Exception ("End point not found " , 1 );
81
81
} elseif (method_exists ($ controllerStr , $ method ) !== true ) {
@@ -90,8 +90,14 @@ public function route($pathParts, $headers, $returnResult = false)
90
90
$ this ->recordAction ->record ($ userId , $ controllerStr . "\\" . $ method , $ params );
91
91
}
92
92
93
+ $ callback = array ($ controller , $ method );
94
+
95
+ if (!is_callable ($ callback )) {
96
+ throw new \Exception ("Cant find route " , 1 );
97
+ }
98
+
93
99
// TODO Pass provided arguments to controller
94
- $ data = call_user_func_array (array ( $ controller , $ method ) , $ params );
100
+ $ data = call_user_func_array ($ callback , $ params );
95
101
96
102
if ($ returnResult ) {
97
103
return $ data ;
@@ -101,7 +107,7 @@ public function route($pathParts, $headers, $returnResult = false)
101
107
echo json_encode ($ data );
102
108
}
103
109
104
- public function orderParams ($ passedArguments , $ class , $ method , int $ userId , $ headers )
110
+ public function orderParams (array $ passedArguments , string $ class , string $ method , int $ userId , array $ headers ) : array
105
111
{
106
112
$ reflectedMethod = new \ReflectionMethod ($ class , $ method );
107
113
$ paramaters = $ reflectedMethod ->getParameters ();
@@ -124,7 +130,7 @@ public function orderParams($passedArguments, $class, $method, int $userId, $hea
124
130
$ hasDefault = $ param ->isDefaultValueAvailable ();
125
131
126
132
$ type = $ param ->getType ();
127
- if (!empty ($ type )) {
133
+ if (!empty ($ type ) && $ type instanceof \ReflectionNamedType ) {
128
134
$ type = $ type ->getName ();
129
135
}
130
136
@@ -186,7 +192,7 @@ public function orderParams($passedArguments, $class, $method, int $userId, $hea
186
192
return $ o ;
187
193
}
188
194
189
- private function canAccessProject ($ allowedProjects , $ hostId , $ project )
195
+ private function canAccessProject (array $ allowedProjects , int $ hostId , string $ project ) : void
190
196
{
191
197
if (!is_string ($ project ) || strlen ($ project ) == 0 ) {
192
198
throw new \Exception ("Cant work out which project to use " , 1 );
0 commit comments