Skip to content

Commit f68ad2c

Browse files
committed
Move exceptions to BatchException
1 parent e6acb53 commit f68ad2c

File tree

5 files changed

+137
-142
lines changed

5 files changed

+137
-142
lines changed

spec/BatchResultSpec.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace spec\Http\Client;
44

5-
use Http\Client\Exception;
65
use Psr\Http\Message\RequestInterface;
76
use Psr\Http\Message\ResponseInterface;
87
use PhpSpec\ObjectBehavior;
@@ -14,19 +13,13 @@ function it_is_initializable()
1413
$this->shouldHaveType('Http\Client\BatchResult');
1514
}
1615

17-
function it_is_immutable(RequestInterface $request, ResponseInterface $response, Exception $exception)
16+
function it_is_immutable(RequestInterface $request, ResponseInterface $response)
1817
{
1918
$new = $this->addResponse($request, $response);
20-
$new2 = $new->addException($request, $exception);
2119

2220
$this->getResponses()->shouldReturn([]);
23-
$this->getExceptions()->shouldReturn([]);
2421
$new->shouldHaveType('Http\Client\BatchResult');
2522
$new->getResponses()->shouldReturn([$response]);
26-
$new->getExceptions()->shouldReturn([]);
27-
$new2->shouldHaveType('Http\Client\BatchResult');
28-
$new2->getResponses()->shouldReturn([$response]);
29-
$new2->getExceptions()->shouldReturn([$exception]);
3023
}
3124

3225
function it_has_a_response_for_a_request(RequestInterface $request, ResponseInterface $response)
@@ -37,31 +30,9 @@ function it_has_a_response_for_a_request(RequestInterface $request, ResponseInte
3730
$this->hasResponses()->shouldReturn(false);
3831
$this->hasResponseFor($request)->shouldReturn(false);
3932
$this->getResponses()->shouldReturn([]);
40-
$this->isSuccessful($request)->shouldReturn(false);
41-
$this->isFailed($request)->shouldReturn(false);
4233
$new->getResponseFor($request)->shouldReturn($response);
4334
$new->hasResponses()->shouldReturn(true);
4435
$new->hasResponseFor($request)->shouldReturn(true);
4536
$new->getResponses()->shouldReturn([$response]);
46-
$new->isSuccessful($request)->shouldReturn(true);
47-
$new->isFailed($request)->shouldReturn(false);
48-
}
49-
50-
function it_has_an_exception_for_a_request(RequestInterface $request, Exception $exception)
51-
{
52-
$new = $this->addException($request, $exception);
53-
54-
$this->getExceptionFor($request)->shouldReturn(null);
55-
$this->hasExceptions()->shouldReturn(false);
56-
$this->hasExceptionFor($request)->shouldReturn(false);
57-
$this->getExceptions()->shouldReturn([]);
58-
$this->isSuccessful($request)->shouldReturn(false);
59-
$this->isFailed($request)->shouldReturn(false);
60-
$new->getExceptionFor($request)->shouldReturn($exception);
61-
$new->hasExceptions()->shouldReturn(true);
62-
$new->hasExceptionFor($request)->shouldReturn(true);
63-
$new->getExceptions()->shouldReturn([$exception]);
64-
$new->isSuccessful($request)->shouldReturn(false);
65-
$new->isFailed($request)->shouldReturn(true);
6637
}
6738
}

spec/Exception/BatchExceptionSpec.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
namespace spec\Http\Client\Exception;
44

55
use Http\Client\BatchResult;
6+
use Http\Client\Exception;
7+
use Psr\Http\Message\RequestInterface;
68
use PhpSpec\ObjectBehavior;
79

