Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from cashlink/fix-url-and-encoding
Browse files Browse the repository at this point in the history
Fixed download URL, follow redirects, convert file encoding from UTF-8 to ISO
  • Loading branch information
mensler authored Apr 4, 2019
2 parents 1894f32 + a8f9ed8 commit 7f4162b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 27 deletions.
4 changes: 2 additions & 2 deletions autoloader/index/0.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@
'malkusch\bav\undefinedattributeagencyexception' => '../classes/bank/exception/UndefinedAttributeAgencyException.php',
'malkusch\bav\undefinedfileparsercontextexception' => '../classes/dataBackend/file/fileParser/exception/UndefinedFileParserContextException.php',
'malkusch\bav\missingattributesdatabackendioexception' => '../classes/dataBackend/exception/MissingAttributesDataBackendIOException.php',
'malkusch\bav\fileparserioexception' => '../classes/dataBackend/file/fileParser/exception/FileParserIOException',
'malkusch\bav\parseexception' => '../classes/dataBackend/file/fileParser/exception/ParseException',
'malkusch\bav\fileparserioexception' => '../classes/dataBackend/file/fileParser/exception/FileParserIOException.php',
'malkusch\bav\parseexception' => '../classes/dataBackend/file/fileParser/exception/ParseException.php',
);
86 changes: 68 additions & 18 deletions classes/dataBackend/file/FileDataBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace malkusch\bav;

use LogicException;
use malkusch\index\FileExistsIOException;
use \malkusch\index\FixedSizeIndex;
use \malkusch\index\IndexException;
use malkusch\index\IOIndexException;

/**
* It uses the huge file from the Bundesbank and uses a binary search to find a row.
Expand Down Expand Up @@ -49,9 +52,11 @@ public function __construct($file = null)
$this->parser = new FileParser($file);
$this->fileUtil = new FileUtil();
}

/**
* @return FixedSizeIndex
* @throws FileExistsIOException
* @throws IOIndexException
*/
private function getIndex()
{
Expand Down Expand Up @@ -127,6 +132,18 @@ private function sortFile($file)
$this->fileUtil->safeRename($temp, $file);
}

/**
* Since 2019-03 the file is encoded in UTF-8. Convert the encoding to ISO-8859-1 so the file parser works with fixed line lengths.
*
* @param $file
* @throws EncodingException
* @throws UnsupportedEncodingException
*/
private function reEncodeFile($file) {
$contents = file_get_contents($file);
file_put_contents($file, utf8_decode($contents));
}

/**
* @see DataBackend::uninstall()
* @throws DataBackendIOException
Expand All @@ -140,8 +157,16 @@ public function uninstall()
}

/**
* @see DataBackend::install()
* @throws DataBackendIOException
* @throws DownloaderException
* @throws EncodingException
* @throws FileException
* @throws FileParserIOException
* @throws FileParserNotExistsException
* @throws FileValidatorException
* @throws URIPickerException
* @throws UnsupportedEncodingException
* @see DataBackend::install()
*/
public function install()
{
Expand All @@ -151,10 +176,16 @@ public function install()
/**
* This method works only if your PHP is compiled with cURL.
*
* @see DataBackend::update()
* @throws DataBackendIOException
* @throws FileException
* @throws DownloaderException
* @throws EncodingException
* @throws FileException
* @throws FileParserIOException
* @throws FileParserNotExistsException
* @throws FileValidatorException
* @throws URIPickerException
* @throws UnsupportedEncodingException
* @see DataBackend::update()
*/
public function update()
{
Expand Down Expand Up @@ -188,6 +219,9 @@ public function update()
// download file
$file = $downloader->downloadFile($url);

// convert encoding from UTF-8 to ISO-8859-15 (as needed by file parser)
$this->reEncodeFile($file);

// Validate file format.
$validator = new FileValidator();
$validator->validate($file);
Expand All @@ -205,9 +239,10 @@ public function update()
}

/**
* @throws DataBackendIOException
* @throws DataBackendException
* @return Bank[]
* @throws DataBackendException
* @throws DataBackendIOException
* @throws EncodingException
* @see DataBackend::getAllBanks()
*/
public function getAllBanks()
Expand Down Expand Up @@ -235,11 +270,11 @@ public function getAllBanks()
}

