Skip to content

Commit

Permalink
Support multiple Stream applications
Browse files Browse the repository at this point in the history
  • Loading branch information
zyml committed Dec 8, 2016
1 parent 4eca500 commit 0706545
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 63 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
"role": "Developer"
}
],
"repositories": [
{
"type": "vcs",
"url": "[email protected]:techinasia/laravel-stream.git"
}
],
"require": {
"php": ">=5.6.4",
"illuminate/notifications": "^5.3",
"illuminate/queue": "^5.1|^5.2|^5.3",
"illuminate/support": "^5.1|^5.2|^5.3",
"get-stream/stream": "^2.2"
"techinasia/laravel-stream": "dev-master"
},
"require-dev": {
"mockery/mockery": "^0.9.5",
Expand Down
8 changes: 0 additions & 8 deletions config/services.php

This file was deleted.

20 changes: 11 additions & 9 deletions src/StreamChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace NotificationChannels\GetStream;

use GetStream\Stream\Client;
use Techinasia\GetStream\StreamManager;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;

class StreamChannel
{
/** @var \GetStream\Stream\Client */
protected $client;
/** @var \Techinasia\GetStream\StreamManager */
protected $manager;

/**
* Constructs an instance of the channel.
*
* @param \GetStream\Stream\Client $client
* @param \Techinasia\GetStream\StreamManager $manager
*/
public function __construct(Client $client)
public function __construct(StreamManager $manager)
{
$this->client = $client;
$this->manager = $manager;
}

/**
Expand All @@ -32,10 +32,12 @@ public function send($notifiable, Notification $notification)
{
$args = $notifiable->routeNotificationFor('Stream');

$data = $notification->toStream($notifiable)
$payload = $notification->toStream($notifiable)
->toArray();

$feed = $this->client->feed(Arr::get($args, 'type'), Arr::get($args, 'id'));
$feed->addActivity($data);
$client = $this->manager->application(Arr::get($payload, 'application'));

$feed = $client->feed(Arr::get($args, 'type'), Arr::get($args, 'id'));
$feed->addActivity(Arr::get($payload, 'data'));
}
}
31 changes: 30 additions & 1 deletion src/StreamMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@

class StreamMessage implements Arrayable
{
/** @var string */
protected $application;

/** @var array */
protected $data = [];

/**
* Constructs an instance of StreamMessage.
*
* @param string $application
*/
public function __construct($application = null)
{
$this->application = $application;
}

/**
* Overload methods to have setters for data payload.
*
Expand All @@ -32,6 +45,19 @@ public function __call($name, array $arguments)
return $this;
}

/**
* Set Stream application.
*
* @param string $name
* @return $this
*/
public function application($name = '')
{
$this->application = $name;

return $this;
}

/**
* Support DateTime objects for `time` attribute.
*
Expand Down Expand Up @@ -59,6 +85,9 @@ public function time($time = 'now')
*/
public function toArray()
{
return $this->data;
return [
'application' => $this->application,
'data' => $this->data
];
}
}
38 changes: 0 additions & 38 deletions src/StreamServiceProvider.php

This file was deleted.

11 changes: 8 additions & 3 deletions tests/StreamChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
use Mockery;
use NotificationChannels\GetStream\StreamChannel;
use Orchestra\Testbench\TestCase;
use Techinasia\GetStream\StreamManager;

class StreamChannelTest extends TestCase
{
public function setUp()
{
parent::setUp();

$this->client = Mockery::mock(Client::class);
$this->channel = new StreamChannel($this->client);
$this->manager = Mockery::mock(StreamManager::class);
$this->channel = new StreamChannel($this->manager);
}

public function tearDown()
Expand All @@ -29,7 +30,11 @@ public function testSend()
$feedMock = Mockery::mock(Feed::class);
$feedMock->shouldReceive('addActivity');

$this->client->shouldReceive('feed')->andReturn($feedMock);
$client = Mockery::mock(Client::class);
$client->shouldReceive('feed')->andReturn($feedMock);

$this->manager->shouldReceive('application')->andReturn($client);

$this->channel->send(new TestNotifiable(), new TestNotification());
}
}
20 changes: 17 additions & 3 deletions tests/StreamMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@

class StreamMessageTest extends \PHPUnit_Framework_TestCase
{
/** @test */
public function testConstructor()
{
$message = new StreamMessage('test');
$this->assertEquals('test', $message->toArray()['application']);
}

/** @test */
public function testApplication()
{
$message = (new StreamMessage())->application('test');
$this->assertEquals('test', $message->toArray()['application']);
}

/** @test */
public function testMethodOverloading()
{
$message = (new StreamMessage())->verb('applied');
$this->assertEquals('applied', $message->toArray()['verb']);
$this->assertEquals('applied', $message->toArray()['data']['verb']);
}

/** @test */
public function testTimeWithDateTime()
{
$message = (new StreamMessage())->time(new \DateTime('2020-01-01'));
$this->assertEquals('2020-01-01T00:00:00+0000', $message->toArray()['time']);
$this->assertEquals('2020-01-01T00:00:00+0000', $message->toArray()['data']['time']);
}

/**
Expand All @@ -33,6 +47,6 @@ public function testTimeWithInvalidString()
public function testTimeWithValidString()
{
$message = (new StreamMessage())->time('2020-01-01');
$this->assertEquals('2020-01-01T00:00:00+0000', $message->toArray()['time']);
$this->assertEquals('2020-01-01T00:00:00+0000', $message->toArray()['data']['time']);
}
}

0 comments on commit 0706545

Please sign in to comment.