2014-09-22 22:47:48 +02:00
|
|
|
<?php
|
|
|
|
|
2014-09-23 04:38:32 +02:00
|
|
|
namespace Amp;
|
2014-09-22 22:47:48 +02:00
|
|
|
|
|
|
|
/**
|
2015-07-21 04:24:51 +02:00
|
|
|
* A rejected (failed) promise
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
|
|
|
class Failure implements Promise {
|
|
|
|
private $error;
|
|
|
|
|
2015-06-11 21:22:14 +02:00
|
|
|
/**
|
|
|
|
* The error parameter used to fail a promisor must always be an exception
|
|
|
|
* instance. However, we cannot typehint this parameter in environments
|
2015-07-07 16:15:57 +02:00
|
|
|
* where PHP5.x compatibility is required because PHP7 Throwable
|
2015-06-11 21:22:14 +02:00
|
|
|
* instances will break the typehint.
|
2015-07-21 04:24:51 +02:00
|
|
|
*
|
2015-08-05 23:45:29 +02:00
|
|
|
* @param \Exception|\Throwable $error
|
2015-07-21 04:24:51 +02:00
|
|
|
* @TODO Add Throwable typehint and remove conditional once PHP7 is required
|
2015-06-11 21:22:14 +02:00
|
|
|
*/
|
|
|
|
public function __construct($error) {
|
2015-07-21 04:24:51 +02:00
|
|
|
if ($error instanceof \Throwable || $error instanceof \Exception) {
|
|
|
|
$this->error = $error;
|
|
|
|
} else {
|
2015-06-11 21:22:14 +02:00
|
|
|
throw new \InvalidArgumentException(
|
2015-07-21 04:24:51 +02:00
|
|
|
"Throwable Exception instance required"
|
2015-06-11 21:22:14 +02:00
|
|
|
);
|
|
|
|
}
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-21 04:24:51 +02:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* NOTE: because this object represents a resolved Promise it will *always* invoke
|
2015-07-27 16:07:26 +02:00
|
|
|
* the specified $cb callback immediately.
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-07-27 16:07:26 +02:00
|
|
|
public function when(callable $cb, $cbData = null) {
|
|
|
|
\call_user_func($cb, $this->error, $result = null, $cbData);
|
2015-06-12 12:48:11 +02:00
|
|
|
|
|
|
|
return $this;
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-21 04:24:51 +02:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* Does nothing; a resolved promise has no progress updates
|
2014-09-22 22:47:48 +02:00
|
|
|
*/
|
2015-07-27 16:07:26 +02:00
|
|
|
public function watch(callable $cb, $cbData = null) {
|
2015-06-12 12:48:11 +02:00
|
|
|
return $this;
|
2014-09-22 22:47:48 +02:00
|
|
|
}
|
|
|
|
}
|