1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216:
<?php
namespace FrontBundle\Controller;
use FrontBundle\Client\ClientInterface;
use FrontBundle\Utils\IriHelper;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Message\RequestInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller as SymfonyController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
class BaseController extends SymfonyController implements ApiControllerInterface
{
protected $client;
protected $serializer;
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer($container);
$this->client = $this->get('api.client');
$this->serializer = $this->get('serializer');
}
public function generateUrl($route, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
if (array_key_exists('id', $parameters)) {
$parameters['id'] = IriHelper::extractId($parameters['id']);
}
return parent::generateUrl($route, $parameters, $referenceType);
}
public function decode($data, array $context = [])
{
return $this->serializer->decode($data, JsonEncoder::FORMAT, $context);
}
public function createRequest($method, $url = null, $token = null, $options = [], $wholeCollection = false)
{
if ($token instanceof RequestInterface) {
if (!isset($options['headers'])) {
$options['headers'] = [];
}
$options['headers']['authorization'] = $token->getHeader('authorization');
$token = null;
} elseif ($token instanceof Request) {
$token = $token->getSession()->get('api_token');
}
return $this->client->createRequest($method, $url, $token, $options);
}
public function requestAndDecode($method, $url = null, $token = null, $options = [], $wholeCollection = false)
{
return $this->sendAndDecode($this->createRequest($method, $url, $token, $options), $wholeCollection);
}
public function sendAndDecode(RequestInterface $request, $wholeCollection = false)
{
$decodedResponse = $this->decode($this->client->send($request)->getBody());
if (false === $wholeCollection) {
return $decodedResponse;
}
return $this->getEntitiesFromCollection(
$decodedResponse,
$request
);
}
public function handleGuzzleException(TransferException $exception)
{
$message = null;
switch (true) {
case $exception instanceof ConnectException:
break;
case $exception instanceof RequestException:
$response = $exception->getResponse();
switch ($response->getHeader('Content-Type')) {
case 'application/ld+json':
$body = $this->decode($response->getBody());
$type = $body['@type'];
switch ($type) {
case 'Error':
$message = sprintf(
'%s: %s',
$body['hydra:title'],
$body['hydra:description']
);
break;
case 'ConstraintViolationList':
foreach ($body['violations'] as $violation) {
$this->addFlash(
'error',
sprintf('%s: %s', $violation['propertyPath'], $violation['message'])
);
}
return;
}
break;
case 'application/json':
$message = $this->decode($response->getBody());
break;
}
break;
}
if (null === $message) {
$message = $exception->getMessage();
$message = (true === empty($message))
? 'Une erreur est survenue.'
: $message
;
}
$this->addFlash('error', $message);
}
public function getDoctrine()
{
throw new \LogicException('The DoctrineBundle should not be used in the Front application.');
}
private function getEntitiesFromCollection(array $decodedResponse, $token = null)
{
if ('hydra:PagedCollection' !== $decodedResponse['@type']) {
return $decodedResponse;
}
$resources = [$decodedResponse['hydra:member']];
while (isset($decodedResponse['hydra:nextPage'])) {
$decodedResponse = $this->requestAndDecode(
'GET',
$decodedResponse['hydra:nextPage'],
$token
);
$resources[] = $decodedResponse['hydra:member'];
}
return call_user_func_array('array_merge', $resources);
}
}