Exemple #1
0
 /**
  * createResultCollection.
  *
  * @param bool $hasError
  */
 public function createResultCollection($hasError = false)
 {
     $errorLog = null;
     if ($hasError) {
         $errorLog = curl_strerror(5);
     }
     $result = new Result($this->createUrlInfo(), $hasError ? 400 : 200, 0.42, $errorLog);
     $resultCollection = new ResultCollection();
     $resultCollection->append($result);
     return $resultCollection;
 }
Exemple #2
0
 /**
  * run.
  *
  * @return array
  */
 public function run()
 {
     $urls = $this->urlProvider->getUrls();
     $client = $this->client;
     //This is a bit messie, need a refacto
     $resultCollection = new ResultCollection();
     $requests = function () use($urls, $client, $resultCollection) {
         foreach ($urls as $url) {
             (yield function () use($client, $url, $resultCollection) {
                 return $client->sendAsync(new Request($url->getMethod(), $url->getUrl(), $url->getHeaders()), ['timeout' => $url->getTimeout(), 'connect_timeout' => $url->getTimeout(), 'on_stats' => function (TransferStats $tranferStats) use($url, $resultCollection) {
                     $handlerError = null;
                     $validatorError = null;
                     $validatorResult = null;
                     if ($tranferStats->hasResponse()) {
                         $validatorResult = $url->getValidator()->check((string) $tranferStats->getResponse()->getBody());
                         if (false === $validatorResult) {
                             $validatorError = $url->getValidator()->getError();
                         }
                         $statusCode = $tranferStats->getResponse()->getStatusCode();
                         $transferTime = $tranferStats->getTransferTime();
                     } else {
                         // If we have a connection error
                         $statusCode = 400;
                         $transferTime = 0;
                         $handlerError = curl_strerror($tranferStats->getHandlerErrorData());
                     }
                     $resultCollection->offsetSet($url->getName(), new Result($url, $statusCode, $transferTime, $handlerError, $validatorResult, $validatorError));
                 }]);
             });
         }
     };
     $pool = new Pool($this->client, $requests(), ['concurrency' => 5]);
     $promise = $pool->promise();
     $promise->wait();
     return $resultCollection;
 }