Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Dec 3, 2023
1 parent 733cbe9 commit f0ef137
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 111 deletions.
10 changes: 6 additions & 4 deletions tests/webfiori/tests/http/WebServicesManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
namespace webfiori\tests\http;

use webfiori\json\Json;
use PHPUnit\Framework\TestCase;
use webfiori\http\AbstractWebService;
use webfiori\http\Request;
use webfiori\http\ResponseMessages;
use webfiori\http\WebServicesManager;
use webfiori\json\Json;
use webfiori\tests\http\testServices\NoAuthService;
use webfiori\tests\http\testServices\NotImplService;
use webfiori\tests\http\testServices\SampleServicesManager;
use webfiori\tests\http\testServices\NoAuthService;
/**
* Description of WebAPITest
*
Expand Down Expand Up @@ -160,7 +162,7 @@ public function testConstructor00() {
$this->clrearVars();
putenv('REQUEST_METHOD=GET');
$api = new SampleServicesManager();
$this->assertEquals('GET', \webfiori\http\Request::getMethod());
$this->assertEquals('GET', Request::getMethod());
$this->assertNull($api->getCalledServiceName());
$this->assertEquals('1.0.1',$api->getVersion());
$this->assertEquals('NO DESCRIPTION',$api->getDescription());
Expand Down Expand Up @@ -393,7 +395,7 @@ public function testSumArray04() {
$_POST['numbers'] = '[1,2,"as",1.9,\'hello\',10]';
$_POST['pass'] = '1234';
$api = new SampleServicesManager();
$api->setResponseMessage('401', 'Your password is inncorrect.');
ResponseMessages::set('401', 'Your password is inncorrect.');
$api->setOutputStream($this->outputStreamName);
$api->process();
$this->assertEquals('{"message":"Your password is inncorrect.","type":"error","http-code":401}', $api->readOutputStream());
Expand Down
34 changes: 2 additions & 32 deletions webfiori/http/AbstractWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,7 @@ abstract class AbstractWebService implements JsonI {
* @since 1.0.2
*/
const I = 'info';
/**
* An array that contains the names of request methods.
*
* This array contains the following strings:
* <ul>
* <li>GET</li>
* <li>HEAD</li>
* <li>POST</li>
* <li>PUT</li>
* <li>DELETE</li>
* <li>TRACE</li>
* <li>OPTIONS</li>
* <li>PATCH</li>
* <li>CONNECT</li>
* </ul>
*
* @var array An array that contains the names of request methods.
*
* @since 1.0
*/
const METHODS = [
'GET',
'HEAD',
'POST',
'PUT',
'DELETE',
'TRACE',
'OPTIONS',
'PATCH',
'CONNECT'
];

/**
* The name of the service.
*
Expand Down Expand Up @@ -342,7 +312,7 @@ public function addParameters(array $params) {
public final function addRequestMethod(string $method) : bool {
$uMethod = strtoupper(trim($method));

if (in_array($uMethod, self::METHODS) && !in_array($uMethod, $this->reqMethods)) {
if (in_array($uMethod, RequestMethod::getAll()) && !in_array($uMethod, $this->reqMethods)) {
$this->reqMethods[] = $uMethod;

return true;
Expand Down
95 changes: 95 additions & 0 deletions webfiori/http/RequestMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace webfiori\http;

/**
* A class which is used to hold names of request methods.
*
* @author Ibrahim
*/
class RequestMethod {
/**
* A constant which is used to represent 'GET' request method.
*
* @var string
*/
const GET = 'GET';
/**
* A constant which is used to represent 'GET' request method.
*
* @var string
*/
const POST = 'POST';
/**
* A constant which is used to represent 'PUT' request method.
*
* @var string
*/
const PUT = 'PUT';
/**
* A constant which is used to represent 'head' request method.
*
* @var string
*/
const HEAD = 'HEAD';
/**
* A constant which is used to represent 'patch' request method.
*
* @var string
*/
const PATCH = 'PATCH';
/**
* A constant which is used to represent 'delete' request method.
*
* @var string
*/
const DELETE = 'DELETE';
/**
* A constant which is used to represent 'trace' request method.
*
* @var string
*/
const TRACE = 'TRACE';
/**
* A constant which is used to represent 'options' request method.
*
* @var string
*/
const OPTIONS = 'OPTIONS';
/**
* A constant which is used to represent 'connect' request method.
*
* @var string
*/
const CONNECT = 'CONNECT';
/**
* Returns an array that holds request methods in upper case.
*
* The returned array will contains the following strings:
* <ul>
* <li>GET</li>
* <li>HEAD</li>
* <li>POST</li>
* <li>PUT</li>
* <li>DELETE</li>
* <li>TRACE</li>
* <li>OPTIONS</li>
* <li>PATCH</li>
* <li>CONNECT</li>
* </ul>
*
* @return array An array of request methods names.
*/
public static function getAll() : array {
return [
self::CONNECT,
self::DELETE,
self::GET,
self::HEAD,
self::OPTIONS,
self::POST,
self::PUT,
self::TRACE
];
}
}
85 changes: 85 additions & 0 deletions webfiori/http/ResponseMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace webfiori\http;

