Skip to content

Commit 725bd3a

Browse files
committed
[BC break] Improved handle of API response
1 parent abbb7dc commit 725bd3a

File tree

8 files changed

+155
-180
lines changed

8 files changed

+155
-180
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ $push = new Push('your_flow_api_token');
6464

6565
if (!$push->sendChatMessage($message, array('connect_timeout' => 1, 'timeout' => 1))) {
6666
// handle errors...
67-
$message->getErrors();
67+
$message->getResponseErrors();
6868
}
6969
```
7070

@@ -86,7 +86,7 @@ $push = new Push('your_flow_api_token');
8686

8787
if (!$push->sendTeamInboxMessage($message, array('connect_timeout' => 1, 'timeout' => 1))) {
8888
// handle errors...
89-
$message->getErrors();
89+
$message->getResponseErrors();
9090
}
9191
```
9292

src/Mremi/Flowdock/Api/Push/BaseMessage.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Mremi\Flowdock\Api\Push;
1313

14+
use Guzzle\Http\Message\Response;
15+
1416
/**
1517
* Base push message class
1618
*
@@ -29,9 +31,9 @@ abstract class BaseMessage implements BaseMessageInterface
2931
protected $tags = array();
3032

3133
/**
32-
* @var array
34+
* @var Response
3335
*/
34-
protected $errors = array();
36+
protected $response;
3537

3638
/**
3739
* {@inheritdoc}
@@ -80,27 +82,19 @@ public function getTags()
8082
/**
8183
* {@inheritdoc}
8284
*/
83-
public function addError($error)
85+
public function setResponse(Response $response)
8486
{
85-
$this->errors[] = $error;
87+
$this->response = $response;
8688

8789
return $this;
8890
}
8991

9092
/**
9193
* {@inheritdoc}
9294
*/
93-
public function getErrors()
95+
public function getResponse()
9496
{
95-
return $this->errors;
96-
}
97-
98-
/**
99-
* {@inheritdoc}
100-
*/
101-
public function hasErrors()
102-
{
103-
return count($this->errors) > 0;
97+
return $this->response;
10498
}
10599

106100
/**
@@ -111,7 +105,7 @@ public function getData()
111105
$array = $this->toArray();
112106

113107
// to be consistent with the Flowdock API
114-
unset($array['errors']);
108+
unset($array['response']);
115109

116110
return $array;
117111
}
@@ -128,6 +122,48 @@ public function toArray()
128122
return array_combine($keys, array_values($array));
129123
}
130124

125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function getResponseBody()
129+
{
130+
if (null === $this->response) {
131+
return array();
132+
}
133+
134+
$body = json_decode($this->response->getBody(true), true);
135+
136+
return is_array($body) ? $body : array();
137+
}
138+
139+
/**
140+
* {@inheritdoc}
141+
*/
142+
public function getResponseMessage()
143+
{
144+
$body = $this->getResponseBody();
145+
146+
return array_key_exists('message', $body) ? $body['message'] : null;
147+
}
148+
149+
/**
150+
* {@inheritdoc}
151+
*/
152+
public function getResponseErrors()
153+
{
154+
$body = $this->getResponseBody();
155+
156+
return array_key_exists('errors', $body) ? $body['errors'] : array();
157+
}
158+
159+
/**
160+
* {@inheritdoc}
161+
*/
162+
public function hasResponseErrors()
163+
{
164+
return count($this->getResponseErrors()) > 0;
165+
}
166+
131167
/**
132168
* A string to underscore.
133169
*

src/Mremi/Flowdock/Api/Push/BaseMessageInterface.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Mremi\Flowdock\Api\Push;
1313

14+
use Guzzle\Http\Message\Response;
15+
1416
/**
1517
* Base push message interface
1618
*
@@ -58,39 +60,60 @@ public function addTag($tag);
5860
public function getTags();
5961

6062
/**
61-
* Adds an error to the message
63+
* Sets the Flowdock response
6264
*
63-
* @param string $error
65+
* @param Response $response
6466
*
6567
* @return static
6668
*/
67-
public function addError($error);
69+
public function setResponse(Response $response);
70+
71+
/**
72+
* Gets the Flowdock response
73+
*
74+
* @return Response
75+
*/
76+
public function getResponse();
6877

