Author: Thomas Müller (t_mueller_stolzenhain@yahoo.de)
Inheritance: extends DomainException
 /**
  *
  */
 public function testHttpError()
 {
     /** @var \BrowscapPHP\Exception\FetcherException $exception */
     $exception = FetcherException::httpError('http://example.org', 'Uri not reachable');
     self::assertInstanceOf('\\BrowscapPHP\\Exception\\FetcherException', $exception);
     self::assertSame('Could not fetch HTTP resource "http://example.org": Uri not reachable', $exception->getMessage());
 }
 /**
  * fetches a remote file, parses it and writes the result into the cache
  *
  * if the local stored information are in the same version as the remote data no actions are
  * taken
  *
  * @param string $remoteFile The code for the remote file to load
  *
  * @throws \BrowscapPHP\Exception\FileNotFoundException
  * @throws \BrowscapPHP\Helper\Exception
  * @throws \BrowscapPHP\Exception\FetcherException
  */
 public function update($remoteFile = IniLoader::PHP_INI)
 {
     $this->getLogger()->debug('started fetching remote file');
     if (null === ($cachedVersion = $this->checkUpdate())) {
         // no newer version available
         return;
     }
     $uri = (new IniLoader())->setRemoteFilename($remoteFile)->getRemoteIniUrl();
     /** @var \Psr\Http\Message\ResponseInterface $response */
     $response = $this->getClient()->get($uri, ['connect_timeout' => $this->connectTimeout]);
     if ($response->getStatusCode() !== 200) {
         throw new FetcherException('an error occured while fetching remote data from URI ' . $uri . ': StatusCode was ' . $response->getStatusCode());
     }
     try {
         $content = $response->getBody()->getContents();
     } catch (\Exception $e) {
         throw new FetcherException('an error occured while fetching remote data', 0, $e);
     }
     if (empty($content)) {
         $error = error_get_last();
         throw FetcherException::httpError($uri, $error['message']);
     }
     $this->getLogger()->debug('finished fetching remote file');
     $converter = new Converter($this->getLogger(), $this->getCache());
     $this->storeContent($converter, $content, $cachedVersion);
 }
Example #3
0
 /**
  * fetches a remote file, parses it and writes the result into the cache
  *
  * if the local stored information are in the same version as the remote data no actions are
  * taken
  *
  * @param string      $remoteFile The code for the remote file to load
  * @param string|null $buildFolder
  * @param int|null    $buildNumber
  *
  * @throws \BrowscapPHP\Exception\FileNotFoundException
  * @throws \BrowscapPHP\Helper\Exception
  * @throws \BrowscapPHP\Exception\FetcherException
  */
 public function update($remoteFile = IniLoader::PHP_INI, $buildFolder = null, $buildNumber = null)
 {
     $this->getLogger()->debug('started fetching remote file');
     $converter = new Converter($this->getLogger(), $this->getCache());
     if (class_exists('\\Browscap\\Browscap')) {
         $resourceFolder = 'vendor/browscap/browscap/resources/';
         if (null === $buildNumber) {
             $buildNumber = (int) file_get_contents('vendor/browscap/browscap/BUILD_NUMBER');
         }
         if (null === $buildFolder) {
             $buildFolder = 'resources';
         }
         $buildFolder .= '/browscap-ua-test-' . $buildNumber;
         $iniFile = $buildFolder . '/full_php_browscap.ini';
         mkdir($buildFolder, 0777, true);
         $writerCollectionFactory = new PhpWriterFactory();
         $writerCollection = $writerCollectionFactory->createCollection($this->getLogger(), $buildFolder);
         $buildGenerator = new BuildGenerator($resourceFolder, $buildFolder);
         $buildGenerator->setLogger($this->getLogger())->setCollectionCreator(new CollectionCreator())->setWriterCollection($writerCollection)->run($buildNumber, false);
         $converter->setVersion($buildNumber)->storeVersion()->convertFile($iniFile);
         $filesystem = new Filesystem();
         $filesystem->remove($buildFolder);
     } else {
         if (null === ($cachedVersion = $this->checkUpdate($remoteFile))) {
             // no newer version available
             return;
         }
         $this->getLoader()->setRemoteFilename($remoteFile)->setOptions($this->options)->setLogger($this->getLogger());
         try {
             $content = $this->getLoader()->load();
         } catch (Helper\Exception $e) {
             throw new FetcherException('an error occured while loading remote data', 0, $e);
         }
         if (false === $content) {
             $internalLoader = $this->getLoader()->getLoader();
             $error = error_get_last();
             throw FetcherException::httpError($internalLoader->getUri(), $error['message']);
         }
         $this->getLogger()->debug('finished fetching remote file');
         $this->storeContent($converter, $content, $cachedVersion);
     }
 }