/**
* A class which is used to keep track of values of default response messages.
*
* The library supports following default status messages.
* <ul>
* <li>'401': 'Not Authorized.'</li>
* <li>'404-1': 'The following parameter(s) has invalid values: '</li>
* <li>'404-2': 'The following required parameter(s) where missing from the request body: '</li>
* <li>'404-3': 'Service name is not set.'</li>
* <li>'404-4': 'Service not implemented.'</li>
* <li>'404-5': 'Service not supported.'</li>
* <li>'404-6': 'Request method(s) of the service are not set in code.'</li>
* <li>'405': 'Method Not Allowed.'</li>
* <li>'415': 'Content type not supported.'</li>
*
* @author Ibrahim
*/
class ResponseMessages {
private $messages;
private static $inst;
/**
*
* @return ResponseMessages
*/
private static function getInstance() : ResponseMessages {
if (self::$inst === null) {
self::$inst = new ResponseMessages();
}
return self::$inst;
}
/**
* Sets a custom HTTP response message for specific error code.
*
* This method is used to customize the message that will be sent back to
* the client in case of using a method such as WebServicesManager::notAuth().
* Also, this method can be used to add custom code with error message.
*
* @param string $code A string that represent the error code. By default,
* the class has the following error codes pre-defined:
* <ul>
* <li>'401': 'Not Authorized.'</li>
* <li>'404-1': 'The following parameter(s) has invalid values: '</li>
* <li>'404-2': 'The following required parameter(s) where missing from the request body: '</li>
* <li>'404-3': 'Service name is not set.'</li>
* <li>'404-4': 'Service not implemented.'</li>
* <li>'404-5': 'Service not supported.'</li>
* <li>'404-6': 'Request method(s) of the service are not set in code.'</li>
* <li>'405': 'Method Not Allowed.'</li>
* <li>'415': 'Content type not supported.'</li>
* </ul>
*
* @param string $message A string that represents the error message.
*/
public static function set(string $code, string $message) {
self::getInstance()->messages[trim($code)] = $message;
}
/**
* Returns the value of response message given its code.
*
* @param string $code The code of the message such as 415.
*
* @return string If the code has an error message set, the method will
* return it. Other than that, the string '-' is returned.
*/
public static function get(string $code) : string {
return isset(self::getInstance()->messages[trim($code)]) ?? '-';
}
private function __construct() {
$this->messages = [
'401' => 'Not Authorized.',
'404-1' => 'The following parameter(s) has invalid values: ',
'404-2' => 'The following required parameter(s) where missing from the request body: ',
'404-3' => 'Service name is not set.',
'404-4' => 'Service not implemented.',
'404-5' => 'Service not supported.',
'404-6' => 'Request methods of the service are not set in code.',
'405' => 'Method Not Allowed.',
'415' => 'Content type not supported.'
];
}
}
Loading

0 comments on commit f0ef137

Please sign in to comment.