Skip to content

Commit

Permalink
Merge pull request #61 from GetStream/fix/cs
Browse files Browse the repository at this point in the history
Fix CS
  • Loading branch information
hannesvdvreken authored Feb 22, 2018
2 parents 7cc6a3b + 2433d48 commit 9c2479a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 73 deletions.
27 changes: 16 additions & 11 deletions lib/GetStream/Stream/ActivityUpdateOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,36 @@

namespace GetStream\Stream;

use Exception;
use GuzzleHttp;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;

class ActivityUpdateOperation extends Feed
{
/**
* @var string
*/
protected $token;

/**
* @param Client $client
* @param string $api_key
* @param string $token
*/
public function __construct($client, $api_key, $token)
{
$this->client = $client;
$this->api_key = $api_key;
$this->token = $token;
}

/**
* @param string $resource
* @param string $action
*
* @return array
*/
protected function getHttpRequestHeaders($resource, $action)
{
$headers = parent::getHttpRequestHeaders($resource, $action);
$headers['Authorization'] = $this->token;

return $headers;
}

Expand All @@ -32,10 +41,6 @@ public function updateActivities($activities)
return;
}

$data = [
'activities' => $activities
];
return $this->makeHttpRequest('activities/', 'POST', $data);
return $this->makeHttpRequest('activities/', 'POST', compact('activities'));
}

}
46 changes: 31 additions & 15 deletions lib/GetStream/Stream/Analytics.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
<?php
namespace GetStream\Stream;

use Exception;
use GuzzleHttp;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;

namespace GetStream\Stream;

class Analytics extends Feed
{
const API_ENDPOINT = 'https://analytics.stream-io-api.com/analytics/';

/**
* @var string
*/
protected $token;

/**
* @param Client $client
* @param string $api_key
* @param string $token
*/
public function __construct($client, $api_key, $token)
{
$this->client = $client;
$this->api_key = $api_key;
$this->token = $token;
}

/**
* @param string $resource
* @param string $action
*
* @return array
*/
protected function getHttpRequestHeaders($resource, $action)
{
$headers = parent::getHttpRequestHeaders($resource, $action);
$headers['Authorization'] = $this->token;

return $headers;
}

/**
* @param string $targetUrl
* @param array $events
*
* @return string
*/
public function createRedirectUrl($targetUrl, $events)
{
$parsed_url = parse_url($targetUrl);
$query_params = $this->getHttpRequestHeaders('analytics', '*');
$query_params['api_key'] = $this->api_key;
$query_params['url'] = $targetUrl;
$query_params['auth_type'] = 'jwt';
$query_params['authorization'] = $query_params['Authorization'];
unset($query_params['Authorization']);
unset($query_params['stream-auth-type']);
unset($query_params['Content-Type']);
unset($query_params['X-Stream-Client']);
$query_params['events'] = json_encode($events);
$qString = http_build_query($query_params);
return static::API_ENDPOINT . 'redirect/' . "?{$qString}";
}

unset(
$query_params['Authorization'],
$query_params['stream-auth-type'],
$query_params['Content-Type'],
$query_params['X-Stream-Client']
);

return static::API_ENDPOINT . 'redirect/?' . http_build_query($query_params);
}
}
57 changes: 29 additions & 28 deletions lib/GetStream/Stream/BaseFeed.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace GetStream\Stream;

class BaseFeed
{

/**
* @var string
*/
Expand Down Expand Up @@ -66,13 +66,11 @@ public function __construct($client, $feed_slug, $user_id, $api_key, $token)
$this->token = $token;
$this->api_key = $api_key;

if ($client instanceof Client) {
$this->client = $client;
}
$this->client = $client;
}

/**
* @param $feed_slug
* @param string $feed_slug
*
* @return bool
*/
Expand All @@ -82,7 +80,7 @@ public function validFeedSlug($feed_slug)
}

/**
* @param $user_id
* @param string $user_id
*
* @return bool
*/
Expand Down Expand Up @@ -132,66 +130,69 @@ public function getUserId()
}

/**
* @param array $to
* @param array $to
*
* @return array
*/
public function signToField($to)
{
$recipients = [];
foreach ($to as $recipient) {
return array_map(function ($recipient) {
$bits = explode(':', $recipient);
$recipient_feed = $this->client->feed($bits[0], $bits[1]);
$recipient_token = $recipient_feed->getToken();
$recipients[] = "$recipient $recipient_token";
}
return $recipients;

return "$recipient $recipient_token";
}, $to);
}

