2
\$\begingroup\$

I'm at the point where I am happy with the functionality of my code but I dislike how its written. It seems like I can be more modular with it but I can't seem to figure out how to improve on it without changing to much. I understand that I can probably merge both getMed Methods but they are end points in a URL which I rather keep separate. Any assistance will be greatly appreciated

private function getSIS($url, $session, Application $app)
{
 $response = "You don't have permission to access this!";
 if($app['security']->isGranted('ROLE_SUPER_ADMIN'))
 {
 //['fomservices']['sis'] is hard coded in my config dev/prod file.
 $client = new Client(['base_uri' => $app['fomservices']['sis'],]);
 //for testing
 $token = 123456;
 try {
 if (!empty($session)) {
 $response = $client->request('GET', $url.'/'.$session, [
 'headers' => [
 'token' => $token
 ]
 ]);
 } else {
 $response = $client->request('GET', $url, [
 'headers' => [
 'token' => $token
 ]
 ]);
 }
 return $response;
 } catch (ClientException $e) {
 $response = $e->getResponse();
 $responseBodyAsString = $response->getBody()->getContents();
 return new Response($responseBodyAsString, $response->getStatusCode());
 } catch (ServerException $e) {
 $response = $e->getResponse();
 $responseBodyAsString = $response->getBody()->getContents();
 return new Response($responseBodyAsString, $response->getStatusCode());
 } catch (BadResponseException $e){
 $response = $e->getResponse();
 $responseBodyAsString = $response->getBody()->getContents();
 return new Response($responseBodyAsString, $response->getStatusCode());
 }
 }
 return $response;
}
public function getMedAction(Application $app)
{
 $response = $this->getSIS('med', '', $app);
 if($response instanceof \GuzzleHttp\Psr7\Response)
 return $response->getBody();
 return new Response ($response);
}
public function getMedSessionAction($session, Application $app)
{
 $response = $this->getSIS('med', $session, $app);
 if($response instanceof \GuzzleHttp\Psr7\Response)
 return $response->getBody();
 return new Response ($response);
}
asked Sep 21, 2015 at 14:57
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I was able to improve on my previous code by making sure all the return statements were returning the same type of response (String / StatusCode). This allowed the 20 methods calling this one must smaller since I didn't have to check what response contain.

private function getSIS($url, $session, Application $app)
{
 if($app['security']->isGranted('ROLE_SUPER_ADMIN'))
 {
 $client = new Client(['base_uri' => $app['fomservices']['sis']]);
 $token = 234561;
 // Calling the external API which multiple errors could occur.
 try {
 if (!empty($session)) {
 $response = $client->request('GET', $url.'/'.$session, [
 'headers' => [
 'token' => $token
 ]
 ]);
 } else {
 $response = $client->request('GET', $url, [
 'headers' => [
 'token' => $token
 ]
 ]);
 }
 return new Response($response->getBody(), 200);
 } catch (ClientException $e) {
 $response = $e->getResponse();
 return new Response($response->getBody()->getContents(), $response->getStatusCode());
 } catch (ServerException $e) {
 $response = $e->getResponse();
 return new Response($response->getBody()->getContents(), $response->getStatusCode());
 } catch (BadResponseException $e){
 $response = $e->getResponse();
 return new Response($response->getBody()->getContents(), $response->getStatusCode());
 }
 }
 return new Response("You don't have permission to access this!", 401);
}
public function getMedAction(Application $app)
{
 return $this->getSIS('med', '', $app);
}
public function getMedSessionAction($session, Application $app)
{
 return $this->getSIS('med', $session, $app);
}
answered Sep 22, 2015 at 15:38
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.