|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSHttpCache package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\HttpCache\HttpClient; |
| 13 | + |
| 14 | +use FOS\HttpCache\Exception\ExceptionCollection; |
| 15 | +use FOS\HttpCache\Exception\ProxyResponseException; |
| 16 | +use FOS\HttpCache\Exception\ProxyUnreachableException; |
| 17 | +use FOS\HttpCache\ProxyClient\Request\RequestQueue; |
| 18 | +use Guzzle\Common\Exception\ExceptionCollection as GuzzleExceptionCollection; |
| 19 | +use Guzzle\Http\Client; |
| 20 | +use Guzzle\Http\ClientInterface; |
| 21 | +use Guzzle\Http\Exception\CurlException; |
| 22 | +use Guzzle\Http\Exception\RequestException; |
| 23 | + |
| 24 | +class Guzzle3Adapter implements HttpClientInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Constructor |
| 28 | + * |
| 29 | + * @param ClientInterface $client Guzzle3 client (optional) |
| 30 | + */ |
| 31 | + public function __construct(ClientInterface $client = null) |
| 32 | + { |
| 33 | + $this->client = $client ?: new Client(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function sendRequests(RequestQueue $requests) |
| 40 | + { |
| 41 | + $guzzleRequests = array(); |
| 42 | + foreach ($requests as $request) { |
| 43 | + $guzzleRequests[] = $this->client->createRequest( |
| 44 | + $request->getMethod(), |
| 45 | + $request->getUrl(), |
| 46 | + $request->getHeaders() |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + $this->client->send($guzzleRequests); |
| 52 | + } catch (GuzzleExceptionCollection $e) { |
| 53 | + $this->handleException($e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Handle request exception |
| 59 | + * |
| 60 | + * @param GuzzleExceptionCollection $exceptions |
| 61 | + * |
| 62 | + * @throws ExceptionCollection |
| 63 | + */ |
| 64 | + private function handleException(GuzzleExceptionCollection $exceptions) |
| 65 | + { |
| 66 | + $collection = new ExceptionCollection(); |
| 67 | + |
| 68 | + foreach ($exceptions as $exception) { |
| 69 | + if ($exception instanceof CurlException) { |
| 70 | + // Caching proxy unreachable |
| 71 | + $e = ProxyUnreachableException::proxyUnreachable( |
| 72 | + $exception->getRequest()->getHost(), |
| 73 | + $exception->getMessage(), |
| 74 | + $exception |
| 75 | + ); |
| 76 | + } elseif ($exception instanceof RequestException) { |
| 77 | + // Other error |
| 78 | + $e = ProxyResponseException::proxyResponse( |
| 79 | + $exception->getRequest()->getHost(), |
| 80 | + $exception->getCode(), |
| 81 | + $exception->getMessage(), |
| 82 | + $exception->getRequest()->getRawHeaders(), |
| 83 | + $exception |
| 84 | + ); |
| 85 | + } else { |
| 86 | + // Unexpected exception type |
| 87 | + $e = $exception; |
| 88 | + } |
| 89 | + |
| 90 | + $collection->add($e); |
| 91 | + } |
| 92 | + |
| 93 | + throw $collection; |
| 94 | + } |
| 95 | +} |
0 commit comments