Skip to content

Commit

Permalink
Merge pull request #1 from dfridrich/1.0
Browse files Browse the repository at this point in the history
phpcsfixer
  • Loading branch information
dfridrich committed Sep 8, 2015
2 parents 6f847e6 + 4fed834 commit 308e908
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 63 deletions.
62 changes: 26 additions & 36 deletions src/Ares.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
use Defr\Ares\TaxRecord;

/**
* Class Ares
* @package Defr
* Class Ares.
*
* @author Dennis Fridrich <[email protected]>
*/
class Ares
{

const URL_BAS = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=%s';
const URL_RES = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_res.cgi?ICO=%s';
const URL_TAX = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/ares_es.cgi?ico=%s';
Expand All @@ -41,30 +40,29 @@ class Ares
*/
public function __construct($cacheDir = null, $debug = false)
{

if ($cacheDir === null) {
$cacheDir = sys_get_temp_dir();
}

$this->cacheDir = $cacheDir . '/defr/ares';
$this->cacheDir = $cacheDir.'/defr/ares';
$this->debug = $debug;

// Create cache dirs if they doesn't exist
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0777, true);
}

}

/**
* @param $id
*
* @return AresRecord
*
* @throws \InvalidArgumentException
* @throws Ares\AresException
*/
public function findByIdentificationNumber($id)
{

$id = Lib::toInteger($id);

if (!is_int($id)) {
Expand All @@ -74,14 +72,12 @@ public function findByIdentificationNumber($id)
// Sestaveni URL
$url = sprintf(self::URL_BAS, $id);

$cachedFileName = $id . '_' . date($this->cacheStrategy) . '.php';
$cachedFile = $this->cacheDir . '/bas_' . $cachedFileName;
$cachedRawFile = $this->cacheDir . '/bas_raw_' . $cachedFileName;
$cachedFileName = $id.'_'.date($this->cacheStrategy).'.php';
$cachedFile = $this->cacheDir.'/bas_'.$cachedFileName;
$cachedRawFile = $this->cacheDir.'/bas_raw_'.$cachedFileName;

if (!is_file($cachedFile)) {

try {

$aresRequest = file_get_contents($url);
if ($this->debug) {
file_put_contents($cachedRawFile, $aresRequest);
Expand All @@ -94,7 +90,6 @@ public function findByIdentificationNumber($id)
$elements = $data->children($ns['D'])->VBAS;

if (strval($elements->ICO) == $id) {

$record = new AresRecord();

$record->setCompanyId($id);
Expand All @@ -110,13 +105,12 @@ public function findByIdentificationNumber($id)
}

if (strval($elements->AA->NCO)) {
$record->setTown(strval($elements->AA->N . ' - ' . strval($elements->AA->NCO)));
$record->setTown(strval($elements->AA->N.' - '.strval($elements->AA->NCO)));
} else {
$record->setTown(strval($elements->AA->N));
}

$record->setZip(strval($elements->AA->PSC));

} else {
throw new AresException('IČ firmy nebylo nalezeno.');
}
Expand All @@ -128,27 +122,25 @@ public function findByIdentificationNumber($id)
}

file_put_contents($cachedFile, serialize($record));

} else {

/** @var AresRecord $record */
$record = unserialize(file_get_contents($cachedFile));

}

return $record;

}

/**
* @param $id
*
* @return AresRecord
*
* @throws \InvalidArgumentException
* @throws Ares\AresException
*/
public function findInResById($id)
{

$id = Lib::toInteger($id);

if (!is_int($id)) {
Expand All @@ -158,9 +150,9 @@ public function findInResById($id)
// Sestaveni URL
$url = sprintf(self::URL_RES, $id);

$cachedFileName = $id . '_' . date($this->cacheStrategy) . '.php';
$cachedFile = $this->cacheDir . '/res_' . $cachedFileName;
$cachedRawFile = $this->cacheDir . '/res_raw_' . $cachedFileName;
$cachedFileName = $id.'_'.date($this->cacheStrategy).'.php';
$cachedFile = $this->cacheDir.'/res_'.$cachedFileName;
$cachedRawFile = $this->cacheDir.'/res_raw_'.$cachedFileName;

if (!is_file($cachedFile)) {
try {
Expand Down Expand Up @@ -201,18 +193,18 @@ public function findInResById($id)
}

return $record;

}

/**
* @param $id
*
* @return TaxRecord|mixed
*
* @throws \InvalidArgumentException
* @throws \Exception
*/
public function findVatById($id)
{

$id = Lib::toInteger($id);

if (!is_int($id)) {
Expand All @@ -222,9 +214,9 @@ public function findVatById($id)
// Sestaveni URL
$url = sprintf(self::URL_TAX, $id);

$cachedFileName = $id . '_' . date($this->cacheStrategy) . '.php';
$cachedFile = $this->cacheDir . '/tax_' . $cachedFileName;
$cachedRawFile = $this->cacheDir . '/tax_raw_' . $cachedFileName;
$cachedFileName = $id.'_'.date($this->cacheStrategy).'.php';
$cachedFile = $this->cacheDir.'/tax_'.$cachedFileName;
$cachedRawFile = $this->cacheDir.'/tax_raw_'.$cachedFileName;

if (!is_file($cachedFile)) {
try {
Expand Down Expand Up @@ -257,19 +249,19 @@ public function findVatById($id)
}

return $record;

}

/**
* @param $name
* @param null $city
*
* @return array|AresRecords
*
* @throws \InvalidArgumentException
* @throws \Exception
*/
public function findByName($name, $city = null)
{

if (strlen($name) < 3) {
throw new \InvalidArgumentException('Zadejte minimálně tři znaky pro hledání.');
}
Expand All @@ -280,9 +272,9 @@ public function findByName($name, $city = null)
urlencode(Lib::stripDiacritics($city))
);

$cachedFileName = date($this->cacheStrategy) . '_' . md5($name . $city) . '.php';
$cachedFile = $this->cacheDir . '/find_' . $cachedFileName;
$cachedRawFile = $this->cacheDir . '/find_raw_' . $cachedFileName;
$cachedFileName = date($this->cacheStrategy).'_'.md5($name.$city).'.php';
$cachedFile = $this->cacheDir.'/find_'.$cachedFileName;
$cachedRawFile = $this->cacheDir.'/find_raw_'.$cachedFileName;

if (!is_file($cachedFile)) {
try {
Expand Down Expand Up @@ -324,7 +316,6 @@ public function findByName($name, $city = null)
}

return $records;

}

/**
Expand All @@ -336,11 +327,10 @@ public function setCacheStrategy($cacheStrategy)
}

/**
* @param boolean $debug
* @param bool $debug
*/
public function setDebug($debug)
{
$this->debug = $debug;
}

}
}
7 changes: 3 additions & 4 deletions src/Ares/AresException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Defr\Ares;

/**
* Class AresException
* @package Defr\Ares
* Class AresException.
*
* @author Dennis Fridrich <[email protected]>
*/
class AresException extends \Exception
{

}
}
12 changes: 5 additions & 7 deletions src/Ares/AresRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Defr\Ares;

/**
* Class AresRecord
* @package Defr\Ares
* Class AresRecord.
*
* @author Dennis Fridrich <[email protected]>
*/
class AresRecord
{

public $companyId;
public $taxId;
public $companyName;
Expand Down Expand Up @@ -72,10 +71,10 @@ public function setStreetOrientationNumber($streetOrientationNumber)
*/
public function getStreetWithNumbers()
{
return $this->street . ' '
. ($this->streetOrientationNumber
return $this->street.' '
.($this->streetOrientationNumber
?
$this->streetHouseNumber . '/' . $this->streetOrientationNumber
$this->streetHouseNumber.'/'.$this->streetOrientationNumber
:
$this->streetHouseNumber);
}
Expand Down Expand Up @@ -167,5 +166,4 @@ public function getZip()
{
return $this->zip;
}

}
21 changes: 10 additions & 11 deletions src/Ares/AresRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace Defr\Ares;

/**
* Class AresRecords
* @package Defr\Ares
* Class AresRecords.
*
* @author Dennis Fridrich <[email protected]>
*/
class AresRecords implements \ArrayAccess, \IteratorAggregate
{
public $array = array();

public $array = Array();

function offsetExists($offset)
public function offsetExists($offset)
{
if (isset($this->array[$offset])) {
return true;
Expand All @@ -23,9 +22,10 @@ function offsetExists($offset)

/**
* @param mixed $offset
*
* @return bool|AresRecord
*/
function offsetGet($offset)
public function offsetGet($offset)
{
if ($this->offsetExists($offset)) {
return $this->array[$offset];
Expand All @@ -34,7 +34,7 @@ function offsetGet($offset)
}
}

function offsetSet($offset, $value)
public function offsetSet($offset, $value)
{
if ($offset) {
$this->array[$offset] = $value;
Expand All @@ -43,14 +43,13 @@ function offsetSet($offset, $value)
}
}

function offsetUnset($offset)
public function offsetUnset($offset)
{
unset($this->array[$offset]);
}

function getIterator()
public function getIterator()
{
return new \ArrayIterator($this->array);
}

}
}
8 changes: 3 additions & 5 deletions src/Ares/TaxRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Defr\Ares;

/**
* Class TaxRecord
* @package Defr\Ares
* Class TaxRecord.
*
* @author Dennis Fridrich <[email protected]>
*/
class TaxRecord
{

public $taxId = null;

/**
Expand All @@ -27,5 +26,4 @@ public function __toString()
{
return strval($this->taxId);
}

}
}

0 comments on commit 308e908

Please sign in to comment.