Skip to content

Commit c92df82

Browse files
committed
Moves exceptions back to the adapter package
1 parent 2d9bbf6 commit c92df82

File tree

2 files changed

+161
-26
lines changed

2 files changed

+161
-26
lines changed

src/Exception/HttpAdapterException.php

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,94 @@
1818
/**
1919
* @author GeLo <[email protected]>
2020
*/
21-
interface HttpAdapterException extends Exception
21+
class HttpAdapterException extends \Exception implements Exception
2222
{
23+
/**
24+
* @var RequestInterface|null
25+
*/
26+
private $request;
27+
28+
/**
29+
* @var ResponseInterface|null
30+
*/
31+
private $response;
32+
2333
/**
2434
* Returns the request
2535
*
2636
* @return RequestInterface|null
2737
*/
28-
public function getRequest();
38+
public function getRequest()
39+
{
40+
return $this->request;
41+
}
2942

3043
/**
3144
* Checks if there is a request
3245
*
3346
* @return boolean
3447
*/
35-
public function hasRequest();
48+
public function hasRequest()
49+
{
50+
return isset($this->request);
51+
}
3652

3753
/**
3854
* Sets the request
3955
*
4056
* @param RequestInterface|null $request
4157
*/
42-
public function setRequest(RequestInterface $request = null);
58+
public function setRequest(RequestInterface $request = null)
59+
{
60+
$this->request = $request;
61+
}
4362

4463
/**
4564
* Returns the response
4665
*
4766
* @return ResponseInterface|null
4867
*/
49-
public function getResponse();
68+
public function getResponse()
69+
{
70+
return $this->response;
71+
}
5072

5173
/**
5274
* Checks if there is a response
5375
*
5476
* @return boolean
5577
*/
56-
public function hasResponse();
78+
public function hasResponse()
79+
{
80+
return isset($this->response);
81+
}
5782

5883
/**
5984
* Sets the response
6085
*
6186
* @param ResponseInterface|null $response
6287
*/
63-
public function setResponse(ResponseInterface $response = null);
88+
public function setResponse(ResponseInterface $response = null)
89+
{
90+
$this->response = $response;
91+
}
92+
93+
/**
94+
* @param string $uri
95+
* @param string $adapterName
96+
* @param \Exception|null $previous
97+
*/
98+
public static function cannotFetchUri($uri, $adapterName, \Exception $previous = null)
99+
{
100+
$message = sprintf(
101+
'An error occurred when fetching the URI "%s" with the adapter "%s" ("%s").',
102+
$uri,
103+
$adapterName,
104+
isset($previous) ? $previous->getMessage() : ''
105+
);
106+
107+
$code = isset($previous) ? $previous->getCode() : 0;
108+
109+
return new self($message, $code, $previous);
110+
}
64111
}