/**
* @param array $activity_data
* @param array $activity
* @return mixed
*/
public function addActivity($activity_data)
public function addActivity($activity)
{
if (array_key_exists('to', $activity_data)) {
$activity_data['to'] = $this->signToField($activity_data['to']);
if (array_key_exists('to', $activity)) {
$activity['to'] = $this->signToField($activity['to']);
}
return $this->makeHttpRequest("{$this->base_feed_url}/", 'POST', $activity_data, null, 'feed', 'write');

return $this->makeHttpRequest("{$this->base_feed_url}/", 'POST', $activity, null, 'feed', 'write');
}

/**
* @param array $activities_data
* @param array $activities
* @return mixed
*/
public function addActivities($activities_data)
public function addActivities($activities)
{
foreach ($activities_data as $i => $activity) {
foreach ($activities as &$activity) {
if (array_key_exists('to', $activity)) {
$activities_data[$i]['to'] = $this->signToField($activity['to']);
$activity['to'] = $this->signToField($activity['to']);
}
}
$data = ['activities' => $activities_data];
return $this->makeHttpRequest("{$this->base_feed_url}/", 'POST', $data, null, 'feed', 'write');

return $this->makeHttpRequest("{$this->base_feed_url}/", 'POST', compact('activities'), null, 'feed', 'write');
}

/**
* @param int $activity_id
* @param bool $foreign_id
* @param int $activity_id
* @param bool $foreign_id
* @return mixed
*/
public function removeActivity($activity_id, $foreign_id = false)
{
$query_params = [];

if ($foreign_id === true) {
$query_params['foreign_id'] = 1;
}

return $this->makeHttpRequest("{$this->base_feed_url}/{$activity_id}/", 'DELETE', null, $query_params, 'feed', 'delete');
}

/**
* @param int $offset
* @param int $limit
* @param array $options
* @param int $offset
* @param int $limit
* @param array $options
* @return mixed
*/
public function getActivities($offset = 0, $limit = 20, $options = [])
Expand Down
3 changes: 2 additions & 1 deletion lib/GetStream/Stream/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class Client
* @param string $api_key
* @param string $api_secret
* @param string $api_version
* @param string $timeout
* @param string $location
* @param float $timeout
*/
public function __construct($api_key, $api_secret, $api_version='v1.0', $location='', $timeout=3.0)
{
Expand Down
10 changes: 6 additions & 4 deletions lib/GetStream/Stream/Feed.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace GetStream\Stream;

use GuzzleHttp\Client as GuzzleClient;
Expand Down Expand Up @@ -57,11 +58,12 @@ public function setGuzzleDefaultOption($option, $value)
protected function getHttpRequestHeaders($resource, $action)
{
$token = $this->client->createFeedJWTToken($this, $resource, $action);

return [
'Authorization' => $token,
'Content-Type' => 'application/json',
'stream-auth-type' => 'jwt',
'X-Stream-Client' => 'stream-php-client-' . VERSION
'Authorization' => $token,
'Content-Type' => 'application/json',
'stream-auth-type' => 'jwt',
'X-Stream-Client' => 'stream-php-client-' . VERSION,
];
}

Expand Down
11 changes: 7 additions & 4 deletions lib/GetStream/Stream/Signer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace GetStream\Stream;

use HttpSignatures\Context;
use \Firebase\JWT\JWT;

Expand Down Expand Up @@ -34,11 +36,11 @@ public function __construct($api_key, $api_secret)
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->hashFunction = new HMAC;
$this->context = new Context(array(
$this->context = new Context([
'keys' => array($api_key =>$api_secret),
'algorithm' => 'hmac-sha256',
'headers' => array('(request-target)', 'Date'),
));
]);
}

/**
Expand All @@ -57,6 +59,7 @@ public function urlSafeB64encode($value)
public function signature($value)
{
$digest = $this->hashFunction->digest($value, $this->api_secret);

return $this->urlSafeB64encode($digest);
}

Expand All @@ -71,9 +74,9 @@ public function jwtScopeToken($feedId, $resource, $action)
$payload = [
'action' => $action,
'feed_id' => $feedId,
'resource' => $resource
'resource' => $resource,
];

return JWT::encode($payload, $this->api_secret, 'HS256');
}

}
6 changes: 3 additions & 3 deletions tests/stubs/BaseFeedStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class BaseFeedStub extends BaseFeed
public function makeHttpRequest($uri, $method, $data = null, $query_params = null)
{
return [
'uri' => $uri,
'method' => $method,
'data' => $data,
'uri' => $uri,
'method' => $method,
'data' => $data,
'query_params' => $query_params,
];
}
Expand Down
Loading

0 comments on commit 9c2479a

Please sign in to comment.