commit 41b7c6ac77a4c2910e09394e09da1c1d3dbe3dc9 Author: Antoine Date: Thu Mar 28 17:25:42 2024 +0100 Composition en PHP Slim diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/LoanApproval/composer.json b/LoanApproval/composer.json new file mode 100644 index 0000000..fab136c --- /dev/null +++ b/LoanApproval/composer.json @@ -0,0 +1,7 @@ +{ + "require": { + "slim/psr7": "^1.6", + "slim/slim": "4.*", + "guzzlehttp/guzzle": "^7.0" + } +} diff --git a/LoanApproval/composer.phar b/LoanApproval/composer.phar new file mode 100644 index 0000000..e6ba7bb Binary files /dev/null and b/LoanApproval/composer.phar differ diff --git a/LoanApproval/public/index.php b/LoanApproval/public/index.php new file mode 100644 index 0000000..1a470be --- /dev/null +++ b/LoanApproval/public/index.php @@ -0,0 +1,71 @@ +addPsr4('BL\\', __DIR__); +$app = AppFactory::create(); + +$app->addBodyParsingMiddleware(); +$app->addRoutingMiddleware(); +$app->addErrorMiddleware(true, true, true); + +$proxy1='192.168.128.139:8080';//http://193.49.118.36:8080'; +$proxy2='proxycl.iut.uca.fr ';//http://193.49.118.36:8080'; +$ip_AccManager="192.168.127.122:8080"; +$ip_AppManager="192.168.127.122:8082"; + +function humain($ip_AppManager, $approval_id, Response $response, $account_id) +{ + $manager = new Client(['base_uri' => $ip_AppManager]); + $res_approved = $manager->request('GET', '/approval/approvals/' . $approval_id); + if (str_contains($res_approved->getBody(), "APPROVED")) $response->getBody()->write("Approved loan for account n°" . $account_id . ". Approval id : " . $approval_id); + else $response->getBody()->write("Refused loan for account n°" . $account_id . ". Approval id : " . $approval_id); +} + +$app->post('/loan', function(Request $request, Response $response, $args) use ($ip_AccManager, $ip_AppManager, $proxy1, $proxy2) { + try{ + $data = $request->getParsedBody(); + $somme = $data['somme']; + $account_id = $data['account_id']; + $approval_id = $data['approval_id']; + + if($somme >= 10000){ + try{ + humain($ip_AppManager, $approval_id, $response, $account_id); + return $response->withHeader('Content-type', 'text/html')->withStatus(200); + } + catch (ConnectException $ee){ throw new Exception("Error in URL"); } + catch (ClientException $e) { + throw new Psr7\Message::toString($e->getRequest()); + throw new Psr7\Message::toString($e->getResponse()); + } + } + else{ + try{ + $client = new Client(['base_uri' => $ip_AccManager]); + $res_acc = $client->request('GET', '/account/Accounts/' . $account_id, ['proxy'=>['http' => $proxy1, 'https'=> $proxy2]]); + if ($res_acc->getBody() == 'high'){ + humain($ip_AppManager, $approval_id, $response, $account_id); + } + else{ + $response->getBody()->write("Approved loan for account n°" . $account_id . ". Approval id : " . $approval_id); + } + return $response->withHeader('Content-type', 'text/html')->withStatus(200); + } + catch (ConnectException $ee){ throw new Exception("Error in URL"); } + catch (ClientException $e) { + throw new Psr7\Message::toString($e->getRequest()); + throw new Psr7\Message::toString($e->getResponse()); + } + } + } + catch(Exception $e){ + throw new HttpInternalServerErrorException(); + } +}); + +$app->run(); \ No newline at end of file