6978
/**
70-
* Gets the message errors
79+
* Returns an array representation of the message data
7180
*
7281
* @return array
7382
*/
74-
public function getErrors();
83+
public function getData();
7584

7685
/**
77-
* Returns TRUE whether the message has some errors
86+
* Returns an array representation of the message
7887
*
79-
* @return boolean
88+
* @return array
8089
*/
81-
public function hasErrors();
90+
public function toArray();
8291

8392
/**
84-
* Returns an array representation of the message data
93+
* Gets the Flowdock response body
8594
*
8695
* @return array
8796
*/
88-
public function getData();
97+
public function getResponseBody();
8998

9099
/**
91-
* Returns an array representation of the message
100+
* Gets the Flowdock response message
101+
*
102+
* @return string|null
103+
*/
104+
public function getResponseMessage();
105+
106+
/**
107+
* Gets the Flowdock response errors
92108
*
93109
* @return array
94110
*/
95-
public function toArray();
111+
public function getResponseErrors();
112+
113+
/**
114+
* Returns TRUE whether the Flowdock response has some errors
115+
*
116+
* @return boolean
117+
*/
118+
public function hasResponseErrors();
96119
}

src/Mremi/Flowdock/Api/Push/Push.php

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,40 +74,9 @@ protected function sendMessage(BaseMessageInterface $message, $baseUrl, array $o
7474
$message->getData()
7575
), $options);
7676

77-
$response = $request->send();
77+
$message->setResponse($request->send());
7878

79-
return $this->isValid($message, $response);
80-
}
81-
82-
/**
83-
* Returns TRUE whether the response is valid
84-
*
85-
* @param BaseMessageInterface $message A message instance
86-
* @param Response $response A response instance
87-
*
88-
* @return boolean
89-
*/
90-
protected function isValid(BaseMessageInterface $message, Response $response)
91-
{
92-
if (200 !== $response->getStatusCode()) {
93-
$message->addError(sprintf('Status code of response is wrong, expected 200, got %s', $response->getStatusCode()));
94-
}
95-
96-
if ($response->hasHeader('content-type')) {
97-
if (array('application/json; charset=utf-8') !== $response->getHeader('content-type')->toArray()) {
98-
$message->addError(sprintf('Content type of response is wrong, expected "application/json; charset=utf-8", got %s',
99-
json_encode($response->getHeader('content-type')->toArray())
100-
));
101-
}
102-
} else {
103-
$message->addError(sprintf('Could not find header "content-type" in response, got %s', json_encode($response->getHeaderLines())));
104-
}
105-
106-
if ('{}' !== $response->getBody(true)) {
107-
$message->addError(sprintf('Response body is wrong, expected "{}", got "%s"', $response->getBody(true)));
108-
}
109-
110-
return !$message->hasErrors();
79+
return !$message->hasResponseErrors();
11180
}
11281

