Skip to content

Commit bb33cbb

Browse files
sagikazarmarkdbu
authored andcommitted
Move exceptions to BatchResult
1 parent ff7822a commit bb33cbb

File tree

3 files changed

+66
-159
lines changed

3 files changed

+66
-159
lines changed

spec/Exception/BatchExceptionSpec.php

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class BatchExceptionSpec extends ObjectBehavior
1111
{
12+
function let(BatchResult $batchResult)
13+
{
14+
$this->beConstructedWith($batchResult);
15+
}
16+
1217
function it_is_initializable()
1318
{
1419
$this->shouldHaveType('Http\Client\Exception\BatchException');
@@ -24,50 +29,8 @@ function it_is_an_exception()
2429
$this->shouldImplement('Http\Client\Exception');
2530
}
2631

27-
function it_throws_an_exception_if_the_result_is_not_available()
28-
{
29-
$this->shouldThrow('RuntimeException')->duringGetResult();
30-
}
31-
32-
function it_throws_an_exception_if_the_result_is_already_set(BatchResult $batchResult)
32+
function it_has_a_batch_result()
3333
{
34-
$this->setResult($batchResult);
35-
3634
$this->getResult()->shouldHaveType('Http\Client\BatchResult');
37-
$this->shouldThrow('RuntimeException')->duringSetResult($batchResult);
38-
}
39-
40-
function it_has_an_exception_for_a_request(RequestInterface $request, Exception $exception)
41-
{
42-
$this->shouldThrow('UnexpectedValueException')->duringGetExceptionFor($request);
43-
$this->hasExceptionFor($request)->shouldReturn(false);
44-
45-
$this->addException($request, $exception);
46-
47-
$this->getExceptionFor($request)->shouldReturn($exception);
48-
$this->hasExceptionFor($request)->shouldReturn(true);
49-
}
50-
51-
function it_has_exceptions(RequestInterface $request, Exception $exception)
52-
{
53-
$this->getExceptions()->shouldReturn([]);
54-
55-
$this->addException($request, $exception);
56-
57-
$this->getExceptions()->shouldReturn([$exception]);
58-
}
59-
60-
function it_checks_if_a_request_failed(RequestInterface $request, Exception $exception, BatchResult $batchResult)
61-
{
62-
$batchResult->hasResponseFor($request)->willReturn(false);
63-
$this->setResult($batchResult);
64-
65-
$this->isSuccessful($request)->shouldReturn(false);
66-
$this->isFailed($request)->shouldReturn(false);
67-
68-
$this->addException($request, $exception);
69-
70-
$this->isSuccessful($request)->shouldReturn(false);
71-
$this->isFailed($request)->shouldReturn(true);
7235
}
7336
}

src/BatchResult.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Psr\Http\Message\ResponseInterface;
88

99
/**
10-
* Successful responses returned from parallel request execution
10+
* Responses and exceptions returned from parallel request execution
1111
*
1212
* @author Márk Sági-Kazár <[email protected]>
1313
*/
@@ -54,8 +54,61 @@ public function hasResponseFor(RequestInterface $request);
5454
* @param ResponseInterface $response
5555
*
5656
* @return BatchResult
57-
*
58-
* @internal
5957
*/
6058
public function addResponse(RequestInterface $request, ResponseInterface $response);
59+
60+
/**
61+
* Checks if a request is successful
62+
*
63+
* @param RequestInterface $request
64+
*
65+
* @return boolean
66+
*/
67+
public function isSuccessful(RequestInterface $request);
68+
69+
/**
70+
* Checks if a request is failed
71+
*
72+
* @param RequestInterface $request
73+
*
74+
* @return boolean
75+
*/
76+
public function isFailed(RequestInterface $request);
77+
78+
/**
79+
* Returns all exceptions
80+
*
81+
* @return Exception[]
82+
*/
83+
public function getExceptions();
84+
85+
/**
86+
* Returns an exception for a request
87+
*
88+
* @param RequestInterface $request
89+
*
90+
* @return Exception
91+
*
92+
* @throws \UnexpectedValueException If request is not found
93+
*/
94+
public function getExceptionFor(RequestInterface $request);
95+
96+
/**
97+
* Checks if there is an exception for a request
98+
*
99+
* @param RequestInterface $request
100+
*
101+
* @return boolean
102+
*/
103+
public function hasExceptionFor(RequestInterface $request);
104+
105+
/**
106+
* Adds an exception
107+
*
108+
* @param RequestInterface $request
109+
* @param Exception $exception
110+
*
111+
* @return BatchResult
112+
*/
113+
public function addException(RequestInterface $request, Exception $exception);
61114
}

