Skip to content

Commit

Permalink
refact zip methods
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed Nov 14, 2017
1 parent 62010bd commit 5415191
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
18 changes: 10 additions & 8 deletions src/Ws/Services/BaseSunat.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Greenter\Model\Response\Error;
use Greenter\Ws\Reader\DomCdrReader;
use Greenter\Ws\Reader\XmlErrorReader;
use Greenter\Zip\ZipFactory;
use Greenter\Zip\ZipReader;
use Greenter\Zip\ZipWriter;

/**
* Class BaseSunat
Expand Down Expand Up @@ -78,7 +79,8 @@ protected function getErrorFromFault(\SoapFault $fault)
*/
protected function compress($filename, $xml)
{
return (new ZipFactory())->compress($filename, $xml);
return (new ZipWriter())
->compress($filename, $xml);
}

/**
Expand All @@ -87,11 +89,11 @@ protected function compress($filename, $xml)
*/
protected function extractResponse($zipContent)
{
$zip = new ZipFactory();
$xml = $zip->decompressXmlFile($zipContent);
$reader = new DomCdrReader();
$xml = (new ZipReader())
->decompressXmlFile($zipContent);

return $reader->getCdrResponse($xml);
return (new DomCdrReader())
->getCdrResponse($xml);
}

/**
Expand All @@ -100,8 +102,8 @@ protected function extractResponse($zipContent)
*/
protected function getMessageError($code)
{
$search = new XmlErrorReader();
$msg = $search->getMessageByCode(intval($code));
$msg = (new XmlErrorReader())
->getMessageByCode(intval($code));

return $msg;
}
Expand Down
1 change: 0 additions & 1 deletion src/Ws/Services/BillSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
class BillSender extends BaseSunat implements SenderInterface
{

/**
* @param string $filename
* @param string $content
Expand Down
17 changes: 1 addition & 16 deletions src/Zip/ZipFactory.php → src/Zip/ZipReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,10 @@
* Class ZipFactory
* @package Greenter\Zip
*/
final class ZipFactory
final class ZipReader
{
const UNZIP_FORMAT = 'Vsig/vver/vflag/vmeth/vmodt/vmodd/Vcrc/Vcsize/Vsize/vnamelen/vexlen';

/**
* Comprime el contenido del archivo con el nombre especifico y retorna el contenido del zip.
*
* @param string $filename
* @param string $content
* @return string
*/
public function compress($filename, $content)
{
$archive = new ZipFile();
$archive->addFile($content, $filename);

return $archive->file();
}

/**
* Retorna el contenido del primer xml dentro del zip.
*
Expand Down
17 changes: 14 additions & 3 deletions src/Zip/ZipFile.php → src/Zip/ZipWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Class ZipFile.
*/
class ZipFile
class ZipWriter
{
/**
* Array to store compressed data.
Expand Down Expand Up @@ -65,8 +65,6 @@ public function unix2DosTime($unixtime = 0)
| ($timearray['seconds'] >> 1);
}

// end of the 'unix2DosTime()' method

/**
* Adds "file" to archive.
*
Expand Down Expand Up @@ -148,5 +146,18 @@ public function file()
return $data.$header;
}

/**
* Comprime el contenido del archivo con el nombre especifico y retorna el contenido del zip.
*
* @param string $filename
* @param string $content
* @return string
*/
public function compress($filename, $content)
{
$this->addFile($content, $filename);

return $this->file();
}
// end of the 'file()' method
}
16 changes: 8 additions & 8 deletions tests/Ws/Zip/ZipFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace Tests\Greenter\Zip;

use Greenter\Zip\ZipFactory;
use Greenter\Zip\ZipFile;
use Greenter\Zip\ZipReader;
use Greenter\Zip\ZipWriter;

/**
* Class ZipFactoryTest
Expand All @@ -29,41 +29,41 @@ public function testCompressFile()
public function testDecompressLastFile()
{
$zipContent = $this->createZip();
$helper = new ZipFactory();
$helper = new ZipReader();
$content = $helper->decompressXmlFile($zipContent);

$this->assertEquals(self::DATA_XML, $content);
}

public function testUnixTime()
{
$zip = new ZipFile();
$zip = new ZipWriter();
$result = $zip->unix2DosTime(181233012);

$this->assertEquals(2162688, $result);
}

public function testInvalidZip()
{
$zip = new ZipFactory();
$zip = new ZipReader();
$res = $zip->decompressXmlFile('');

$this->assertEmpty($res);
}

public function testNotXmlZip()
{
$helper = new ZipFactory();
$helper = new ZipWriter();
$zip = $helper->compress('myFile.txt', 'TEST TEXT 1');

$res = $helper->decompressXmlFile($zip);
$res = (new ZipReader())->decompressXmlFile($zip);

$this->assertEmpty($res);
}

private function createZip()
{
$helper = new ZipFactory();
$helper = new ZipWriter();
$zip = $helper->compress('myFile.xml', self::DATA_XML);

return $zip;
Expand Down

0 comments on commit 5415191

Please sign in to comment.