Skip to content

Commit

Permalink
[#2] Implement new basic Envelope
Browse files Browse the repository at this point in the history
  • Loading branch information
kgilden committed Oct 11, 2014
1 parent 0966919 commit afa3465
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/Native/Envelope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/*
* This file is part of the DigiDoc package.
*
* (c) Kristen Gilden <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KG\DigiDoc\Native;

use KG\DigiDoc\EnvelopeInterface;

class Envelope implements EnvelopeInterface
{
/**
* @var \ZipArchive
*/
private $archive;

/**
* @var string
*/
private $path;

/**
* @param string $path
*/
public function __construct($path)
{
$this->path = $path;

$this->archive = new \ZipArchive();
if (true !== ($error = $this->archive->open($this->path))) {
// @todo better exception?
throw new \RuntimeException(sprintf('Failed to open archive "%s", ZipArchive code %d', $this->path, $error));
}
}

/**
* {@inheritDoc}
*/
public function getFiles()
{
$nonMetaNames = array();

foreach ($this->getAllFileNames() as $fileName) {
// Skip metadata files.
if (0 === strpos($fileName, 'META-INF/') || 'mimetype' === $fileName) {
continue;
}

$nonMetaNames[] = $fileName;
}

return new \ArrayIterator($this->convertNamesToFullPaths($nonMetaNames));
}

/**
* {@inheritDoc}
*/
public function getSignatures()
{
$signatureNames = array();

foreach ($this->getAllFileNames() as $fileName) {
if (preg_match('/^META-INF\/signatures\d+\.xml$/', $fileName)) {
$signatureNames[] = $fileName;
}
}

return new \ArrayIterator($this->convertNamesToFullPaths($signatureNames));
}

public function __destruct()
{
$this->archive->close();
}

/**
* @param array $names
*
* @return array
*/
private function convertNamesToFullPaths($names)
{
$paths = array();

foreach ($names as $name) {
$paths[] = sprintf('zip://%s#%s', $this->path, $name);
}

return $paths;
}

/**
* @return array
*/
private function getAllFileNames()
{
$fileNames = array();

for ($i = 0; $i < $this->archive->numFiles; $i++) {
$fileNames[] = $this->archive->getNameIndex($i);
}

return $fileNames;
}
}
63 changes: 63 additions & 0 deletions tests/Native/EnvelopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the DigiDoc package.
*
* (c) Kristen Gilden <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KG\DigiDoc\Tests\Envelope;

use KG\DigiDoc\Native\Envelope;

class EnvelopeTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
*/
public function testGetFilesFailsIfArchiveFailsToOpen()
{
$envelope = new Envelope('not-exists.bdoc');
}

public function testGetFilesReturnsIterator()
{
$envelope = new Envelope(__DIR__ . '/../fixtures/envelope.bdoc');
$this->assertInstanceOf('\Iterator', $envelope->getFiles());
}

public function testGetFilesNotContainsMetaFiles()
{
$path = __DIR__ . '/../fixtures/envelope.bdoc';

$envelope = new Envelope($path);
$expected = array(
'zip://' . $path . '#' . 'hello.txt',
'zip://' . $path . '#' . 'kitten.jpg',
);

$this->assertEquals($expected, iterator_to_array($envelope->getFiles()));
}

public function testGetSignaturesReturnsIterator()
{
$envelope = new Envelope(__DIR__ . '/../fixtures/envelope.bdoc');
$this->assertInstanceOf('\Iterator', $envelope->getSignatures());
}

public function testGetSignaturesContainsOnlySignatureFiles()
{
$path = __DIR__ . '/../fixtures/envelope.bdoc';

$envelope = new Envelope($path);
$expected = array(
'zip://' . $path . '#' . 'META-INF/signatures0.xml',
'zip://' . $path . '#' . 'META-INF/signatures1.xml',
);

$this->assertEquals($expected, iterator_to_array($envelope->getSignatures()));
}
}
Binary file added tests/fixtures/envelope.bdoc
Binary file not shown.
Empty file added tests/fixtures/hello.txt
Empty file.
Binary file added tests/fixtures/kitten.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit afa3465

Please sign in to comment.