Skip to content

Commit abbb7dc

Browse files
committed
[BC break] Handled team inbox message
1 parent bc764a8 commit abbb7dc

17 files changed

+938
-351
lines changed

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,39 @@ Composer will install the library to your project's `vendor/mremi` directory.
5252
```php
5353
<?php
5454

55-
use Mremi\Flowdock\Api\Push\Chat\Chat;
56-
use Mremi\Flowdock\Api\Push\Chat\Message;
55+
use Mremi\Flowdock\Api\Push\ChatMessage;
56+
use Mremi\Flowdock\Api\Push\Push;
5757

58-
$message = new Message;
59-
$message
58+
$message = ChatMessage::create()
6059
->setContent('This message has been sent with mremi/flowdock PHP library')
6160
->setExternalUserName('mremi')
6261
->addTag('#hello-world');
6362

64-
$chat = new Chat('your_flow_api_token');
63+
$push = new Push('your_flow_api_token');
6564

66-
if (!$chat->sendMessage($message, array('connect_timeout' => 1, 'timeout' => 1))) {
65+
if (!$push->sendChatMessage($message, array('connect_timeout' => 1, 'timeout' => 1))) {
66+
// handle errors...
67+
$message->getErrors();
68+
}
69+
```
70+
71+
### Team Inbox
72+
73+
```php
74+
<?php
75+
76+
use Mremi\Flowdock\Api\Push\Push;
77+
use Mremi\Flowdock\Api\Push\TeamInboxMessage;
78+
79+
$message = TeamInboxMessage::create()
80+
->setSource('source')
81+
->setFromAddress('[email protected]')
82+
->setSubject('subject')
83+
->setContent('This message has been sent with mremi/flowdock PHP library');
84+
85+
$push = new Push('your_flow_api_token');
86+
87+
if (!$push->sendTeamInboxMessage($message, array('connect_timeout' => 1, 'timeout' => 1))) {
6788
// handle errors...
6889
$message->getErrors();
6990
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
"phpunit/phpunit": "~4.1"
2020
},
2121
"autoload": {
22-
"psr-0": { "Mremi\\Flowdock": "src" }
22+
"psr-0": { "Mremi\\Flowdock": "src", "Mremi\\Flowdock\\Tests": "tests/" }
2323
}
2424
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ abstract class BaseMessage implements BaseMessageInterface
3333
*/
3434
protected $errors = array();
3535

36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public static function create()
40+
{
41+
return new static;
42+
}
43+
3644
/**
3745
* {@inheritdoc}
3846
*/

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@
1818
*/
1919
interface BaseMessageInterface
2020
{
21+
/**
22+
* Creates a new message instance
23+
*
24+
* @return static
25+
*/
26+
public static function create();
27+
2128
/**
2229
* Sets the content of the message
2330
*
2431
* @param string $content
2532
*
26-
* @return BaseMessageInterface
33+
* @return static
2734
*/
2835
public function setContent($content);
2936

@@ -39,7 +46,7 @@ public function getContent();
3946
*
4047
* @param string $tag
4148
*
42-
* @return BaseMessageInterface
49+
* @return static
4350
*/
4451
public function addTag($tag);
4552

@@ -55,7 +62,7 @@ public function getTags();
5562
*
5663
* @param string $error
5764
*
58-
* @return BaseMessageInterface
65+
* @return static
5966
*/
6067
public function addError($error);
6168

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

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

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

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

src/Mremi/Flowdock/Api/Push/Chat/Message.php renamed to src/Mremi/Flowdock/Api/Push/ChatMessage.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mremi\Flowdock\Api\Push\Chat;
13-
14-
use Mremi\Flowdock\Api\Push\BaseMessage;
12+
namespace Mremi\Flowdock\Api\Push;
1513

1614
/**
1715
* Chat message class
1816
*
1917
* @author Rémi Marseille <[email protected]>
2018
*/
21-
class Message extends BaseMessage implements MessageInterface
19+
class ChatMessage extends BaseMessage implements ChatMessageInterface
2220
{
2321
/**
2422
* @var string

src/Mremi/Flowdock/Api/Push/Chat/MessageInterface.php renamed to src/Mremi/Flowdock/Api/Push/ChatMessageInterface.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,21 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mremi\Flowdock\Api\Push\Chat;
13-
14-
use Mremi\Flowdock\Api\Push\BaseMessageInterface;
12+
namespace Mremi\Flowdock\Api\Push;
1513

1614
/**
1715
* Chat message interface
1816
*
1917
* @author Rémi Marseille <[email protected]>
2018
*/
21-
interface MessageInterface extends BaseMessageInterface
19+
interface ChatMessageInterface extends BaseMessageInterface
2220
{
2321
/**
2422
* Sets the name of the user sending the message
2523
*
2624
* @param string $externalUserName
2725
*
28-
* @return MessageInterface
26+
* @return ChatMessageInterface
2927
*/
3028
public function setExternalUserName($externalUserName);
3129

@@ -41,7 +39,7 @@ public function getExternalUserName();
4139
*
4240
* @param integer $messageId
4341
*
44-
* @return MessageInterface
42+
* @return ChatMessageInterface
4543
*/
4644
public function setMessageId($messageId);
4745

src/Mremi/Flowdock/Api/Push/Chat/Chat.php renamed to src/Mremi/Flowdock/Api/Push/Push.php

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,64 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Mremi\Flowdock\Api\Push\Chat;
12+
namespace Mremi\Flowdock\Api\Push;
1313

14+
use Guzzle\Http\Client;
1415
use Guzzle\Http\Message\Response;
1516

16-
use Mremi\Flowdock\Api\Push\BasePush;
17-
1817
/**
19-
* Chat class
18+
* Push class
2019
*
2120
* @author Rémi Marseille <[email protected]>
2221
*/
23-
class Chat extends BasePush implements ChatInterface
22+
class Push implements PushInterface
2423
{
24+
const BASE_CHAT_URL = 'https://api.flowdock.com/v1/messages/chat';
25+
const BASE_TEAM_INBOX_URL = 'https://api.flowdock.com/v1/messages/team_inbox';
26+
27+
/**
28+
* @var string
29+
*/
30+
private $flowApiToken;
31+
32+
/**
33+
* Constructor
34+
*
35+
* @param string $flowApiToken A flow API token
36+
*/
37+
public function __construct($flowApiToken)
38+
{
39+
$this->flowApiToken = $flowApiToken;
40+
}
41+
2542
/**
2643
* {@inheritdoc}
2744
*/
28-
public function sendMessage(MessageInterface $message, array $options = array())
45+
public function sendChatMessage(ChatMessageInterface $message, array $options = array())
46+
{
47+
return $this->sendMessage($message, self::BASE_CHAT_URL, $options);
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function sendTeamInboxMessage(TeamInboxMessageInterface $message, array $options = array())
54+
{
55+
return $this->sendMessage($message, self::BASE_TEAM_INBOX_URL, $options);
56+
}
57+
58+
/**
59+
* Sends a message to a flow
60+
*
61+
* @param BaseMessageInterface $message A message instance to send
62+
* @param string $baseUrl A base URL
63+
* @param array $options An array of options used by request
64+
*
65+
* @return boolean
66+
*/
67+
protected function sendMessage(BaseMessageInterface $message, $baseUrl, array $options = array())
2968
{
30-
$client = $this->createClient();
69+
$client = $this->createClient(sprintf('%s/%s', $baseUrl, $this->flowApiToken));
3170

3271
$request = $client->post(null, array(
3372
'Content-Type' => 'application/json'
@@ -40,23 +79,15 @@ public function sendMessage(MessageInterface $message, array $options = array())
4079
return $this->isValid($message, $response);
4180
}
4281

43-
/**
44-
* {@inheritdoc}
45-
*/
46-
protected function getApiUrl()
47-
{
48-
return sprintf('https://api.flowdock.com/v1/messages/chat/%s', $this->flowApiToken);
49-
}
50-
5182
/**
5283
* Returns TRUE whether the response is valid
5384
*
54-
* @param MessageInterface $message A message instance
55-
* @param Response $response A response instance
85+
* @param BaseMessageInterface $message A message instance
86+
* @param Response $response A response instance
5687
*
5788
* @return boolean
5889
*/
59-
protected function isValid(MessageInterface $message, Response $response)
90+
protected function isValid(BaseMessageInterface $message, Response $response)
6091
{
6192
if (200 !== $response->getStatusCode()) {
6293
$message->addError(sprintf('Status code of response is wrong, expected 200, got %s', $response->getStatusCode()));
@@ -78,4 +109,16 @@ protected function isValid(MessageInterface $message, Response $response)
78109

79110
return !$message->hasErrors();
80111
}
112+
113+
/**
114+
* Creates a client to interact with Flowdock API
115+
*
116+
* @param string $url
117+
*
118+
* @return Client
119+
*/
120+
protected function createClient($url)
121+
{
122+
return new Client($url);
123+
}
81124
}

0 commit comments

Comments
 (0)