/**
* @throws DataBackendIOException
* @throws BankNotFoundException
* @param String $bankID
* @see DataBackend::getNewBank()
* @return Bank
* @throws EncodingException
* @throws DataBackendIOException
* @throws BankNotFoundException*@see DataBackend::getNewBank()
*/
public function getNewBank($bankID)
{
Expand All @@ -266,10 +301,14 @@ public function getNewBank($bankID)
}

/**
* @see DataBackend::getMainAgency()
* @param Bank $bank
* @return Agency
* @throws DataBackendException
* @throws DataBackendIOException
* @throws EncodingException
* @throws FileParserNotExistsException
* @throws NoMainAgencyException
* @return Agency
* @see DataBackend::getMainAgency()
*/
public function getMainAgency(Bank $bank)
{
Expand All @@ -286,7 +325,7 @@ public function getMainAgency(Bank $bank)
throw new NoMainAgencyException($bank);

} catch (UndefinedFileParserContextException $e) {
throw new \LogicException("Start and end should be defined.");
throw new LogicException("Start and end should be defined.");

} catch (FileParserIOException $e) {
throw new DataBackendIOException("Parser Exception at bank {$bank->getBankID()}");
Expand All @@ -298,10 +337,13 @@ public function getMainAgency(Bank $bank)
}

/**
* @see DataBackend::getAgenciesForBank()
* @throws DataBackendIOException
* @throws DataBackendException
* @param Bank $bank
* @return Agency[]
* @throws DataBackendException
* @throws DataBackendIOException
* @throws EncodingException
* @throws FileParserNotExistsException
* @see DataBackend::getAgenciesForBank()
*/
public function getAgenciesForBank(Bank $bank)
{
Expand All @@ -318,7 +360,7 @@ public function getAgenciesForBank(Bank $bank)
return $agencies;

} catch (UndefinedFileParserContextException $e) {
throw new \LogicException("Start and end should be defined.");
throw new LogicException("Start and end should be defined.");

} catch (FileParserIOException $e) {
throw new DataBackendIOException();
Expand All @@ -330,12 +372,16 @@ public function getAgenciesForBank(Bank $bank)
}

/**
* @param $bankID
* @return FileParserContext
* @throws EncodingException
* @throws FileParserIOException
* @throws FileParserNotExistsException
*/
private function defineContextInterval($bankID)
{
if (! isset($this->contextCache[$bankID])) {
throw new \LogicException("The contextCache object should exist!");
throw new LogicException("The contextCache object should exist!");

}
$context = $this->contextCache[$bankID];
Expand Down Expand Up @@ -400,9 +446,13 @@ public function isInstalled()
/**
* Returns bank agencies for a given BIC.
*
* @todo This method is inefficient. Add index based implementation.
* @param string $bic BIC
* @return Agency[]
* @throws DataBackendException
* @throws DataBackendIOException
* @throws EncodingException
* @throws UndefinedAttributeAgencyException
* @todo This method is inefficient. Add index based implementation.
*/
public function getBICAgencies($bic)
{
Expand Down
1 change: 1 addition & 0 deletions classes/dataBackend/file/download/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct()

}
curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, true);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions classes/dataBackend/file/fileParser/FileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ private function init()

}


$dummyLine = fgets($this->fp);
if (! $dummyLine) {
throw new FileParserIOException();
Expand Down Expand Up @@ -193,10 +192,11 @@ public function seekLine($line, $offset = 0)
}

/**
* @throws FileParserIOException
* @throws FileParserNotExistsException
* @param int $line
* @return string
* @throws EncodingException
* @throws FileParserIOException
* @throws FileParserNotExistsException
*/
public function readLine($line)
{
Expand All @@ -205,10 +205,11 @@ public function readLine($line)
}

/**
* @throws FileParserIOException
* @throws FileParserNotExistsException
* @param int $line
* @return string
* @throws EncodingException
* @throws FileParserIOException
* @throws FileParserNotExistsException
*/
public function getBankID($line)
{
Expand Down Expand Up @@ -249,9 +250,10 @@ public function __destruct()
}

/**
* @throws ParseException
* @param DataBackend $dataBackend
* @param string $line
* @return Bank
* @throws ParseException
*/
public function getBank(DataBackend $dataBackend, $line)
{
Expand All @@ -265,9 +267,10 @@ public function getBank(DataBackend $dataBackend, $line)
}

/**
* @throws ParseException
* @param Bank $bank
* @param string $line
* @return Agency
* @throws ParseException
*/
public function getAgency(Bank $bank, $line)
{
Expand Down

0 comments on commit 7f4162b

Please sign in to comment.