Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mickaël Andrieu
committed
Jan 7, 2019
1 parent
85be647
commit aa15280
Showing
9 changed files
with
410 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace PrestaShop\CircuitBreaker\Contracts; | ||
|
||
use PrestaShop\CircuitBreaker\Exceptions\TransactionNotFound; | ||
|
||
/** | ||
* Helper to manipulate a list of transactions. | ||
*/ | ||
interface TransactionCollection extends \Countable | ||
{ | ||
/** | ||
* @param string $service the Transaction service URI | ||
* | ||
* @throws TransactionNotFound | ||
* | ||
* @return Transaction | ||
*/ | ||
public function findOneByService($service); | ||
|
||
/** | ||
* Returns all transactions of the storage. | ||
* | ||
* @return array | ||
*/ | ||
public function findAll(); | ||
|
||
/** | ||
* Returns all transactions of the storage | ||
* using a callable to filter results. | ||
* | ||
* @return array | ||
*/ | ||
public function findAllUsing(callable $function); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace PrestaShop\CircuitBreaker\Storages; | ||
|
||
use PrestaShop\CircuitBreaker\Contracts\Storage; | ||
use PrestaShop\CircuitBreaker\Contracts\Transaction; | ||
use Symfony\Component\Cache\Adapter\AbstractAdapter; | ||
use PrestaShop\CircuitBreaker\Exceptions\TransactionNotFound; | ||
use PrestaShop\CircuitBreaker\Transactions\SimpleCollection; | ||
|
||
/** | ||
* Implementation of Storage using the Symfony Cache Component. | ||
*/ | ||
final class SymfonyCache implements Storage | ||
{ | ||
/** | ||
* @var AbstractAdapter the Symfony Cache adapter | ||
*/ | ||
private $symfonyCacheAdapter; | ||
|
||
/** | ||
* @var string the Symfony Cache namespace | ||
*/ | ||
private $namespace; | ||
|
||
public function __construct(AbstractAdapter $symfonyCacheAdapter, $namespace) | ||
{ | ||
$this->symfonyCacheAdapter = $symfonyCacheAdapter; | ||
$this->namespace = $namespace; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function saveTransaction($service, Transaction $transaction) | ||
{ | ||
$key = $this->getKey($service); | ||
$cacheItem = $this->symfonyCacheAdapter->getItem($key); | ||
|
||
$cacheItem->set($transaction); | ||
|
||
return $this->symfonyCacheAdapter->save($cacheItem); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTransaction($service) | ||
{ | ||
$key = $this->getKey($service); | ||
|
||
if ($this->hasTransaction($service)) { | ||
return $this->symfonyCacheAdapter->getItem($key)->get(); | ||
} | ||
|
||
throw new TransactionNotFound(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTransactions() | ||
{ | ||
$transactions = []; | ||
foreach ($this->symfonyCacheAdapter->getItems([$this->namespace]) as $item) { | ||
$transactions[] = $item->get(); | ||
} | ||
|
||
return SimpleCollection::create($transactions); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasTransaction($service) | ||
{ | ||
$key = $this->getKey($service); | ||
|
||
return $this->symfonyCacheAdapter->hasItem($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
return $this->symfonyCacheAdapter->clear(); | ||
} | ||
|
||
/** | ||
* Helper method to properly store the transaction | ||
* | ||
* @param string $service the service URI | ||
* | ||
* @return string the transaction unique identifier | ||
*/ | ||
private function getKey($service) | ||
{ | ||
return md5($service); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace PrestaShop\CircuitBreaker\Transactions; | ||
|
||
use PrestaShop\CircuitBreaker\Contracts\TransactionCollection; | ||
use PrestaShop\CircuitBreaker\Exceptions\TransactionNotFound; | ||
use PrestaShop\CircuitBreaker\Contracts\Transaction; | ||
|
||
final class SimpleCollection implements TransactionCollection | ||
{ | ||
/** | ||
* @var Transaction[] the list of transactions | ||
*/ | ||
private $transactions; | ||
|
||
public function __construct(array $transactions = []) | ||
{ | ||
$this->transactions = $transactions; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findOneByService($service) | ||
{ | ||
foreach ($this->transactions as $transaction) { | ||
if ($transaction->getService() === $service) { | ||
return $transaction; | ||
} | ||
} | ||
|
||
throw new TransactionNotFound(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findAll() | ||
{ | ||
return $this->transactions; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findAllUsing(callable $function) | ||
{ | ||
return array_filter($this->transactions, $function); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function count() | ||
{ | ||
return count($this->transactions); | ||
} | ||
|
||
/** | ||
* Helper static constructor | ||
* | ||
* @param array $transactions | ||
* | ||
* @return self | ||
*/ | ||
public static function create(array $transactions) | ||
{ | ||
return new self($transactions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.