77use Psr \Http \Message \RequestInterface ;
88
99/**
10- * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
10+ * This exception is thrown when a batch of requests led to at least one failure.
11+ *
12+ * It holds the response/exception pairs and gives access to a BatchResult with the successful responses.
13+ *
14+ *@author Márk Sági-Kazár <mark.sagikazar@gmail.com>
1115 */
1216final class BatchException extends RuntimeException
1317{
@@ -27,12 +31,18 @@ public function __construct()
2731 }
2832
2933 /**
30- * Returns a list BatchResult which contains succesful responses
34+ * Returns the BatchResult that contains those responses that where successful.
35+ *
36+ * Note that the BatchResult may contains 0 responses if all requests failed.
3137 *
3238 * @return BatchResult
3339 */
3440 public function getResult ()
3541 {
42+ if (!isset ($ this ->result )) {
43+ $ this ->result = new BatchResult ();
44+ }
45+
3646 return $ this ->result ;
3747 }
3848
@@ -41,10 +51,16 @@ public function getResult()
4151 *
4252 * @param BatchResult $result
4353 *
54+ * @throws InvalidArgumentException If the BatchResult is already set
55+ *
4456 * @internal
4557 */
4658 public function setResult (BatchResult $ result )
4759 {
60+ if (isset ($ this ->result )) {
61+ throw new InvalidArgumentException ('BatchResult is already set ' );
62+ }
63+
4864 $ this ->result = $ result ;
4965 }
5066
@@ -57,7 +73,7 @@ public function setResult(BatchResult $result)
5773 */
5874 public function isSuccessful (RequestInterface $ request )
5975 {
60- return isset ( $ this ->result ) && $ this -> result ->hasResponseFor ($ request );
76+ return $ this ->getResult () ->hasResponseFor ($ request );
6177 }
6278
6379 /**
0 commit comments