11382
/**

tests/Mremi/Flowdock/Tests/Api/Push/BaseMessageTest.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Mremi\Flowdock\Tests\Api\Push;
1313

14+
use Guzzle\Http\Message\Response;
15+
1416
use Mremi\Flowdock\Api\Push\BaseMessageInterface;
1517

1618
/**
@@ -51,9 +53,14 @@ public function testFreshMessage()
5153
$this->assertTrue(is_array($this->message->getTags()));
5254
$this->assertCount(0, $this->message->getTags());
5355

54-
$this->assertTrue(is_array($this->message->getErrors()));
55-
$this->assertCount(0, $this->message->getErrors());
56-
$this->assertFalse($this->message->hasErrors());
56+
$this->assertTrue(is_array($this->message->getResponseBody()));
57+
$this->assertCount(0, $this->message->getResponseBody());
58+
59+
$this->assertNull($this->message->getResponseMessage());
60+
61+
$this->assertTrue(is_array($this->message->getResponseErrors()));
62+
$this->assertCount(0, $this->message->getResponseErrors());
63+
$this->assertFalse($this->message->hasResponseErrors());
5764
}
5865

5966
/**
@@ -71,19 +78,35 @@ public function testAddTags()
7178
}
7279

7380
/**
74-
* Tests to add some errors to a message
81+
* Tests a valid response
82+
*/
83+
public function testValidResponse()
84+
{
85+
$this->message->setResponse(new Response(200, null, '{"dummy": "ok"}'));
86+
87+
$this->assertEquals(array('dummy' => 'ok'), $this->message->getResponseBody());
88+
89+
$this->assertNull($this->message->getResponseMessage());
90+
91+
$this->assertTrue(is_array($this->message->getResponseErrors()));
92+
$this->assertCount(0, $this->message->getResponseErrors());
93+
$this->assertFalse($this->message->hasResponseErrors());
94+
}
95+
96+
/**
97+
* Tests an invalid response
7598
*/
76-
public function testAddErrors()
99+
public function testInvalidResponse()
77100
{
78-
$this->message->addError('Error #1');
79-
$this->assertCount(1, $this->message->getErrors());
80-
$this->assertEquals(array('Error #1'), $this->message->getErrors());
81-
$this->assertTrue($this->message->hasErrors());
82-
83-
$this->message->addError('Error #2');
84-
$this->assertCount(2, $this->message->getErrors());
85-
$this->assertEquals(array('Error #1', 'Error #2'), $this->message->getErrors());
86-
$this->assertTrue($this->message->hasErrors());
101+
$this->message->setResponse(new Response(400, null, '{"message": "Validation error", "errors": {"content": ["can\'t be blank"]}}'));
102+
103+
$this->assertEquals(array('message' => 'Validation error', 'errors' => array('content' => array('can\'t be blank'))), $this->message->getResponseBody());
104+
105+
$this->assertEquals('Validation error', $this->message->getResponseMessage());
106+
107+
$this->assertTrue(is_array($this->message->getResponseErrors()));
108+
$this->assertEquals(array('content' => array('can\'t be blank')), $this->message->getResponseErrors());
109+
$this->assertTrue($this->message->hasResponseErrors());
87110
}
88111

89112
/**

tests/Mremi/Flowdock/Tests/Api/Push/ChatMessageTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Mremi\Flowdock\Tests\Api\Push;
1313

14+
use Guzzle\Http\Message\Response;
15+
1416
use Mremi\Flowdock\Api\Push\ChatMessage;
1517

1618
/**
@@ -39,8 +41,7 @@ public function testGetData()
3941
->addTag('tag1')
4042
->addTag('tag2')
4143
->setMessageId(1)
42-
->addError('Error #1')
43-
->addError('Error #2');
44+
->setResponse(new Response(200));
4445

4546
$expected = array(
4647
'content' => $this->message->getContent(),
@@ -63,15 +64,14 @@ public function testToArray()
6364
->addTag('tag1')
6465
->addTag('tag2')
6566
->setMessageId(1)
66-
->addError('Error #1')
67-
->addError('Error #2');
67+
->setResponse(new Response(200));
6868

6969
$expected = array(
7070
'content' => $this->message->getContent(),
7171
'external_user_name' => $this->message->getExternalUserName(),
7272
'tags' => $this->message->getTags(),
7373
'message_id' => $this->message->getMessageId(),
74-
'errors' => $this->message->getErrors(),
74+
'response' => $this->message->getResponse(),
7575
);
7676

7777
$this->assertEquals($expected, $this->message->toArray());

0 commit comments

Comments
 (0)