Skip to content

Commit

Permalink
fix type hints and method documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Phobetor authored Sep 21, 2017
1 parent 22abfef commit c6aaf55
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions Service/GoogleTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Cache\CacheProvider;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -32,14 +33,14 @@ class GoogleTranslator
/**
* Guzzle Client.
*
* @var GuzzleHttp\ClientInterface
* @var \GuzzleHttp\ClientInterface
*/
private $client;

/**
* Cache Provider.
*
* @var Doctrine\Common\Cache\CacheProvider
* @var \Doctrine\Common\Cache\CacheProvider
*/
private $cacheProvider;

Expand Down Expand Up @@ -98,7 +99,7 @@ public function setCache(CacheProvider $cacheProvider, array $cacheCalls)
protected function getCacheValue($method, $params = array())
{
if (!isset($this->cacheCalls[$method]) || $this->cacheCalls[$method] !== true) {
return;
return null;
}
$id = $this->getCacheId($method, $params);

Expand All @@ -124,11 +125,13 @@ protected function getCacheId($method, $params = array())
* @param string $method
* @param mixed $value
* @param array $params
*
* @return bool
*/
protected function setCacheValue($method, $value, $params = array())
{
if (!isset($this->cacheCalls[$method]) || $this->cacheCalls[$method] !== true) {
return;
return false;
}

return $this->cacheProvider->save($this->getCacheId($method, $params), $value);
Expand All @@ -137,11 +140,15 @@ protected function setCacheValue($method, $value, $params = array())
/**
* Call API with curl.
*
* @param [type] $fonction [description]
* @param string $function
* @param array $params
* @param string $method
*
* @return mixed array or string
*
* @return [type] [description]
* @throws \Exception
*/
private function call($fonction, $params = array(), $method = 'GET')
private function call($function, $params = array(), $method = 'GET')
{
$params['key'] = $this->apiKey;

Expand All @@ -155,9 +162,9 @@ private function call($fonction, $params = array(), $method = 'GET')
}

try {
$response = $this->client->request($method, self::URL.$fonction, $clientParams);
} catch (\GuzzleHttp\Exception\RequestException $e) {
$this->logger->error('REST Exception', ['e' => $e, 'url' => self::URL.$fonction, 'params' => $clientParams]);
$response = $this->client->request($method, self::URL.$function, $clientParams);
} catch (RequestException $e) {
$this->logger->error('REST Exception', ['e' => $e, 'url' => self::URL.$function, 'params' => $clientParams]);
$message = $e->getResponse()->getReasonPhrase();

$json = json_decode($e->getResponse()->getBody());
Expand All @@ -177,6 +184,8 @@ private function call($fonction, $params = array(), $method = 'GET')
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return mixed array or string
*
* @throws \Exception
*/
private function handlingCallResponse(ResponseInterface $response)
{
Expand All @@ -187,8 +196,8 @@ private function handlingCallResponse(ResponseInterface $response)
$json = json_decode($response->getBody());

if (is_null($json)) {
$this->logger->error('Unable to decode response : '.$response);
throw new \Exception('Unable to decode response : '.$response);
$this->logger->error('Unable to decode response : '.$response->getBody());
throw new \Exception('Unable to decode response : '.$response->getBody());
}
if (!isset($json->data)) {
if (isset($json->error, $json->error->message)) {
Expand All @@ -207,6 +216,8 @@ private function handlingCallResponse(ResponseInterface $response)
* Return supported languages.
*
* @return array
*
* @throws \Exception
*/
public function getSupportedLanguages()
{
Expand Down Expand Up @@ -273,6 +284,8 @@ public function translate($source, $target, $text)
* @param string $method GET|POST
*
* @return array
*
* @throws \Exception
*/
private function handleTranslateResponse($source, $target, $text, $method = 'GET')
{
Expand Down

0 comments on commit c6aaf55

Please sign in to comment.