Skip to content

Commit ff7822a

Browse files
sagikazarmarkdbu
authored andcommitted
Remove BatchResult implementation, remove custom SPL exceptions, use SPL exceptions where possible
1 parent be5824f commit ff7822a

8 files changed

+26
-181
lines changed

spec/BatchResultSpec.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

spec/Exception/BatchExceptionSpec.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@ function it_is_an_exception()
2424
$this->shouldImplement('Http\Client\Exception');
2525
}
2626

27-
function it_has_a_result()
27+
function it_throws_an_exception_if_the_result_is_not_available()
2828
{
29-
$this->setResult($result = new BatchResult());
30-
$this->getResult()->shouldReturn($result);
29+
$this->shouldThrow('RuntimeException')->duringGetResult();
3130
}
3231

33-
function it_throws_an_exception_if_the_result_is_already_set()
32+
function it_throws_an_exception_if_the_result_is_already_set(BatchResult $batchResult)
3433
{
34+
$this->setResult($batchResult);
35+
3536
$this->getResult()->shouldHaveType('Http\Client\BatchResult');
36-
$this->shouldThrow('Http\Client\Exception\InvalidArgumentException')->duringSetResult(new BatchResult());
37+
$this->shouldThrow('RuntimeException')->duringSetResult($batchResult);
3738
}
3839

3940
function it_has_an_exception_for_a_request(RequestInterface $request, Exception $exception)
4041
{
41-
$this->shouldThrow('Http\Client\Exception\UnexpectedValueException')->duringGetExceptionFor($request);
42+
$this->shouldThrow('UnexpectedValueException')->duringGetExceptionFor($request);
4243
$this->hasExceptionFor($request)->shouldReturn(false);
4344

4445
$this->addException($request, $exception);
@@ -56,8 +57,11 @@ function it_has_exceptions(RequestInterface $request, Exception $exception)
5657
$this->getExceptions()->shouldReturn([$exception]);
5758
}
5859

59-
function it_checks_if_a_request_failed(RequestInterface $request, Exception $exception)
60+
function it_checks_if_a_request_failed(RequestInterface $request, Exception $exception, BatchResult $batchResult)
6061
{
62+
$batchResult->hasResponseFor($request)->willReturn(false);
63+
$this->setResult($batchResult);
64+
6165
$this->isSuccessful($request)->shouldReturn(false);
6266
$this->isFailed($request)->shouldReturn(false);
6367

spec/Exception/InvalidArgumentExceptionSpec.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

spec/Exception/UnexpectedValueExceptionSpec.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/BatchResult.php

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Http\Client;
44

5-
use Http\Client\Exception\UnexpectedValueException;
5+
use Http\Client\Exception;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88

@@ -11,33 +11,14 @@
1111
*
1212
* @author Márk Sági-Kazár <[email protected]>
1313
*/
14-
final class BatchResult
14+
interface BatchResult
1515
{
16-
/**
17-
* @var \SplObjectStorage
18-
*/
19-
private $responses;
20-
21-
public function __construct()
22-
{
23-
$this->responses = new \SplObjectStorage();
24-
}
25-
2616
/**
2717
* Returns all successful responses
2818
*
2919
* @return ResponseInterface[]
3020
*/
31-
public function getResponses()
32-
{
33-
$responses = [];
34-
35-
foreach ($this->responses as $request) {
36-
$responses[] = $this->responses[$request];
37-
}
38-
39-
return $responses;
40-
}
21+
public function getResponses();
4122

4223
/**
4324
* Returns a response of a request
@@ -46,26 +27,16 @@ public function getResponses()
4627
*
4728
* @return ResponseInterface
4829
*
49-
* @throws UnexpectedValueException
30+
* @throws \UnexpectedValueException If request is not found
5031
*/
51-
public function getResponseFor(RequestInterface $request)
52-
{
53-
try {
54-
return $this->responses[$request];
55-
} catch (\UnexpectedValueException $e) {
56-
throw new UnexpectedValueException('Request not found', $e->getCode(), $e);
57-
}
58-
}
32+
public function getResponseFor(RequestInterface $request);
5933

6034
/**
6135
* Checks if there are any successful responses at all
6236
*
6337
* @return boolean
6438
*/
65-
public function hasResponses()
66-
{
67-
return $this->responses->count() > 0;
68-
}
39+
public function hasResponses();
6940

7041
/**
7142
* Checks if there is a response of a request
@@ -74,10 +45,7 @@ public function hasResponses()
7445
*
7546
* @return ResponseInterface
7647
*/
77-
public function hasResponseFor(RequestInterface $request)
78-
{
79-
return $this->responses->contains($request);
80-
}
48+
public function hasResponseFor(RequestInterface $request);
8149

8250
/**
8351
* Adds a response in an immutable way
@@ -89,16 +57,5 @@ public function hasResponseFor(RequestInterface $request)
8957
*
9058
* @internal
9159
*/
92-
public function addResponse(RequestInterface $request, ResponseInterface $response)
93-
{
94-
$new = clone $this;
95-
$new->responses->attach($request, $response);
96-
97-
return $new;
98-
}
99-
100-
public function __clone()
101-
{
102-
$this->responses = clone $this->responses;
103-
}
60+
public function addResponse(RequestInterface $request, ResponseInterface $response);
10461
}

src/Exception/BatchException.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public function __construct()
3636
* Note that the BatchResult may contains 0 responses if all requests failed.
3737
*
3838
* @return BatchResult
39+
*
40+
* @throws \RuntimeException If the BatchResult is not available
3941
*/
4042
public function getResult()
4143
{
4244
if (!isset($this->result)) {
43-
$this->result = new BatchResult();
45+
throw new \RuntimeException('BatchResult is not available');
4446
}
4547

4648
return $this->result;
@@ -51,14 +53,14 @@ public function getResult()
5153
*
5254
* @param BatchResult $result
5355
*
54-
* @throws InvalidArgumentException If the BatchResult is already set
56+
* @throws \RuntimeException If the BatchResult is already set
5557
*
5658
* @internal
5759
*/
5860
public function setResult(BatchResult $result)
5961
{
6062
if (isset($this->result)) {
61-
throw new InvalidArgumentException('BatchResult is already set');
63+
throw new \RuntimeException('BatchResult is already set');
6264
}
6365

6466
$this->result = $result;
@@ -111,14 +113,14 @@ public function getExceptions()
111113
*
112114
* @return Exception
113115
*
114-
* @throws UnexpectedValueException
116+
* @throws \UnexpectedValueException If request is not found
115117
*/
116118
public function getExceptionFor(RequestInterface $request)
117119
{
118120
try {
119121
return $this->exceptions[$request];
120122
} catch (\UnexpectedValueException $e) {
121-
throw new UnexpectedValueException('Request not found', $e->getCode(), $e);
123+
throw new \UnexpectedValueException('Request not found', $e->getCode(), $e);
122124
}
123125
}
124126

src/Exception/InvalidArgumentException.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Exception/UnexpectedValueException.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)