810
class BatchExceptionSpec extends ObjectBehavior
911
{
10-
private $result;
11-
12-
function let()
13-
{
14-
$this->beConstructedWith($this->result = new BatchResult());
15-
}
16-
1712
function it_is_initializable()
1813
{
1914
$this->shouldHaveType('Http\Client\Exception\BatchException');
@@ -27,6 +22,27 @@ function it_is_an_exception()
2722

2823
function it_has_a_result()
2924
{
30-
$this->getResult()->shouldReturn($this->result);
25+
$this->getResult()->shouldReturn(null);
26+
$this->setResult($result = new BatchResult());
27+
$this->getResult()->shouldReturn($result);
28+
}
29+
30+
function it_has_an_exception_for_a_request(RequestInterface $request, Exception $exception)
31+
{
32+
$this->getExceptionFor($request)->shouldReturn(null);
33+
$this->hasExceptions()->shouldReturn(false);
34+
$this->hasExceptionFor($request)->shouldReturn(false);
35+
$this->getExceptions()->shouldReturn([]);
36+
$this->isSuccessful($request)->shouldReturn(false);
37+
$this->isFailed($request)->shouldReturn(false);
38+
39+
$this->addException($request, $exception);
40+
41+
$this->getExceptionFor($request)->shouldReturn($exception);
42+
$this->hasExceptions()->shouldReturn(true);
43+
$this->hasExceptionFor($request)->shouldReturn(true);
44+
$this->getExceptions()->shouldReturn([$exception]);
45+
$this->isSuccessful($request)->shouldReturn(false);
46+
$this->isFailed($request)->shouldReturn(true);
3147
}
3248
}

src/BatchResult.php

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,9 @@ final class BatchResult
1717
*/
1818
private $responses;
1919

20-
/**
21-
* @var \SplObjectStorage
22-
*/
23-
private $exceptions;
24-
2520
public function __construct()
2621
{
2722
$this->responses = new \SplObjectStorage();
28-
$this->exceptions = new \SplObjectStorage();
29-
}
30-
31-
/**
32-
* Checks if a request is successful
33-
*
34-
* @param RequestInterface $request
35-
*
36-
* @return boolean
37-
*/
38-
public function isSuccessful(RequestInterface $request)
39-
{
40-
return $this->responses->contains($request);
41-
}
42-
43-
/**
44-
* Checks if a request is failed
45-
*
46-
* @param RequestInterface $request
47-
*
48-
* @return boolean
49-
*/
50-
public function isFailed(RequestInterface $request)
51-
{
52-
return $this->exceptions->contains($request);
5323
}
5424

5525
/**
@@ -111,6 +81,8 @@ public function hasResponseFor(RequestInterface $request)
11181
* @param ResponseInterface $response
11282
*
11383
* @return BatchResult
84+
*
85+
* @internal
11486
*/
11587
public function addResponse(RequestInterface $request, ResponseInterface $response)
11688
{
@@ -120,77 +92,8 @@ public function addResponse(RequestInterface $request, ResponseInterface $respon
12092
return $new;
12193
}
12294

123-
/**
124-
* Returns all exceptions
125-
*
126-
* @return Exception[]
127-
*/
128-
public function getExceptions()
129-
{
130-
$exceptions = [];
131-
132-
foreach ($this->exceptions as $request) {
133-
$exceptions[] = $this->exceptions[$request];
134-
}
135-
136-
return $exceptions;
137-
}
138-
139-
/**
140-
* Returns an exception for a request or null if not found
141-
*
142-
* @param RequestInterface $request
143-
*
144-
* @return Exception|null
145-
*/
146-
public function getExceptionFor(RequestInterface $request)
147-
{
148-
if ($this->exceptions->contains($request)) {
149-
return $this->exceptions[$request];
150-
}
151-
}
152-
153-
/**
154-
* Checks if there are any exceptions at all
155-
*
156-
* @return boolean
157-
*/
158-
public function hasExceptions()
159-
{
160-
return $this->exceptions->count() > 0;
161-
}
162-
163-
/**
164-
* Checks if there is an exception for a request
165-
*
166-
* @param RequestInterface $request
167-
*
168-
* @return Exception
169-
*/
170-
public function hasExceptionFor(RequestInterface $request)
171-
{
172-
return $this->exceptions->contains($request);
173-
}
174-
175-
/**
176-
* Adds an exception in an immutable way
177-
*
178-
* @param RequestInterface $request
179-
* @param Exception $exception
180-
*
181-
* @return BatchResult
182-
*/
183-
public function addException(RequestInterface $request, Exception $exception)
184-
{
185-
$new = clone $this;
186-
$new->exceptions->attach($request, $exception);
187-
188-
return $new;
189-
}
190-
19195
public function __clone()
19296
{
19397
$this->responses = clone $this->responses;
194-
$this->exceptions = clone $this->exceptions;
19598
}
19699
}

src/Exception/BatchException.php

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Http\Client\Exception;
44

55
use Http\Client\BatchResult;
6+
use Http\Client\Exception;
7+
use Psr\Http\Message\RequestInterface;
68

79
/**
810
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
@@ -15,20 +17,121 @@ final class BatchException extends RuntimeException
1517
private $result;
1618

1719
/**
18-
* @param BatchResult $result
20+
* @var \SplObjectStorage
1921
*/
20-
public function __construct(BatchResult $result)
22+
private $exceptions;
23+
24+
public function __construct()
2125
{
22-
$this->result = $result;
26+
$this->exceptions = new \SplObjectStorage();
2327
}
2428

2529
/**
26-
* Returns a list BatchResult which contains succesful responses and exceptions for failed requests.
30+
* Returns a list BatchResult which contains succesful responses
2731
*
2832
* @return BatchResult
2933
*/
3034
public function getResult()
3135
{
3236
return $this->result;
3337
}
38+
39+
/**
40+
* Sets the successful response list
41+
*
42+
* @param BatchResult $result
43+
*
44+
* @internal
45+
*/
46+
public function setResult(BatchResult $result)
47+
{
48+
$this->result = $result;
49+
}
50+
51+
/**
52+
* Checks if a request is successful
53+
*
54+
* @param RequestInterface $request
55+
*
56+
* @return boolean
57+
*/
58+
public function isSuccessful(RequestInterface $request)
59+
{
60+
return isset($this->result) && $this->result->hasResponseFor($request);
61+
}
62+
63+
/**
64+
* Checks if a request is failed
65+
*
66+
* @param RequestInterface $request
67+
*
68+
* @return boolean
69+
*/
70+
public function isFailed(RequestInterface $request)
71+
{
72+
return $this->exceptions->contains($request);
73+
}
74+
75+
/**
76+
* Returns all exceptions
77+
*
78+
* @return Exception[]
79+
*/
80+
public function getExceptions()
81+
{
82+
$exceptions = [];
83+
84+
foreach ($this->exceptions as $request) {
85+
$exceptions[] = $this->exceptions[$request];
86+
}
87+
88+
return $exceptions;
89+
}
90+
91+
/**
92+
* Returns an exception for a request or null if not found
93+
*
94+
* @param RequestInterface $request
95+
*
96+
* @return Exception|null
97+
*/
98+
public function getExceptionFor(RequestInterface $request)
99+
{
100+
if ($this->exceptions->contains($request)) {
101+
return $this->exceptions[$request];
102+
}
103+
}
104+
105+
/**
106+
* Checks if there are any exceptions at all
107+
*
108+
* @return boolean
109+
*/
110+
public function hasExceptions()
111+
{
112+
return $this->exceptions->count() > 0;
113+
}
114+
115+
/**
116+
* Checks if there is an exception for a request
117+
*
118+
* @param RequestInterface $request
119+
*
120+
* @return boolean
121+
*/
122+
public function hasExceptionFor(RequestInterface $request)
123+
{
124+
return $this->exceptions->contains($request);
125+
}
126+
127+
/**
128+
* Adds an exception
129+
*
130+
* @param RequestInterface $request
131+
* @param Exception $exception
132+
*/
133+
public function addException(RequestInterface $request, Exception $exception)
134+
{
135+
$this->exceptions->attach($request, $exception);
136+
}
34137
}

src/HttpPsrClient.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Client;
44

5+
use Http\Client\Exception\BatchException;
56
use Psr\Http\Message\RequestInterface;
67
use Psr\Http\Message\ResponseInterface;
78

@@ -30,9 +31,10 @@ public function sendRequest(RequestInterface $request, array $options = []);
3031
* @param RequestInterface[] $requests
3132
* @param array $options
3233
*
33-
* @return ResponseInterface[]
34+
* @return BatchResult
3435
*
3536
* @throws Exception
37+
* @throws BatchException
3638
*/
3739
public function sendRequests(array $requests, array $options = []);
3840
}

0 commit comments

Comments
 (0)