1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00
amp/lib/Failure.php

51 lines
1.3 KiB
PHP
Raw Normal View History

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;
/**
* The error parameter used to fail a promisor must always be an exception
* instance. However, we cannot typehint this parameter in environments
* where PHP5.x compatibility is required because PHP7 Throwable
* instances will break the typehint.
2015-07-21 04:24:51 +02:00
*
* @param Exception $error
* @TODO Add Throwable typehint and remove conditional once PHP7 is required
*/
public function __construct($error) {
2015-07-21 04:24:51 +02:00
if ($error instanceof \Throwable || $error instanceof \Exception) {
$this->error = $error;
} else {
throw new \InvalidArgumentException(
2015-07-21 04:24:51 +02:00
"Throwable Exception instance required"
);
}
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
* the specified $cb callback immediately.
2014-09-22 22:47:48 +02:00
*/
public function when(callable $cb, $cbData = null) {
\call_user_func($cb, $this->error, $result = null, $cbData);
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
*/
public function watch(callable $cb, $cbData = null) {
return $this;
2014-09-22 22:47:48 +02:00
}
}