Skip to content

Instantly share code, notes, and snippets.

@tw3eX
Created October 9, 2015 04:56
Show Gist options
  • Save tw3eX/82027ecb97fc3b70b277 to your computer and use it in GitHub Desktop.
Save tw3eX/82027ecb97fc3b70b277 to your computer and use it in GitHub Desktop.
First
<?php namespace RedisCommunucate;
class Communicate implements \Push\ICommunicated {
private $redisClient;
public function __construct($redisClient){
$this->redisClient = $redisClient;
}
public function communicate($projectId, $data)
{
$toSend = array(
"project" => $projectId
);
$data['data'] = json_decode($data['data']);
$to_send = array_merge($toSend, $data);
$to_send['data'] = array($toSend['data']);
$this->redisClient->rpush("centrifugo.api", json_encode($toSend));
}
}
{
"name": "Centrifugo test",
"type": "project",
"description": "Centrifugo test",
"license": "MIT",
"authors": [
{
"name": "Valeriy Zakharov",
"email": "[email protected]",
"homepage": "http://nothing-extra.ru",
"role": "Developer"
}
],
"require": {
"predis/predis": "^1.0"
},
"autoload": {
"classmap": [
"Push",
"RedisCommunucate"
],
"scripts": {
"post-update-cmd": [
"composer dump-autoload"
]
}
}
}
<?php namespace Push;
interface ICommunicated {
public function communicate($projectId,$data);
}
<?php
namespace Push;
class Push{
protected $projectSecret;
private $projectKey;
private $communicate;
public function __construct($project, $secret, ICommunicated $communicate )
{
$this->setProject($project, $secret)->setCommunicate($communicate);
}
private function setProject($projectKey, $projectSecret)
{
$this->projectKey = $projectKey;
$this->projectSecret = $projectSecret;
return $this;
}
public function setCommunicate(ICommunicated $communicate)
{
$this->communicate = $communicate;
return $this;
}
public function publish($channel, $data = [])
{
return $this->send("publish", ["channel" => $channel, "data" => $data]);
}
public function send($method, $params = [])
{
if (empty($params)) {
$params = new \StdClass();
}
$data = json_encode(["method" => $method, "params" => $params]);
return
$this->communicate
->communicate(
$this->projectKey,
["data" => $data]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment