Skip to content

Commit

Permalink
Merge pull request #2 from mice-tm/master
Browse files Browse the repository at this point in the history
added versions support
  • Loading branch information
andrey-tm authored Nov 7, 2018
2 parents e169507 + a75cf99 commit 8ce0603
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.2.0] - 2018-11-07
### Added
- Versionized events support

## [1.1.0] - 2018-10-25
### Added
- Unsubscribe method optional parameter
Expand All @@ -14,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Base component implementation

[Unreleased]: https://github.com/andrey-tm/yii2-service-events-client/compare/1.0.0...HEAD
[Unreleased]: https://github.com/andrey-tm/yii2-service-events-client/compare/1.2.0...HEAD
[1.2.0]: https://github.com/andrey-tm/yii2-service-events-client/compare/1.1.0...1.2.0
[1.1.0]: https://github.com/andrey-tm/yii2-service-events-client/compare/1.0.0...1.1.0
[1.0.0]: https://github.com/andrey-tm/yii2-service-events-client/tree/1.0.0
30 changes: 25 additions & 5 deletions services/EventsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,50 @@ public function __construct(
/**
* @inheritdoc
*/
public function fire(string $event, array $data)
public function fire(string $event, array $data, string $version = null)
{
$this->sendRequest(
'events',
[
'name' => $event,
'data' => $data,
'version' => $version
]
);
}

/**
* @inheritdoc
*/
public function subscribe(string $event, string $endpoint, string $method = 'post')
public function subscribe(string $event, string $endpoint, string $method = 'post', string $version = null)
{
$this->sendRequest(
'event-subscriptions',
[
'event' => $event,
'endpoint' => $endpoint,
'method' => $method,
'version' => $version,
]
);
}

/**
* @inheritdoc
*/
public function unsubscribe(string $event, string $endpoint, $method = null)
public function unsubscribe(string $event, string $endpoint)
{
$this->sendRequest(
'event-subscriptions/' . $event .
($method ? '/' . $method : '') . '/' . urlencode(trim($endpoint, " \t\n\r\0\x0B\/")),
'event-subscriptions/' . $event . '/' . $this->urlEncode($endpoint),
[],
'delete'
);
}

public function unsubscribeVersionized(string $event, string $endpoint, string $method, string $version)
{
$this->sendRequest(
'event-subscriptions/' . $event . '/' . $version . '/' . $method . '/' . $this->urlEncode($endpoint),
[],
'delete'
);
Expand Down Expand Up @@ -156,4 +166,14 @@ protected function requestClientAccessToken(): string
$this->accessToken = $response['access_token'];
return $response['access_token'];
}

/**
* Encode and trim endpoint
* @param $endpoint
* @return string
*/
private function urlEncode($endpoint)
{
return urlencode(trim($endpoint, " \t\n\r\0\x0B\/"));
}
}
19 changes: 15 additions & 4 deletions services/EventsServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,35 @@ interface EventsServiceInterface
*
* @param string $event Event name
* @param array $data Event data
* @param string|null $version Event version
*/
public function fire(string $event, array $data);
public function fire(string $event, array $data, string $version = null);

/**
* Add subscription on event
*
* @param string $event Event name
* @param string $endpoint Event endpoint
* @param string $method Http method
* @param string $version|null Event version
*/
public function subscribe(string $event, string $endpoint, string $method = 'post');
public function subscribe(string $event, string $endpoint, string $method = 'post', string $version = null);

/**
* Remove subscription on event
*
* @param string $event Event name
* @param string $endpoint Event endpoint
* @param string|null $method
*/
public function unsubscribe(string $event, string $endpoint, $method = null);
public function unsubscribe(string $event, string $endpoint);

/**
* Remove versionized subscription on event
*
* @param string $event Event name
* @param string $endpoint Event endpoint
* @param string $method
* @param string $version
*/
public function unsubscribeVersionized(string $event, string $endpoint, string $method, string $version);
}

0 comments on commit 8ce0603

Please sign in to comment.