Implemented Storage interface

This commit is contained in:
ErickSkrauch 2016-04-29 00:14:34 +03:00
parent 9939ee72b2
commit b2b8a0438f
4 changed files with 51 additions and 30 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
namespace Ely\TempMailBuster; namespace Ely\TempMailBuster;
class Storage class Storage implements StorageInterface
{ {
/** /**
* @var array of strings, which contains masks for temp mail services * @var array of strings, which contains masks for temp mail services
@ -17,7 +17,7 @@ class Storage
} }
/** /**
* @return array with current items * @inheritdoc
*/ */
public function getItems() public function getItems()
{ {
@ -25,20 +25,18 @@ class Storage
} }
/** /**
* @param array $items override current items with passed values * @inheritdoc
* @return static
*/ */
public function setItems(array $items) public function setItems($items)
{ {
$this->items = $items; $this->items = (array)$items;
return $this; return $this;
} }
/** /**
* @param string|array $items item or items, that will be merged to items * @inheritdoc
* @return static
*/ */
public function append($items) public function appendItems($items)
{ {
$items = (array)$items; $items = (array)$items;
$this->items = array_merge($this->items, $items); $this->items = array_merge($this->items, $items);

22
src/StorageInterface.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Ely\TempMailBuster;
interface StorageInterface
{
/**
* @return array with current items
*/
public function getItems();
/**
* @param array $items replace current items with passed values
* @return static
*/
public function setItems($items);
/**
* @param string|array $items item or items, that will be merged to exists array of strings
* @return static
*/
public function appendItems($items);
}

View File

@ -4,11 +4,11 @@ namespace Ely\TempMailBuster;
class TempMailBuster class TempMailBuster
{ {
/** /**
* @var Storage * @var StorageInterface
*/ */
private $primaryStorage; private $primaryStorage;
/** /**
* @var Storage|null * @var StorageInterface|null
*/ */
private $secondaryStorage; private $secondaryStorage;
/** /**
@ -17,9 +17,10 @@ class TempMailBuster
private $isWhitelistMode = false; private $isWhitelistMode = false;
/** /**
* @param Storage $storage * @param StorageInterface $primaryStorage
* @param StorageInterface|null $secondaryStorage
*/ */
public function __construct(Storage $primaryStorage, Storage $secondaryStorage = null) public function __construct(StorageInterface $primaryStorage, StorageInterface $secondaryStorage = null)
{ {
$this->primaryStorage = $primaryStorage; $this->primaryStorage = $primaryStorage;
$this->secondaryStorage = $secondaryStorage; $this->secondaryStorage = $secondaryStorage;
@ -62,7 +63,7 @@ class TempMailBuster
} }
/** /**
* @return Storage * @return StorageInterface
*/ */
public function getPrimaryStorage() public function getPrimaryStorage()
{ {
@ -70,17 +71,17 @@ class TempMailBuster
} }
/** /**
* @param Storage $primaryStorage * @param StorageInterface $primaryStorage
* @return static * @return static
*/ */
public function setPrimaryStorage(Storage $primaryStorage) public function setPrimaryStorage(StorageInterface $primaryStorage)
{ {
$this->primaryStorage = $primaryStorage; $this->primaryStorage = $primaryStorage;
return $this; return $this;
} }
/** /**
* @return Storage|null * @return StorageInterface|null
*/ */
public function getSecondaryStorage() public function getSecondaryStorage()
{ {
@ -88,10 +89,10 @@ class TempMailBuster
} }
/** /**
* @param Storage|null $secondaryStorage * @param StorageInterface|null $secondaryStorage
* @return static * @return static
*/ */
public function setSecondaryStorage(Storage $secondaryStorage = null) public function setSecondaryStorage(StorageInterface $secondaryStorage = null)
{ {
$this->secondaryStorage = $secondaryStorage; $this->secondaryStorage = $secondaryStorage;
return $this; return $this;

View File

@ -9,21 +9,21 @@ class StorageTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(['item'], $storage->getItems()); $this->assertEquals(['item'], $storage->getItems());
} }
public function testAppend()
{
$storage = new Storage(['item1']);
$this->assertEquals($storage, $storage->append(['item2']));
$this->assertEquals(['item1', 'item2'], $storage->getItems());
$storage = new Storage(['item1']);
$this->assertEquals($storage, $storage->append('item2'));
$this->assertEquals(['item1', 'item2'], $storage->getItems());
}
public function testSetItems() public function testSetItems()
{ {
$storage = new Storage(['item1']); $storage = new Storage(['item1']);
$this->assertEquals($storage, $storage->setItems(['item2'])); $this->assertEquals($storage, $storage->setItems(['item2']));
$this->assertEquals(['item2'], $storage->getItems()); $this->assertEquals(['item2'], $storage->getItems());
} }
public function testAppendItems()
{
$storage = new Storage(['item1']);
$this->assertEquals($storage, $storage->appendItems(['item2']));
$this->assertEquals(['item1', 'item2'], $storage->getItems());
$storage = new Storage(['item1']);
$this->assertEquals($storage, $storage->appendItems('item2'));
$this->assertEquals(['item1', 'item2'], $storage->getItems());
}
} }