1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
<?php
namespace phpmock\functions;
/**
* Abstract class for sleep() and usleep() functions.
*
* @author Markus Malkusch <markus@malkusch.de>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
*/
abstract class AbstractSleepFunction implements FunctionProvider
{
/**
* @var Incrementable[] Observing Incrementables.
*/
private $incrementables = [];
/**
* Sets the Incrementable objects.
*
* @param Incrementable[] $incrementables Observing Incrementables.
* @see addIncrementable()
*/
public function __construct(array $incrementables = [])
{
$this->incrementables = $incrementables;
}
/**
* Returns the sleep() mock function.
*
* A call will increase all registered Increment objects.
*
* @return callable The callable for this object.
*/
public function getCallable()
{
return function ($amount) {
foreach ($this->incrementables as $incrementable) {
$incrementable->increment($this->convertToSeconds($amount));
}
};
}
/**
* Converts the sleep() parameter into seconds.
*
* @param int $amount Amount of time units.
* @return mixed Seconds.
* @internal
*/
abstract protected function convertToSeconds($amount);
/**
* Adds an Incrementable object.
*
* These objects are observing this function and get notified by
* increasing the amount of passed time. Incrementables are used
* for time() and microtime() mocks.
*
* @param Incrementable $incrementable Observing Incrementable.
*/
public function addIncrementable(Incrementable $incrementable)
{
$this->incrementables[] = $incrementable;
}
}