src/Exception/MultiHttpAdapterException.php

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,39 @@
1717
/**
1818
* @author GeLo <[email protected]>
1919
*/
20-
interface MultiHttpAdapterException extends Exception
20+
class MultiHttpAdapterException extends \Exception implements Exception
2121
{
22+
/**
23+
* @var HttpAdapterException[]
24+
*/
25+
private $exceptions;
26+
27+
/**
28+
* @var ResponseInterface[]
29+
*/
30+
private $responses;
31+
32+
/**
33+
* @param HttpAdapterException[] $exceptions
34+
* @param ResponseInterface[] $responses
35+
*/
36+
public function __construct(array $exceptions = [], array $responses = [])
37+
{
38+
parent::__construct('An error occurred when sending multiple requests.');
39+
40+
$this->setExceptions($exceptions);
41+
$this->setResponses($responses);
42+
}
43+
2244
/**
2345
* Returns all exceptions
2446
*
2547
* @return HttpAdapterException[]
2648
*/
27-
public function getExceptions();
49+
public function getExceptions()
50+
{
51+
return $this->exceptions;
52+
}
2853

2954
/**
3055
* Checks if a specific exception exists
@@ -33,61 +58,94 @@ public function getExceptions();
3358
*
3459
* @return boolean TRUE if there is the exception else FALSE.
3560
*/
36-
public function hasException(HttpAdapterException $exception);
61+
public function hasException(HttpAdapterException $exception)
62+
{
63+
return array_search($exception, $this->exceptions, true) !== false;
64+
}
3765

3866
/**
3967
* Checks if any exception exists
4068
*
4169
* @return boolean
4270
*/
43-
public function hasExceptions();
71+
public function hasExceptions()
72+
{
73+
return !empty($this->exceptions);
74+
}
4475

4576
/**
4677
* Sets the exceptions
4778
*
4879
* @param HttpAdapterException[] $exceptions
4980
*/
50-
public function setExceptions(array $exceptions);
81+
public function setExceptions(array $exceptions)
82+
{
83+
$this->clearExceptions();
84+
$this->addExceptions($exceptions);
85+
}
5186

5287
/**
5388
* Adds an exception
5489
*
5590
* @param HttpAdapterException $exception
5691
*/
57-
public function addException(HttpAdapterException $exception);
92+
public function addException(HttpAdapterException $exception)
93+
{
94+
$this->exceptions[] = $exception;
95+
}
5896

5997
/**
6098
* Adds some exceptions
6199
*
62100
* @param HttpAdapterException[] $exceptions
63101
*/
64-
public function addExceptions(array $exceptions);
102+
public function addExceptions(array $exceptions)
103+
{
104+
foreach ($exceptions as $exception) {
105+
$this->addException($exception);
106+
}
107+
}
65108

66109
/**
67110
* Removes an exception
68111
*
69112
* @param HttpAdapterException $exception
70113
*/
71-
public function removeException(HttpAdapterException $exception);
114+
public function removeException(HttpAdapterException $exception)
115+
{
116+
unset($this->exceptions[array_search($exception, $this->exceptions, true)]);
117+
$this->exceptions = array_values($this->exceptions);
118+
}
72119

73120
/**
74121
* Removes some exceptions
75122
*
76123
* @param HttpAdapterException[] $exceptions
77124
*/
78-
public function removeExceptions(array $exceptions);
125+
public function removeExceptions(array $exceptions)
126+
{
127+
foreach ($exceptions as $exception) {
128+
$this->removeException($exception);
129+
}
130+
}
79131

80132
/**
81133
* Clears all exceptions
82134
*/
83-
public function clearExceptions();
135+
public function clearExceptions()
136+
{
137+
$this->exceptions = [];
138+
}
84139

85140
/**
86141
* Returns all responses
87142
*
88143
* @return ResponseInterface[]
89144
*/
90-
public function getResponses();
145+
public function getResponses()
146+
{
147+
return $this->responses;
148+
}
91149

92150
/**
93151
* Checks if a specific response exists
@@ -96,52 +154,82 @@ public function getResponses();
96154
*
97155
* @return boolean
98156
*/
99-
public function hasResponse(ResponseInterface $response);
157+
public function hasResponse(ResponseInterface $response)
158+
{
159+
return array_search($response, $this->responses, true) !== false;
160+
}
100161

101162
/**
102163
* Checks if any response exists
103164
*
104165
* @return boolean
105166
*/
106-
public function hasResponses();
167+
public function hasResponses()
168+
{
169+
return !empty($this->responses);
170+
}
107171

108172
/**
109173
* Sets the responses
110174
*
111175
* @param ResponseInterface[] $responses
112176
*/
113-
public function setResponses(array $responses);
177+
public function setResponses(array $responses)
178+
{
179+
$this->clearResponses();
180+
$this->addResponses($responses);
181+
}
114182

115183
/**
116184
* Adds a response
117185
*
118186
* @param ResponseInterface $response
119187
*/
120-
public function addResponse(ResponseInterface $response);
188+
public function addResponse(ResponseInterface $response)
189+
{
190+
$this->responses[] = $response;
191+
}
121192

122193
/**
123194
* Adds some responses
124195
*
125196
* @param ResponseInterface[] $responses
126197
*/
127-
public function addResponses(array $responses);
198+
public function addResponses(array $responses)
199+
{
200+
foreach ($responses as $response) {
201+
$this->addResponse($response);
202+
}
203+
}
128204

129205
/**
130206
* Removes a response
131207
*
132208
* @param ResponseInterface $response
133209
*/
134-
public function removeResponse(ResponseInterface $response);
210+
public function removeResponse(ResponseInterface $response)
211+
{
212+
unset($this->responses[array_search($response, $this->responses, true)]);
213+
$this->responses = array_values($this->responses);
214+
}
135215

136216
/**
137217
* Removes some responses
138218
*
139219
* @param ResponseInterface[] $responses
140220
*/
141-
public function removeResponses(array $responses);
221+
public function removeResponses(array $responses)
222+
{
223+
foreach ($responses as $response) {
224+
$this->removeResponse($response);
225+
}
226+
}
142227

143228
/**
144229
* Clears all responses
145230
*/
146-
public function clearResponses();
231+
public function clearResponses()
232+
{
233+
$this->responses = [];
234+
}
147235
}

0 commit comments

Comments
 (0)