Skip to content

Commit

Permalink
Core logic for file system classes added
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberinferno committed Sep 21, 2019
1 parent e013c6e commit c376d58
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 58 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
}
],
"require": {
"php": ">=7.0"
"php": ">=7.0",
"ext-libxml": "*",
"ext-dom": "*",
"ext-simplexml": "*"
},
"require-dev": {
"phpunit/phpunit": "6.*"
"phpunit/phpunit": "6.*",
"mikey179/vfsstream": "^1.6"
},
"autoload": {
"psr-4": {
Expand Down
24 changes: 15 additions & 9 deletions src/Exceptions/FileBadFormatException.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: cyberinferno
* Date: 21-Sep-19
* Time: 1:14 PM
*/

namespace cyberinferno\Cabal\Helpers\Exceptions;


class FileBadFormatException
/**
* Class FileBadFormatException
*
* Exception thrown when a specified file is not in expected format
*
* @package cyberinferno\Cabal\Helpers\Exceptions
* @author Karthik P
*/
class FileBadFormatException extends \Exception
{

public function __construct($message = '', $code = 0, \Exception $previous = null) {
if (empty($message)) {
$message = 'Format of the specified file was bad and hence could not be read';
}
parent::__construct($message, $code, $previous);
}
}
24 changes: 15 additions & 9 deletions src/Exceptions/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: cyberinferno
* Date: 21-Sep-19
* Time: 1:06 PM
*/

namespace cyberinferno\Cabal\Helpers\Exceptions;


class FileNotFoundException
/**
* Class FileNotFoundException
*
* Exception thrown when a specified file was not found or could not be accessed
*
* @package cyberinferno\Cabal\Helpers\Exceptions
* @author Karthik P
*/
class FileNotFoundException extends \Exception
{

public function __construct($message = '', $code = 0, \Exception $previous = null) {
if (empty($message)) {
$message = 'File not found';
}
parent::__construct($message, $code, $previous);
}
}
70 changes: 63 additions & 7 deletions src/FileSystem/Client/CabalMsg.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
<?php
/**
* Created by PhpStorm.
* User: cyberinferno
* Date: 21-Sep-19
* Time: 1:29 PM
*/

namespace cyberinferno\Cabal\Helpers\FileSystem\Client;

use cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException;
use cyberinferno\Cabal\Helpers\FileSystem\File;

class CabalMsg
/**
* Class CabalMsg
*
* Helper class to load contents from cabal_msg.dec file
*
* @package cyberinferno\Cabal\Helpers\FileSystem\Client
* @author Karthik P
*/
class CabalMsg extends File
{
/**
* @var \SimpleXMLElement
*/
protected $_xmlObject;

/**
* Returns the XML object of the specified cabal_msg.dec file
*
* @return \SimpleXMLElement
*/
public function getXmlObject()
{
if (isset($this->_xmlObject) === false) {
libxml_use_internal_errors(true);
$domDocument = new \DOMDocument();
$domDocument->recover = true;
$domDocument->loadXML($this->getFileContents());
$this->_xmlObject = simplexml_load_string($domDocument->saveXML());
}
return $this->_xmlObject;
}

/**
* Returns array representation of attributes of the XML tree node attribute
* If $topLevelKey is sent only that node's attributes are returned
*
* WARNING: This method might take lot of time to complete when XML tree is large
*
* @param string $topLevelKey
* @return array
*/
public function getArray($topLevelKey = '')
{
$xmlObject = $this->getXmlObject();
if (!empty($topLevelKey)) {
return get_object_vars($xmlObject->$topLevelKey);
}
return get_object_vars($xmlObject);
}

/**
* @inheritdoc
*/
protected function validateFormat()
{
if (filesize($this->_filePath) == 0 || $this->getFileContents() === null) {
throw new FileBadFormatException();
}
$fileContents = $this->getFileContents();
if (substr($fileContents, 0, strlen('<cabal_message>')) !== '<cabal_message>') {
throw new FileBadFormatException();
}
}
}
69 changes: 62 additions & 7 deletions src/FileSystem/File.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,70 @@
<?php
/**
* Created by PhpStorm.
* User: cyberinferno
* Date: 21-Sep-19
* Time: 6:35 PM
*/

namespace cyberinferno\Cabal\Helpers\FileSystem;

use cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException;
use cyberinferno\Cabal\Helpers\Exceptions\FileNotFoundException;

class File
/**
* Class File
*
* Base class for all file system classes. Has basic file loading and validating methods
*
* @package cyberinferno\Cabal\Helpers\FileSystem
* @author Karthik P
*/
abstract class File
{
protected $_filePath;
protected $_fileContents;

/**
* File constructor.
* @param string $filePath
* @throws FileBadFormatException
* @throws FileNotFoundException
*/
public function __construct($filePath)
{
$this->_filePath = $filePath;
$this->validateFileExists();
$this->validateFormat();
}

/**
* Checks whether the specified file exists
*
* @throws FileNotFoundException
*/
protected function validateFileExists()
{
if (file_exists($this->_filePath) === false) {
throw new FileNotFoundException();
}
}

/**
* Checks whether the specified file isn't empty
*
* @throws FileBadFormatException
*/
protected function validateFormat()
{
if (filesize($this->_filePath) == 0 || $this->getFileContents() === null) {
throw new FileBadFormatException();
}
}

/**
* Loads the file contents if not already loaded and returns the same
*
* @return null|string
*/
public function getFileContents()
{
if (isset($this->_fileContents) === false) {
$this->_fileContents = file_get_contents($this->_filePath);
}
return $this->_fileContents;
}
}
40 changes: 33 additions & 7 deletions src/FileSystem/Server/WorldDrop.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
<?php
/**
* Created by PhpStorm.
* User: cyberinferno
* Date: 22-Sep-19
* Time: 12:32 AM
*/

namespace cyberinferno\Cabal\Helpers\FileSystem\Server;

use cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException;
use cyberinferno\Cabal\Helpers\FileSystem\File;

class WorldDrop
/**
* Class WorldDrop
*
* Helper class to load contents from World_drop.scp file
*
* @package cyberinferno\Cabal\Helpers\FileSystem\Server
*/
class WorldDrop extends File
{
/**
* Returns array representation of the World_dop.scp file
*
* @return array
*/
public function getArray()
{
$fileContents = $this->getFileContents();
return explode("\n", $fileContents);
}

/**
* @inheritdoc
*/
protected function validateFormat()
{
if (filesize($this->_filePath) == 0 || $this->getFileContents() === null) {
throw new FileBadFormatException();
}
$fileContents = $this->getFileContents();
if (substr($fileContents, 0, strlen('[WorldDrop]')) !== '[WorldDrop]') {
throw new FileBadFormatException();
}
}
}
70 changes: 62 additions & 8 deletions tests/CabalMsgTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
<?php
/**
* Created by PhpStorm.
* User: cyberinferno
* Date: 21-Sep-19
* Time: 8:20 PM
*/

class CabalMsgTest

class CabalMsgTest extends \PHPUnit\Framework\TestCase
{
public $fileSystem;

public function setUp()
{
$directory = [
'client' => [
'cabal_msg.dec' => '<cabal_message><version index="3" /></cabal_message>',
'cabal_msg_invalid.dec' => '<cabal_message'
]
];
$this->fileSystem = \org\bovigo\vfs\vfsStream::setup('root', 444, $directory);
}

public function testIsThereAnySyntaxError()
{
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg($this->fileSystem->url() . '/client/cabal_msg.dec');
$this->assertTrue(is_object($myClass));
unset($myClass);
}

/**
* @expectedException \cyberinferno\Cabal\Helpers\Exceptions\FileNotFoundException
*/
public function testFileNotFoundExceptionIsThrownWhenNoFile()
{
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg(
$this->fileSystem->url() . '/unknown.dec'
);
unset($myClass);
}

/**
* @expectedException \cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException
*/
public function testFileBadFormatExceptionIsThrownWhenNoFile()
{
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg(
$this->fileSystem->url() . '/client/cabal_msg_invalid.dec'
);
unset($myClass);
}

public function testGetXmlObject()
{
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg(
$this->fileSystem->url() . '/client/cabal_msg.dec'
);
$result = $myClass->getXmlObject();
$this->assertTrue($result instanceof \SimpleXMLElement);
}

public function testGetArray()
{
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg(
$this->fileSystem->url() . '/client/cabal_msg.dec'
);
$result = $myClass->getArray();
$this->assertTrue(is_array($result));
$result = $myClass->getArray('version');
$this->assertTrue(!empty($result));
}
}
2 changes: 1 addition & 1 deletion tests/GameConstantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public function testListAuras()
$myClass = new cyberinferno\Cabal\Helpers\GameConstants();
$result = $myClass::listAuras();
$this->assertTrue(is_array($result));
$this->assertEquals(6, count($result));
$this->assertEquals(7, count($result));
}
}
Loading

0 comments on commit c376d58

Please sign in to comment.