Static Loader class was refactored into AntiTempmailRepo class.

Added LoaderInterface.
Added fromLoader creator for Storage.
This commit is contained in:
ErickSkrauch 2016-04-29 01:53:51 +03:00
parent b2b8a0438f
commit 7b82e48cdd
7 changed files with 150 additions and 85 deletions

View File

@ -1,38 +0,0 @@
<?php
namespace Ely\TempMailBuster;
use Exception;
class Loader
{
public static function getPaths()
{
return [
__DIR__ . '/../vendor/ely/anti-tempmail-repo/data.json',
__DIR__ . '/../../anti-tempmail-repo/data.json',
];
}
public static function load()
{
$paths = static::getPaths();
$dataPath = null;
foreach($paths as $path) {
if (file_exists($path)) {
$dataPath = $path;
break;
}
}
if ($dataPath === null) {
throw new Exception('Cannot find data file. Please check getPaths() implementation.');
}
$data = json_decode(file_get_contents($dataPath), true);
if (!is_array($data)) {
throw new Exception('Cannot decode json from data file.');
}
return $data;
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Ely\TempMailBuster\Loader;
use Ely\TempMailBuster\LoaderInterface;
use Exception;
/**
* Class-wrapper for https://github.com/elyby/anti-tempmail-repo data file
*/
class AntiTempmailRepo implements LoaderInterface
{
/**
* @var array with custom paths to find data file
*/
private $customPaths;
/**
* @param array $customPaths
*/
public function __construct(array $customPaths = [])
{
$this->customPaths = $customPaths;
}
/**
* @inheritdoc
*/
public function load()
{
$paths = $this->getPaths();
$dataPath = null;
foreach($paths as $path) {
if (file_exists($path)) {
$dataPath = $path;
break;
}
}
if ($dataPath === null) {
throw new Exception('Cannot find data file. Please check getPaths() implementation.');
}
$data = json_decode(file_get_contents($dataPath), true);
if (!is_array($data)) {
throw new Exception('Cannot decode json from data file.');
}
return $data;
}
/**
* Generates file path array to data repository.
*
* Default paths are used in following cases:
* 1. if this library and data repository are included in project as composer dependencies;
* 2. if this library is deployed for development (data repository included as composer dependency to this library).
*
* @see Loader::customPath for custom data repository implementation
*
* @return array
*/
protected function getPaths()
{
return array_merge($this->customPaths, [
__DIR__ . '/../../../anti-tempmail-repo/data.json',
__DIR__ . '/../../vendor/ely/anti-tempmail-repo/data.json',
]);
}
}

12
src/LoaderInterface.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace Ely\TempMailBuster;
interface LoaderInterface
{
/**
* Load data from some source and return array with strings
*
* @return array
*/
public function load();
}

View File

@ -43,4 +43,15 @@ class Storage implements StorageInterface
return $this; return $this;
} }
/**
* Create new Storage object based on data, provided by passed loader
*
* @param LoaderInterface $loader
* @return static
*/
public static function fromLoader(LoaderInterface $loader)
{
return new static($loader->load());
}
} }

View File

@ -0,0 +1,45 @@
<?php
namespace Ely\TempMailBuster\Loader;
class AntiTempmailRepoTest extends \PHPUnit_Framework_TestCase
{
public function testLoad()
{
$loader = new AntiTempmailRepo();
$this->assertTrue(is_array($loader->load()));
}
public function testLoadExceptionWrongPaths()
{
$this->expectException('Exception');
$loader = new AntiTempmailRepoWithWrongPaths();
$loader->load();
}
public function testLoadExceptionInvalidJson()
{
$this->expectException('Exception');
$loader = new AntiTempmailRepoWithInvalidJson();
$loader->load();
}
}
class AntiTempmailRepoWithWrongPaths extends AntiTempmailRepo
{
protected function getPaths()
{
return [
__DIR__ . '/virtual_reality.json',
];
}
}
class AntiTempmailRepoWithInvalidJson extends AntiTempmailRepo
{
protected function getPaths()
{
return [
__DIR__ . '/AntiTempmailRepoTest.php',
];
}
}

View File

@ -1,47 +0,0 @@
<?php
namespace Ely\TempMailBuster;
class LoaderTest extends \PHPUnit_Framework_TestCase
{
public function testGetPaths()
{
$this->assertTrue(is_array(Loader::getPaths()));
}
public function testLoad()
{
$this->assertTrue(is_array(Loader::load()));
}
public function testLoadExceptionWrongPaths()
{
$this->expectException('Exception');
LoaderWithWrongPaths::load();
}
public function testLoadExceptionInvalidJson()
{
$this->expectException('Exception');
LoaderWithInvalidJson::load();
}
}
class LoaderWithWrongPaths extends Loader
{
public static function getPaths()
{
return [
__DIR__ . '/virtual_reality.json',
];
}
}
class LoaderWithInvalidJson extends Loader
{
public static function getPaths()
{
return [
__DIR__ . '/LoaderTest.php',
];
}
}

View File

@ -26,4 +26,17 @@ class StorageTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($storage, $storage->appendItems('item2')); $this->assertEquals($storage, $storage->appendItems('item2'));
$this->assertEquals(['item1', 'item2'], $storage->getItems()); $this->assertEquals(['item1', 'item2'], $storage->getItems());
} }
public function testFromLoader()
{
$this->assertInstanceOf('Ely\TempmailBuster\Storage', Storage::fromLoader(new SimpleLoader()));
}
}
class SimpleLoader implements LoaderInterface
{
public function load()
{
return ['foo', 'bar'];
}
} }