src/Exception/BatchException.php

Lines changed: 4 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -21,129 +21,20 @@ final class BatchException extends \RuntimeException implements Exception
2121
private $result;
2222

2323
/**
24-
* @var \SplObjectStorage
24+
* @param BatchResult $result
2525
*/
26-
private $exceptions;
27-
28-
public function __construct()
26+
public function __construct(BatchResult $result)
2927
{
30-
$this->exceptions = new \SplObjectStorage();
28+
$this->result = $result;
3129
}
3230

3331
/**
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.
32+
* Returns the BatchResult that contains all responses and exceptions
3733
*
3834
* @return BatchResult
39-
*
40-
* @throws \RuntimeException If the BatchResult is not available
4135
*/
4236
public function getResult()
4337
{
44-
if (!isset($this->result)) {
45-
throw new \RuntimeException('BatchResult is not available');
46-
}
47-
4838
return $this->result;
4939
}
50-
51-
/**
52-
* Sets the successful response list
53-
*
54-
* @param BatchResult $result
55-
*
56-
* @throws \RuntimeException If the BatchResult is already set
57-
*
58-
* @internal
59-
*/
60-
public function setResult(BatchResult $result)
61-
{
62-
if (isset($this->result)) {
63-
throw new \RuntimeException('BatchResult is already set');
64-
}
65-
66-
$this->result = $result;
67-
}
68-
69-
/**
70-
* Checks if a request is successful
71-
*
72-
* @param RequestInterface $request
73-
*
74-
* @return boolean
75-
*/
76-
public function isSuccessful(RequestInterface $request)
77-
{
78-
return $this->getResult()->hasResponseFor($request);
79-
}
80-
81-
/**
82-
* Checks if a request is failed
83-
*
84-
* @param RequestInterface $request
85-
*
86-
* @return boolean
87-
*/
88-
public function isFailed(RequestInterface $request)
89-
{
90-
return $this->exceptions->contains($request);
91-
}
92-
93-
/**
94-
* Returns all exceptions
95-
*
96-
* @return Exception[]
97-
*/
98-
public function getExceptions()
99-
{
100-
$exceptions = [];
101-
102-
foreach ($this->exceptions as $request) {
103-
$exceptions[] = $this->exceptions[$request];
104-
}
105-
106-
return $exceptions;
107-
}
108-
109-
/**
110-
* Returns an exception for a request
111-
*
112-
* @param RequestInterface $request
113-
*
114-
* @return Exception
115-
*
116-
* @throws \UnexpectedValueException If request is not found
117-
*/
118-
public function getExceptionFor(RequestInterface $request)
119-
{
120-
try {
121-
return $this->exceptions[$request];
122-
} catch (\UnexpectedValueException $e) {
123-
throw new \UnexpectedValueException('Request not found', $e->getCode(), $e);
124-
}
125-
}
126-
127-
/**
128-
* Checks if there is an exception for a request
129-
*
130-
* @param RequestInterface $request
131-
*
132-
* @return boolean
133-
*/
134-
public function hasExceptionFor(RequestInterface $request)
135-
{
136-
return $this->exceptions->contains($request);
137-
}
138-
139-
/**
140-
* Adds an exception
141-
*
142-
* @param RequestInterface $request
143-
* @param Exception $exception
144-
*/
145-
public function addException(RequestInterface $request, Exception $exception)
146-
{
147-
$this->exceptions->attach($request, $exception);
148-
}
14940
}

0 commit comments

Comments
 (0)