Removed redundant feature from AntiTempmailRepo class

This commit is contained in:
ErickSkrauch 2016-04-30 12:24:24 +03:00
parent 3131ece3b3
commit 676d2bee6d
2 changed files with 45 additions and 24 deletions

View File

@ -10,24 +10,25 @@ use Exception;
class AntiTempmailRepo implements LoaderInterface class AntiTempmailRepo implements LoaderInterface
{ {
/** /**
* @var array with custom paths to find data file * 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).
*
* @return array
*/ */
private $customPaths; private $searchPaths = [
__DIR__ . '/../../../anti-tempmail-repo/data.json',
/** __DIR__ . '/../../vendor/ely/anti-tempmail-repo/data.json',
* @param array $customPaths ];
*/
public function __construct(array $customPaths = [])
{
$this->customPaths = $customPaths;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function load() public function load()
{ {
$paths = $this->getPaths(); $paths = $this->getSearchPaths();
$dataPath = null; $dataPath = null;
foreach($paths as $path) { foreach($paths as $path) {
if (file_exists($path)) { if (file_exists($path)) {
@ -49,19 +50,18 @@ class AntiTempmailRepo implements LoaderInterface
} }
/** /**
* 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).
*
* @return array * @return array
*/ */
protected function getPaths() public function getSearchPaths()
{ {
return array_merge($this->customPaths, [ return $this->searchPaths;
__DIR__ . '/../../../anti-tempmail-repo/data.json', }
__DIR__ . '/../../vendor/ely/anti-tempmail-repo/data.json',
]); /**
* @param array|string $path
*/
public function setSearchPaths($path)
{
$this->searchPaths = (array)$path;
} }
} }

View File

@ -22,11 +22,32 @@ class AntiTempmailRepoTest extends \PHPUnit_Framework_TestCase
$loader = new AntiTempmailRepoWithInvalidJson(); $loader = new AntiTempmailRepoWithInvalidJson();
$loader->load(); $loader->load();
} }
public function testGetSearchPaths()
{
$loader = new AntiTempmailRepo();
$this->assertTrue(is_array($loader->getSearchPaths()));
}
public function testSetSearchPaths()
{
$path = __DIR__ . '/test.json';
$loader = new AntiTempmailRepo();
$loader->setSearchPaths([$path]);
$this->assertTrue(is_array($loader->getSearchPaths()));
$this->assertEquals([$path], $loader->getSearchPaths());
$loader = new AntiTempmailRepo();
$loader->setSearchPaths($path);
$this->assertTrue(is_array($loader->getSearchPaths()));
$this->assertEquals([$path], $loader->getSearchPaths());
}
} }
class AntiTempmailRepoWithWrongPaths extends AntiTempmailRepo class AntiTempmailRepoWithWrongPaths extends AntiTempmailRepo
{ {
protected function getPaths() public function getSearchPaths()
{ {
return [ return [
__DIR__ . '/virtual_reality.json', __DIR__ . '/virtual_reality.json',
@ -36,7 +57,7 @@ class AntiTempmailRepoWithWrongPaths extends AntiTempmailRepo
class AntiTempmailRepoWithInvalidJson extends AntiTempmailRepo class AntiTempmailRepoWithInvalidJson extends AntiTempmailRepo
{ {
protected function getPaths() public function getSearchPaths()
{ {
return [ return [
__DIR__ . '/AntiTempmailRepoTest.php', __DIR__ . '/AntiTempmailRepoTest.php',