Skip to content

Commit

Permalink
extract storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Dec 2, 2015
1 parent 434a757 commit 19607dc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 32 deletions.
39 changes: 39 additions & 0 deletions src/Detector.php
Expand Up @@ -37,6 +37,10 @@ class Detector
'\Sokil\FraudDetector\Collector',
);

private $storageNamespaces = array(
'\Sokil\FraudDetector\Storage',
);

/**
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
Expand Down Expand Up @@ -231,6 +235,41 @@ public function createCollector(

}

private function getStorageClassName($type)
{
if(false == strpos($type, '_')) {
$className = ucfirst($type);
} else {
$className = implode('', array_map('ucfirst', explode('_', $type)));
}

$className .= 'Storage';

foreach($this->storageNamespaces as $namespace) {
$fullyQualifiedClassName = $namespace . '\\' . $className;
if(class_exists($fullyQualifiedClassName)) {
return $fullyQualifiedClassName;
}
}

throw new \Exception('Class ' . $fullyQualifiedClassName . ' not found');
}

public function createStorage($type, $configuratorCallable = null)
{
$className = $this->getStorageClassName($type);

$storage = new $className();

// configure
if(is_callable($configuratorCallable)) {
call_user_func($configuratorCallable, $storage);
}

return $storage;

}

private function on($stateName, $callable)
{
if($this->hasState(self::STATE_UNCHECKED)) {
Expand Down
10 changes: 0 additions & 10 deletions src/Processor/BlackList/Storage/AbstractStorage.php

This file was deleted.

22 changes: 5 additions & 17 deletions src/Processor/BlackListProcessor.php
Expand Up @@ -2,11 +2,12 @@

namespace Sokil\FraudDetector\Processor;

use Sokil\FraudDetector\Storage\StorageInterface;

class BlackListProcessor extends AbstractProcessor
{
/**
*
* @var \Sokil\FraudDetector\Processor\BlackList\Storage\AbstractStorage
* @var \Sokil\FraudDetector\Storage\StorageInterface
*/
private $storage;

Expand Down Expand Up @@ -52,22 +53,9 @@ public function isBannedOnRateExceed()
return true === $this->banOnRateExceed;
}

public function setStorage($type, $configuratorCallable = null)
public function setStorage(StorageInterface $storage)
{
$className = '\Sokil\FraudDetector\Processor\BlackList\Storage\\' . ucfirst($type) . 'Storage';

if(!class_exists($className)) {
throw new \Exception('Storage ' . $type . ' not found');
}

$this->storage = new $className();

// configure
if(is_callable($configuratorCallable)) {
call_user_func($configuratorCallable, $this->storage);
}

$this->storage = $storage;
return $this;

}
}
8 changes: 8 additions & 0 deletions src/Storage/AbstractStorage.php
@@ -0,0 +1,8 @@
<?php

namespace Sokil\FraudDetector\Storage;

abstract class AbstractStorage implements StorageInterface
{

}
@@ -1,6 +1,6 @@
<?php

namespace Sokil\FraudDetector\Processor\BlackList\Storage;
namespace Sokil\FraudDetector\Storage;

class FakeStorage extends AbstractStorage
{
Expand Down
10 changes: 10 additions & 0 deletions src/Storage/StorageInterface.php
@@ -0,0 +1,10 @@
<?php

namespace Sokil\FraudDetector\Storage;

interface StorageInterface
{
public function store($key);

public function isStored($key);
}
8 changes: 4 additions & 4 deletions tests/DetectorTest.php
Expand Up @@ -249,10 +249,10 @@ public function testCheck_AddToBlackListOnRateExceed()
/* @var $processor \Sokil\FraudDetector\Processor\RequestRateProcessor */
$processor->setCollector($detector->createCollector('fake', 'requestRate', 5, 1));
})
->declareProcessor('blackList', function($processor) {
->declareProcessor('blackList', function($processor, $detector) {
/* @var $processor \Sokil\FraudDetector\Processor\BlackListProcessor */
$processor
->setStorage('fake')
->setStorage($detector->createStorage('fake'))
->banOnRateExceed();
})
->onCheckPassed(function() use($status) {
Expand Down Expand Up @@ -282,9 +282,9 @@ public function testCheck_BlackList()

$detector
->setKey('someKey')
->declareProcessor('blackList', function($processor) {
->declareProcessor('blackList', function($processor, $detector) {
/* @var $processor \Sokil\FraudDetector\Processor\BlackListProcessor */
$processor->setStorage('fake');
$processor->setStorage($detector->createStorage('fake'));
})
->onCheckPassed(function() use($status) {
$status->ok = true;
Expand Down

0 comments on commit 19607dc

Please sign in to comment.