diff --git a/library/Businessprocess/Storage/LegacyStorage.php b/library/Businessprocess/Storage/LegacyStorage.php index a40704e..affa341 100644 --- a/library/Businessprocess/Storage/LegacyStorage.php +++ b/library/Businessprocess/Storage/LegacyStorage.php @@ -68,7 +68,7 @@ class LegacyStorage extends Storage foreach ($this->listAllProcessNames() as $name) { $meta = $this->loadMetadata($name); - if (! $meta->permissionsAreSatisfied()) { + if (! $meta->canRead()) { continue; } @@ -103,28 +103,26 @@ class LegacyStorage extends Storage return preg_split('/\s*,\s*/', $string, -1, PREG_SPLIT_NO_EMPTY); } - protected function readHeader($file) + protected function readHeader($file, Metadata $metadata) { $fh = fopen($file, 'r'); $cnt = 0; - $meta = new Metadata(); while ($cnt < 15 && false !== ($line = fgets($fh))) { $cnt++; - $this->parseHeaderLine($line, $meta); + $this->parseHeaderLine($line, $metadata); } fclose($fh); - return $meta; + return $metadata; } - protected function readHeaderString($string) + protected function readHeaderString($string, Metadata $metadata) { - $meta = new Metadata(); foreach (preg_split('/\n/', $string) as $line) { - $this->parseHeaderLine($line, $meta); + $this->parseHeaderLine($line, $metadata); } - return $meta; + return $metadata; } protected function emptyHeader() @@ -142,11 +140,11 @@ class LegacyStorage extends Storage ); } - protected function parseHeaderLine($line, Metadata $meta) + protected function parseHeaderLine($line, Metadata $metadata) { if (preg_match('/^\s*#\s+(.+?)\s*:\s*(.+)$/', $line, $m)) { - if ($meta->hasKey($m[1])) { - $meta->set($m[1], $m[2]); + if ($metadata->hasKey($m[1])) { + $metadata->set($m[1], $m[2]); } } } @@ -221,7 +219,7 @@ class LegacyStorage extends Storage $bp = new BusinessProcess(); $bp->setName($name); $this->parseString($string, $bp); - $bp->setMetadata($this->readHeaderString($string)); + $this->readHeaderString($string, $bp->getMetadata()); return $bp; } @@ -258,22 +256,13 @@ class LegacyStorage extends Storage return false; } - return $this->loadMetaFromFile($file)->permissionsAreSatisfied(); + return $this->loadMetadata($name)->canRead(); } public function loadMetadata($name) { - return $this->loadMetaFromFile($this->getFilename($name)); - } - - public function loadMetadataFromString($string) - { - return $this->readHeaderString($string); - } - - public function loadMetaFromFile($filename) - { - return $this->readHeader($filename); + $metadata = new Metadata($name); + return $this->readHeader($this->getFilename($name), $metadata); } /** @@ -284,7 +273,7 @@ class LegacyStorage extends Storage { // TODO: do not open twice, this is quick and dirty based on existing code $file = $this->currentFilename = $this->getFilename($name); - $bp->setMetadata($this->readHeader($file)); + $this->readHeader($file, $bp->getMetadata()); } protected function parseFile($name, $bp) diff --git a/library/Businessprocess/Storage/Storage.php b/library/Businessprocess/Storage/Storage.php index 89e1a7f..7d8ccb6 100644 --- a/library/Businessprocess/Storage/Storage.php +++ b/library/Businessprocess/Storage/Storage.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Businessprocess\Storage; use Icinga\Data\ConfigObject; use Icinga\Module\Businessprocess\BusinessProcess; +use Icinga\Module\Businessprocess\Metadata; abstract class Storage { @@ -48,4 +49,10 @@ abstract class Storage * @return bool Whether the process has been deleted */ abstract public function deleteProcess($name); + + /** + * @param string $name + * @return Metadata + */ + abstract public function loadMetadata($name); } diff --git a/test/php/library/Businessprocess/Storage/LegacyStorageTest.php b/test/php/library/Businessprocess/Storage/LegacyStorageTest.php index 77adacf..b0254f4 100644 --- a/test/php/library/Businessprocess/Storage/LegacyStorageTest.php +++ b/test/php/library/Businessprocess/Storage/LegacyStorageTest.php @@ -31,12 +31,12 @@ class LegacyStorageTest extends BaseTestCase $keys = array_keys($this->makeInstance()->listProcesses()); sort($keys); $this->assertEquals( - $keys, array( 'broken_wrong-operator', 'simple_with-header', 'simple_without-header', - ) + ), + $keys ); } @@ -45,12 +45,12 @@ class LegacyStorageTest extends BaseTestCase $keys = array_values($this->makeInstance()->listProcesses()); sort($keys); $this->assertEquals( - $keys, array( 'Simple with header (simple_with-header)', 'broken_wrong-operator', 'simple_without-header', - ) + ), + $keys ); } @@ -104,6 +104,14 @@ class LegacyStorageTest extends BaseTestCase ); } + public function testTitleCanBeReadFromConfig() + { + $this->assertEquals( + 'Simple with header', + $this->makeInstance()->loadProcess('simple_with-header')->getMetadata()->get('Title') + ); + } + public function testAConfiguredLoopCanBeParsed() { $this->assertInstanceOf(