mirror of
https://github.com/elyby/php-tempmailbuster.git
synced 2024-11-28 09:42:25 +05:30
Refactored Storage to be just Storage (not blacklist)
Implemented whitelist functionality
This commit is contained in:
parent
afad6a5b09
commit
ca7e357b68
@ -4,45 +4,45 @@ namespace Ely\TempMailBuster;
|
||||
class Storage
|
||||
{
|
||||
/**
|
||||
* @var array of string, which contains masks for temp mail services
|
||||
* @var array of strings, which contains masks for temp mail services
|
||||
*/
|
||||
private $blacklist;
|
||||
private $items;
|
||||
|
||||
/**
|
||||
* @param array $blacklist
|
||||
* @param array $items
|
||||
*/
|
||||
public function __construct(array $blacklist = [])
|
||||
public function __construct(array $items = [])
|
||||
{
|
||||
$this->blacklist = $blacklist;
|
||||
$this->items = $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array with current blacklist
|
||||
* @return array with current items
|
||||
*/
|
||||
public function getBlacklist()
|
||||
public function getItems()
|
||||
{
|
||||
return $this->blacklist;
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $items item or items, that will be merged to blacklist
|
||||
* @param array $items override current items with passed values
|
||||
* @return static
|
||||
*/
|
||||
public function appendToBlacklist($items)
|
||||
public function setItems(array $items)
|
||||
{
|
||||
$items = (array)$items;
|
||||
$this->blacklist = array_merge($this->blacklist, $items);
|
||||
|
||||
$this->items = $items;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $items override current blacklist with passed values
|
||||
* @param string|array $items item or items, that will be merged to items
|
||||
* @return static
|
||||
*/
|
||||
public function setBlacklist(array $items)
|
||||
public function append($items)
|
||||
{
|
||||
$this->blacklist = $items;
|
||||
$items = (array)$items;
|
||||
$this->items = array_merge($this->items, $items);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -6,22 +6,23 @@ class TempMailBuster
|
||||
/**
|
||||
* @var Storage
|
||||
*/
|
||||
private $storage;
|
||||
private $primaryStorage;
|
||||
/**
|
||||
* @var Storage|null
|
||||
*/
|
||||
private $secondaryStorage;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isWhitelistMode = false;
|
||||
|
||||
/**
|
||||
* @param Storage $storage
|
||||
*/
|
||||
public function __construct(Storage $storage)
|
||||
public function __construct(Storage $primaryStorage, Storage $secondaryStorage = null)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Storage
|
||||
*/
|
||||
public function getStorage()
|
||||
{
|
||||
return $this->storage;
|
||||
$this->primaryStorage = $primaryStorage;
|
||||
$this->secondaryStorage = $secondaryStorage;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -35,12 +36,85 @@ class TempMailBuster
|
||||
public function validate($email)
|
||||
{
|
||||
$domain = $this->getDomain($email);
|
||||
$tempMailsRegex = $this->buildRegex($this->getStorage()->getBlacklist());
|
||||
$match = preg_match($tempMailsRegex, $domain);
|
||||
$secondaryStorage = $this->secondaryStorage;
|
||||
if ($secondaryStorage === null) {
|
||||
$secondaryStorage = new Storage();
|
||||
}
|
||||
|
||||
// TODO: add support for whitelists
|
||||
if (!$this->isWhitelistMode) {
|
||||
$blacklist = $this->primaryStorage;
|
||||
$whitelist = $secondaryStorage;
|
||||
} else {
|
||||
$blacklist = $secondaryStorage;
|
||||
$whitelist = $this->primaryStorage;
|
||||
}
|
||||
|
||||
return !$match;
|
||||
$blacklistRegex = $this->buildRegex($blacklist->getItems());
|
||||
$match = !!preg_match($blacklistRegex, $domain);
|
||||
if ($match == $this->isWhitelistMode) {
|
||||
return !$this->isWhitelistMode;
|
||||
}
|
||||
|
||||
$whitelistRegex = $this->buildRegex($whitelist->getItems());
|
||||
$match = !!preg_match($whitelistRegex, $domain);
|
||||
|
||||
return $match;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Storage
|
||||
*/
|
||||
public function getPrimaryStorage()
|
||||
{
|
||||
return $this->primaryStorage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Storage $primaryStorage
|
||||
* @return static
|
||||
*/
|
||||
public function setPrimaryStorage(Storage $primaryStorage)
|
||||
{
|
||||
$this->primaryStorage = $primaryStorage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Storage|null
|
||||
*/
|
||||
public function getSecondaryStorage()
|
||||
{
|
||||
return $this->secondaryStorage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Storage|null $secondaryStorage
|
||||
* @return static
|
||||
*/
|
||||
public function setSecondaryStorage(Storage $secondaryStorage = null)
|
||||
{
|
||||
$this->secondaryStorage = $secondaryStorage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool is enabled whitelist mode
|
||||
*/
|
||||
public function isIsWhitelistMode()
|
||||
{
|
||||
return $this->isWhitelistMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switching validator working mode
|
||||
*
|
||||
* @param bool $enable
|
||||
* @return static
|
||||
*/
|
||||
public function whitelistMode($enable = true)
|
||||
{
|
||||
$this->isWhitelistMode = $enable;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,27 +3,27 @@ namespace Ely\TempMailBuster;
|
||||
|
||||
class StorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetBlackList()
|
||||
public function testGetItems()
|
||||
{
|
||||
$storage = new Storage(['item']);
|
||||
$this->assertEquals(['item'], $storage->getBlacklist());
|
||||
$this->assertEquals(['item'], $storage->getItems());
|
||||
}
|
||||
|
||||
public function testAppendToBlacklist()
|
||||
public function testAppend()
|
||||
{
|
||||
$storage = new Storage(['item1']);
|
||||
$this->assertEquals($storage, $storage->appendToBlacklist(['item2']));
|
||||
$this->assertEquals(['item1', 'item2'], $storage->getBlacklist());
|
||||
$this->assertEquals($storage, $storage->append(['item2']));
|
||||
$this->assertEquals(['item1', 'item2'], $storage->getItems());
|
||||
|
||||
$storage = new Storage(['item1']);
|
||||
$this->assertEquals($storage, $storage->appendToBlacklist('item2'));
|
||||
$this->assertEquals(['item1', 'item2'], $storage->getBlacklist());
|
||||
$this->assertEquals($storage, $storage->append('item2'));
|
||||
$this->assertEquals(['item1', 'item2'], $storage->getItems());
|
||||
}
|
||||
|
||||
public function testSetBlacklist()
|
||||
public function testSetItems()
|
||||
{
|
||||
$storage = new Storage(['item1']);
|
||||
$this->assertEquals($storage, $storage->setBlacklist(['item2']));
|
||||
$this->assertEquals(['item2'], $storage->getBlacklist());
|
||||
$this->assertEquals($storage, $storage->setItems(['item2']));
|
||||
$this->assertEquals(['item2'], $storage->getItems());
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,70 @@ namespace Ely\TempMailBuster;
|
||||
|
||||
class TempMailBusterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetStorage()
|
||||
public function testValidate()
|
||||
{
|
||||
$object = new TempMailBuster(new Storage());
|
||||
$this->assertTrue($object->validate('notch@mojang.com'));
|
||||
|
||||
$object = new TempMailBuster(new Storage());
|
||||
$object->whitelistMode();
|
||||
$this->assertFalse($object->validate('notch@mojang.com'));
|
||||
|
||||
$object = new TempMailBuster(new Storage(['mojang\.com']));
|
||||
$this->assertFalse($object->validate('notch@mojang.com'));
|
||||
$this->assertTrue($object->validate('erickskrauch@ely.by'));
|
||||
|
||||
$object = new TempMailBuster(new Storage(['gmail\.com']));
|
||||
$object->whitelistMode();
|
||||
$this->assertFalse($object->validate('team@ely.by'));
|
||||
$this->assertTrue($object->validate('erickskrauch@gmail.com'));
|
||||
|
||||
$object = new TempMailBuster(new Storage(['mojang\.com', 'ely\.by']), new Storage(['ely\.by']));
|
||||
$this->assertFalse($object->validate('notch@mojang.com'));
|
||||
$this->assertTrue($object->validate('team@ely.by'));
|
||||
|
||||
$object = new TempMailBuster(new Storage(['gmail\.com', 'mail\.ru']), new Storage(['mail\.ru']));
|
||||
$object->whitelistMode();
|
||||
$this->assertTrue($object->validate('erickskrauch@gmail.com'));
|
||||
$this->assertFalse($object->validate('random@mail.ru'));
|
||||
}
|
||||
|
||||
public function testGetPrimaryStorage()
|
||||
{
|
||||
$storage = new Storage(['test']);
|
||||
$object = new TempMailBuster($storage);
|
||||
$this->assertEquals($storage, $object->getStorage());
|
||||
$this->assertEquals($storage, $object->getPrimaryStorage());
|
||||
}
|
||||
|
||||
public function testSetPrimaryStorage()
|
||||
{
|
||||
$storage = new Storage(['test2']);
|
||||
$object = new TempMailBuster(new Storage(['test1']));
|
||||
$this->assertEquals($object, $object->setPrimaryStorage($storage));
|
||||
$this->assertEquals($storage, $object->getPrimaryStorage());
|
||||
}
|
||||
|
||||
public function testGetSecondaryStorage()
|
||||
{
|
||||
$object = new TempMailBuster(new Storage());
|
||||
$this->assertNull($object->getSecondaryStorage());
|
||||
|
||||
$storage = new Storage(['test']);
|
||||
$object = new TempMailBuster(new Storage(), $storage);
|
||||
$this->assertEquals($storage, $object->getSecondaryStorage());
|
||||
}
|
||||
|
||||
public function testSetSecondaryStorage()
|
||||
{
|
||||
$storage = new Storage(['test2']);
|
||||
$object = new TempMailBuster(new Storage());
|
||||
$this->assertEquals($object, $object->setSecondaryStorage($storage));
|
||||
$this->assertEquals($storage, $object->getSecondaryStorage());
|
||||
$object->setSecondaryStorage(null);
|
||||
$this->assertNull($object->getSecondaryStorage(), 'Set by null should work');
|
||||
$object->setSecondaryStorage($storage);
|
||||
$object->setSecondaryStorage();
|
||||
$this->assertNull($object->getSecondaryStorage(), 'Set by empty value should work');
|
||||
}
|
||||
|
||||
public function testGetDomain()
|
||||
@ -18,6 +77,25 @@ class TempMailBusterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('ely.by', $this->callGetDomain($object, 'ely.by'));
|
||||
}
|
||||
|
||||
public function testIsIsWhitelistMode()
|
||||
{
|
||||
$object = new TempMailBuster(new Storage());
|
||||
$this->assertFalse($object->isIsWhitelistMode(), 'Default should be false');
|
||||
$object->whitelistMode();
|
||||
$this->assertTrue($object->isIsWhitelistMode());
|
||||
}
|
||||
|
||||
public function testWhitelistMode()
|
||||
{
|
||||
$object = new TempMailBuster(new Storage());
|
||||
$this->assertEquals($object, $object->whitelistMode());
|
||||
$this->assertTrue($object->isIsWhitelistMode(), 'Default value should change mode to whitelist');
|
||||
$object->whitelistMode(false);
|
||||
$this->assertFalse($object->isIsWhitelistMode());
|
||||
$object->whitelistMode(true);
|
||||
$this->assertTrue($object->isIsWhitelistMode());
|
||||
}
|
||||
|
||||
public function testBuildRegex()
|
||||
{
|
||||
$object = new TempMailBuster(new Storage());
|
||||
@ -25,15 +103,6 @@ class TempMailBusterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('/^(simple|another)$/', $this->callBuildRegex($object, ['simple', 'another']));
|
||||
}
|
||||
|
||||
public function testValidate()
|
||||
{
|
||||
$storage = new Storage(['mojang\.com']);
|
||||
$object = new TempMailBuster($storage);
|
||||
$this->assertFalse($object->validate('notch@mojang.com'));
|
||||
$this->assertTrue($object->validate('jeb@mojang1.com'));
|
||||
$this->assertTrue($object->validate('erickskrauch@ely.by'));
|
||||
}
|
||||
|
||||
private function callGetDomain($object, $email)
|
||||
{
|
||||
$class = new \ReflectionClass($object);
|
||||
|
Loading…
Reference in New Issue
Block a user