Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 12, 2017
1 parent e26f5ef commit ad6cddc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
31 changes: 31 additions & 0 deletions src/BaseStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\TwitterStreamingApi;

abstract class BaseStream
{
/** @var \Spatie\TwitterStreamingApi\PhirehoseWrapper */
protected $stream;

protected function createStream(
string $accessToken,
string $accessSecret,
string $consumerKey,
string $consumerSecret,
string $filter
) : PhirehoseWrapper
{
return new PhirehoseWrapper(
$accessToken,
$accessSecret,
$consumerKey,
$consumerSecret,
$filter
);
}

public function startListening()
{
$this->stream->startListening();
}
}
11 changes: 8 additions & 3 deletions src/TwitterStream.php → src/PhirehoseWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use OauthPhirehose;
use Phirehose;

class TwitterStream extends OauthPhirehose
class PhirehoseWrapper extends OauthPhirehose
{
/** @var callable */
public $onStreamActivity;
protected $onStreamActivity;

public function __construct($accessToken, $accessSecret, $consumerKey, $consumerSecret, $method = Phirehose::METHOD_FILTER)
{
parent::__construct($accessToken, $accessSecret, $method);
parent::__construct($accessToken, $accessSecret, $consumerKey, $consumerKey);

$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
Expand All @@ -30,6 +30,11 @@ public function enqueueStatus($status)
($this->onStreamActivity)($status);
}

public function performOnStreamActivity(callable $onStreamActivity)
{
$this->onStreamActivity = $onStreamActivity;
}

public function startListening()
{
$this->consume();
Expand Down
17 changes: 10 additions & 7 deletions src/PublicStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@

use Phirehose;

class PublicStream extends TwitterStream
class PublicStream extends BaseStream
{
public function __construct($accessToken, $accessSecret, $consumerKey, $consumerSecret)
{
parent::__construct(


$this->stream = $this->createStream(
$accessToken,
$accessSecret,
$consumerKey,
$consumerSecret,
Phirehose::METHOD_FILTER);
Phirehose::METHOD_FILTER
);
}

/**
* @param string|array $listenFor
* @param callable $whenHears
Expand All @@ -24,13 +27,13 @@ public function __construct($accessToken, $accessSecret, $consumerKey, $consumer
*/
public function whenHears($listenFor, callable $whenHears)
{
if (! is_array($listenFor)) {
if (!is_array($listenFor)) {
$listenFor = [$listenFor];
}

$this->setTrack($listenFor);
$this->stream->setTrack($listenFor);

$this->onStreamActivity = $whenHears;
$this->stream->performOnStreamActivity($whenHears);

return $this;
}
Expand Down

0 comments on commit ad6cddc

Please sign in to comment.