@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * @param string $filename |
23 | 23 | * @param string $nameSpace |
24 | 24 | */ |
25 | - public function __construct($filename, $nameSpace = null){ |
|
25 | + public function __construct($filename, $nameSpace = null) { |
|
26 | 26 | $this->filename = $filename; |
27 | 27 | $this->nameSpace = $nameSpace; |
28 | 28 | } |
@@ -33,15 +33,15 @@ discard block |
||
33 | 33 | * @throws INIKeyNotFoundException |
34 | 34 | * @throws INIParseException |
35 | 35 | */ |
36 | - public function get($key){ |
|
37 | - $config = parse_ini_file($this->filename, (bool)$this->nameSpace); |
|
36 | + public function get($key) { |
|
37 | + $config = parse_ini_file($this->filename, (bool) $this->nameSpace); |
|
38 | 38 | |
39 | - if($config === false){ |
|
40 | - throw new INIParseException('Could not parse: ' . $this->filename, true); |
|
39 | + if ($config === false) { |
|
40 | + throw new INIParseException('Could not parse: '.$this->filename, true); |
|
41 | 41 | } |
42 | 42 | |
43 | - if($this->nameSpace) { |
|
44 | - if(isset($config[$this->nameSpace])) { |
|
43 | + if ($this->nameSpace) { |
|
44 | + if (isset($config[$this->nameSpace])) { |
|
45 | 45 | $config = $config[$this->nameSpace]; |
46 | 46 | } else { |
47 | 47 | throw new INIKeyNotFoundException(); |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | /** |
55 | 55 | * @param $key |
56 | 56 | */ |
57 | - public function set($key){ |
|
57 | + public function set($key) { |
|
58 | 58 | //TODO write this |
59 | 59 | } |
60 | 60 | } |
@@ -22,11 +22,11 @@ discard block |
||
22 | 22 | * @param $file |
23 | 23 | * @param $limitPerMinute |
24 | 24 | */ |
25 | - public function __construct($file, $limitPerMinute){ |
|
25 | + public function __construct($file, $limitPerMinute) { |
|
26 | 26 | $this->db = new \SQLite3($file); |
27 | 27 | |
28 | 28 | $result = $this->db->query('SELECT name FROM sqlite_master'); |
29 | - if(!($row = $result->fetchArray()) || $row['name'] != 'throttle'){ |
|
29 | + if (!($row = $result->fetchArray()) || $row['name'] != 'throttle') { |
|
30 | 30 | $this->db->exec('CREATE TABLE throttle (key STRING, count NUMBER, lastMinute NUMBER)'); |
31 | 31 | } |
32 | 32 | $this->limit = $limitPerMinute; |
@@ -42,20 +42,20 @@ discard block |
||
42 | 42 | * @param $key |
43 | 43 | * @return bool |
44 | 44 | */ |
45 | - protected function genericThrottle($key){ |
|
45 | + protected function genericThrottle($key) { |
|
46 | 46 | $minute = ceil(time() / 60); |
47 | - if($result = $this->db->querySingle('SELECT key, count, lastMinute FROM throttle WHERE key = ' . \SQLite3::escapeString($key), true)) { |
|
48 | - if($result['lastMinute'] == $minute){ |
|
49 | - if($result['count'] + 1 <= $this->limit){ |
|
50 | - $this->db->query('UPDATE throttle SET count = ' . ($result['count'] + 1) . ' WHERE key = ' . \SQLite3::escapeString($key)); |
|
47 | + if ($result = $this->db->querySingle('SELECT key, count, lastMinute FROM throttle WHERE key = '.\SQLite3::escapeString($key), true)) { |
|
48 | + if ($result['lastMinute'] == $minute) { |
|
49 | + if ($result['count'] + 1 <= $this->limit) { |
|
50 | + $this->db->query('UPDATE throttle SET count = '.($result['count'] + 1).' WHERE key = '.\SQLite3::escapeString($key)); |
|
51 | 51 | } else { |
52 | 52 | return true; |
53 | 53 | } |
54 | 54 | } else { |
55 | - $this->db->query('UPDATE throttle SET lastMinute = ' . $minute . ', count = 1 WHERE key = ' . \SQLite3::escapeString($key)); |
|
55 | + $this->db->query('UPDATE throttle SET lastMinute = '.$minute.', count = 1 WHERE key = '.\SQLite3::escapeString($key)); |
|
56 | 56 | } |
57 | 57 | } else { |
58 | - $this->db->query('INSERT INTO throttle (key, count, lastMinute) VALUES (' . \SQLite3::escapeString($key) . ', 1, ' . $minute . ')'); |
|
58 | + $this->db->query('INSERT INTO throttle (key, count, lastMinute) VALUES ('.\SQLite3::escapeString($key).', 1, '.$minute.')'); |
|
59 | 59 | } |
60 | 60 | return false; |
61 | 61 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @param string $extension |
58 | 58 | * @param string $instance |
59 | 59 | */ |
60 | - public function __construct($method, array $headers, array $data, $ip, $version, $apiKey, $endpoint, $extension, $instance = null){ |
|
60 | + public function __construct($method, array $headers, array $data, $ip, $version, $apiKey, $endpoint, $extension, $instance = null) { |
|
61 | 61 | $this->method = strtolower($method); |
62 | 62 | $this->headers = $headers; |
63 | 63 | $this->data = $data; |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | public function getMethod() |
84 | 84 | { |
85 | - return $this->method . ($this->instance ? '' : 'All'); |
|
85 | + return $this->method.($this->instance ? '' : 'All'); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -150,9 +150,9 @@ discard block |
||
150 | 150 | * @return Request |
151 | 151 | * @throws InvalidRequestFormatException |
152 | 152 | */ |
153 | - public static function createFromURL($method, array $headers, array $data, $ip, $url){ |
|
153 | + public static function createFromURL($method, array $headers, array $data, $ip, $url) { |
|
154 | 154 | $splitURL = explode('/', trim($url, '/')); |
155 | - if(count($splitURL) < 3){ |
|
155 | + if (count($splitURL) < 3) { |
|
156 | 156 | throw new InvalidRequestFormatException(); |
157 | 157 | } |
158 | 158 | //Find endpoint |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | |
166 | 166 | $instance = null; |
167 | 167 | |
168 | - if(count($splitURL) == 4){ |
|
168 | + if (count($splitURL) == 4) { |
|
169 | 169 | $instance = implode('.', $splitExtension); |
170 | 170 | } else { |
171 | 171 | $endpoint = implode('.', $splitExtension); |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * @param Configuration $formatsConfig |
46 | 46 | * @param string $endpointNamespace |
47 | 47 | */ |
48 | - public function __construct(AccessControl $accessControl, Throttle $throttle, Configuration $formatsConfig, $endpointNamespace = ''){ |
|
48 | + public function __construct(AccessControl $accessControl, Throttle $throttle, Configuration $formatsConfig, $endpointNamespace = '') { |
|
49 | 49 | $this->accessControl = $accessControl; |
50 | 50 | $this->throttle = $throttle; |
51 | 51 | $this->formatsConfig = $formatsConfig; |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * @throws UnknownEndpointException |
63 | 63 | * @throws UnknownResponseFormatException |
64 | 64 | */ |
65 | - public function handle(Request $request){ |
|
65 | + public function handle(Request $request) { |
|
66 | 66 | $this->validateKey($request); |
67 | 67 | |
68 | 68 | $this->validateExtension($request); |
@@ -89,24 +89,24 @@ discard block |
||
89 | 89 | * @param Request $request |
90 | 90 | * @return string |
91 | 91 | */ |
92 | - protected function endpointClass(Request $request){ |
|
93 | - return '\\' . $this->endpointNamespace . '\Endpoints\v' . str_replace('.', '_', $request->getVersion()) . '\\' . $request->getEndpoint(); |
|
92 | + protected function endpointClass(Request $request) { |
|
93 | + return '\\'.$this->endpointNamespace.'\Endpoints\v'.str_replace('.', '_', $request->getVersion()).'\\'.$request->getEndpoint(); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
97 | 97 | * @param Request $request |
98 | 98 | * @return string |
99 | 99 | */ |
100 | - protected function responseClass(Request $request){ |
|
101 | - return '\\LunixREST\\Response\\' . strtoupper($request->getExtension()) . "Response"; |
|
100 | + protected function responseClass(Request $request) { |
|
101 | + return '\\LunixREST\\Response\\'.strtoupper($request->getExtension())."Response"; |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
105 | 105 | * @param Request $request |
106 | 106 | * @throws InvalidAPIKeyException |
107 | 107 | */ |
108 | - protected function validateKey(Request $request){ |
|
109 | - if(!$this->accessControl->validateKey($request->getApiKey())){ |
|
108 | + protected function validateKey(Request $request) { |
|
109 | + if (!$this->accessControl->validateKey($request->getApiKey())) { |
|
110 | 110 | throw new InvalidAPIKeyException('Invalid API key'); |
111 | 111 | } |
112 | 112 | } |
@@ -119,8 +119,8 @@ discard block |
||
119 | 119 | protected function validateExtension(Request $request) |
120 | 120 | { |
121 | 121 | $formats = $this->formatsConfig->get('formats'); |
122 | - if(!($formats && in_array($request->getExtension(), $formats))){ |
|
123 | - throw new UnknownResponseFormatException('Unknown response format: ' .$request->getExtension()); |
|
122 | + if (!($formats && in_array($request->getExtension(), $formats))) { |
|
123 | + throw new UnknownResponseFormatException('Unknown response format: '.$request->getExtension()); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
@@ -128,10 +128,10 @@ discard block |
||
128 | 128 | * @param $fullEndpoint |
129 | 129 | * @throws UnknownEndpointException |
130 | 130 | */ |
131 | - protected function validateEndpoint($fullEndpoint){ |
|
131 | + protected function validateEndpoint($fullEndpoint) { |
|
132 | 132 | $endpointReflection = new \ReflectionClass($fullEndpoint); |
133 | - if(!class_exists($fullEndpoint) || !$endpointReflection->isSubclassOf('\LunixREST\Endpoints\Endpoint')){ |
|
134 | - throw new UnknownEndpointException("unknown endpoint: " . $fullEndpoint); |
|
133 | + if (!class_exists($fullEndpoint) || !$endpointReflection->isSubclassOf('\LunixREST\Endpoints\Endpoint')) { |
|
134 | + throw new UnknownEndpointException("unknown endpoint: ".$fullEndpoint); |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
@@ -139,8 +139,8 @@ discard block |
||
139 | 139 | * @param Request $request |
140 | 140 | * @throws ThrottleLimitExceededException |
141 | 141 | */ |
142 | - protected function throttle(Request $request){ |
|
143 | - if($this->throttle->throttle($request)){ |
|
142 | + protected function throttle(Request $request) { |
|
143 | + if ($this->throttle->throttle($request)) { |
|
144 | 144 | throw new ThrottleLimitExceededException('Request limit exceeded'); |
145 | 145 | } |
146 | 146 | } |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | * @param Request $request |
150 | 150 | * @throws AccessDeniedException |
151 | 151 | */ |
152 | - protected function validateAccess(Request $request){ |
|
153 | - if(!$this->accessControl->validateAccess($request)){ |
|
152 | + protected function validateAccess(Request $request) { |
|
153 | + if (!$this->accessControl->validateAccess($request)) { |
|
154 | 154 | throw new AccessDeniedException("API key does not have the required permissions to access requested resource"); |
155 | 155 | } |
156 | 156 | } |
@@ -159,13 +159,13 @@ discard block |
||
159 | 159 | * @param $responseData |
160 | 160 | * @throws InvalidResponseFormatException |
161 | 161 | */ |
162 | - protected function validateResponse($responseData){ |
|
163 | - if(!is_array($responseData)){ |
|
162 | + protected function validateResponse($responseData) { |
|
163 | + if (!is_array($responseData)) { |
|
164 | 164 | throw new InvalidResponseFormatException('Method output MUST be an array'); |
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
168 | - protected function executeEndpoint($fullEndpoint, Request $request){ |
|
168 | + protected function executeEndpoint($fullEndpoint, Request $request) { |
|
169 | 169 | $endPoint = new $fullEndpoint($request); |
170 | 170 | return call_user_func([$endPoint, $request->getMethod()]); |
171 | 171 | } |