Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send Message to User #8

Merged
merged 1 commit into from
Aug 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/TwitterChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ public function __construct(Client $client, Twitter $twitter)
public function send($notifiable, Notification $notification)
{
$twitterMessage = $notification->toTwitter($notifiable);

$response = $this->twitter->connection->post(
'statuses/update', ['status' => $twitterMessage->getContent()]);
if ($twitterMessage->getReceiver() != null) {
$response = $this->twitter->connection->post('direct_messages/new', [
'text' => $twitterMessage->getContent(),
'screen_name' => $twitterMessage->getReceiver(),
]);
} else {
$response = $this->twitter->connection->post(
'statuses/update', ['status' => $twitterMessage->getContent()]);
}

if (isset($response->errors)) {
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
Expand Down
30 changes: 26 additions & 4 deletions src/TwitterMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ class TwitterMessage
{
/** @var string */
protected $content;
protected $to;

/**
* @param string $content
*
* @return static
*/
public static function create($content = '')
public static function create()
{
return new static($content);
$args = func_get_args();
if (count($args) == 2) {
return new static($args[0], $args[1]);
} else {
return new static($args[0]);
}
}

/*
* @param string $content
*/
public function __construct($content = '')
public function __construct()
{
$this->content = $content;
$args = func_get_args();
if (count($args) == 2) {
$this->to = $args[0];
$this->content = $args[1];
} else {
$this->content = $args[0];
}
}

/**
Expand All @@ -34,4 +46,14 @@ public function getContent()
{
return $this->content;
}

/**
* Get Twitter message receiver.
*
* @return string
*/
public function getReceiver()
{
return $this->to;
}
}
9 changes: 9 additions & 0 deletions tests/TwitterMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public function it_accepts_a_message_when_constructing_a_message()

$this->assertEquals('myMessage', $message->getContent());
}

/** @test */
public function it_accepts_a_message_to_user_when_constructing_a_user_with_message()
{
$message = new TwitterMessage('twitterUser', 'myMessage');

$this->assertEquals('myMessage', $message->getContent());
$this->assertEquals('twitterUser', $message->getReceiver());
}
}