Skip to content

Commit

Permalink
Merge pull request #3 from asiries335/mvp-3
Browse files Browse the repository at this point in the history
mvp-3
  • Loading branch information
asiries335 authored Sep 6, 2020
2 parents cccea08 + 7e88a53 commit 3573d15
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,31 @@ _Start working_

<?php

// Class predis.
$redisClient = new Redis();
$redisClient->connect('127.0.0.1', '6379');
class Config implements \Asiries335\redisSteamPhp\ClientRedisStreamPhpInterface {

private $client;

$client = new \Asiries335\redisSteamPhp\Client($redisClient);
public function __construct()
{
$this->client = new \Redis();
$this->client->connect('127.0.0.1', '6379');
}

/**
* Method for run command of redis
*
* @param string $command
* @param mixed ...$args
*
* @return mixed
*/
public function call(string $command, ...$args)
{
return $this->client->rawCommand($command, ...$args);
}
}

$client = new \Asiries335\redisSteamPhp\Client(new Config());
```

_Add a message to stream_
Expand Down
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ final class Client
{

/**
* Client.
* Client Redis Interface.
*
* @var Redis
* @var ClientRedisStreamPhpInterface
*/
private $_client;

/**
* Client constructor.
*
* @param \Redis $redisClient Redis
* @param ClientRedisStreamPhpInterface $redisClient Client redis interface
*/
public function __construct(\Redis $redisClient)
public function __construct(ClientRedisStreamPhpInterface $redisClient)
{
$this->_client = $redisClient;
}
Expand Down
17 changes: 17 additions & 0 deletions src/ClientRedisStreamPhpInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace Asiries335\redisSteamPhp;

interface ClientRedisStreamPhpInterface
{
/**
* Call
*
* @param string $command
* @param mixed ...$args
*
* @return mixed
*/
public function call(string $command, ...$args);
}
18 changes: 9 additions & 9 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
use Asiries335\redisSteamPhp\Data\Message;
use Asiries335\redisSteamPhp\Hydrator\CollectionHydrator;
use Asiries335\redisSteamPhp\Hydrator\MessageHydrator;
use Redis;


final class Stream
{
/**
* Client
*
* @var Redis
* @var ClientRedisStreamPhpInterface
*/
private $_client;

Expand All @@ -36,10 +36,10 @@ final class Stream
/**
* Stream constructor.
*
* @param Redis $client Redis Client
* @param string $nameStream Name stream
* @param ClientRedisStreamPhpInterface $client ClientRedisInterface
* @param string $nameStream Name stream
*/
public function __construct(\Redis $client, string $nameStream)
public function __construct(ClientRedisStreamPhpInterface $client, string $nameStream)
{
$this->_client = $client;
$this->_streamName = $nameStream;
Expand All @@ -60,7 +60,7 @@ public function __construct(\Redis $client, string $nameStream)
public function add(string $key, array $values) : string
{
try {
return (string) $this->_client->rawCommand(
return (string) $this->_client->call(
Constants::COMMAND_XADD,
$this->_streamName,
'*',
Expand All @@ -86,7 +86,7 @@ public function add(string $key, array $values) : string
public function delete(string $key) : int
{
try {
return (int) $this->_client->rawCommand(
return (int) $this->_client->call(
Constants::COMMAND_XDEL,
$this->_streamName,
$key
Expand All @@ -108,7 +108,7 @@ public function delete(string $key) : int
public function get() : Collection
{
try {
$items = $this->_client->rawCommand(
$items = $this->_client->call(
Constants::COMMAND_XREAD,
'STREAMS',
$this->_streamName,
Expand Down Expand Up @@ -146,7 +146,7 @@ public function listen(\Closure $closure) : void
$loop->addPeriodicTimer(
Constants::TIME_TICK_INTERVAL,
function () use ($closure, $messageHydrate, $loop) {
$rows = $this->_client->rawCommand(
$rows = $this->_client->call(
Constants::COMMAND_XRANGE,
$this->_streamName,
'-',
Expand Down
11 changes: 6 additions & 5 deletions tests/Unit/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Asiries335\redisSteamPhp\Tests;

use Asiries335\redisSteamPhp\ClientRedisStreamPhpInterface;
use Asiries335\redisSteamPhp\Data\Collection;
use Asiries335\redisSteamPhp\Stream;
use PHPUnit\Framework\TestCase;
Expand All @@ -20,7 +21,7 @@ class StreamTest extends TestCase
*/
public function setUp() : void
{
$this->client = \Mockery::mock(\Redis::class);
$this->client = \Mockery::mock(ClientRedisStreamPhpInterface::class);
}

/**
Expand All @@ -32,7 +33,7 @@ public function setUp() : void
*/
public function testReadEmptyStream() : void
{
$this->client->shouldReceive('rawCommand')->andReturn([]);
$this->client->shouldReceive('call')->andReturn([]);

$stream = new Stream($this->client, self::TEST_NAME_STREAM);

Expand All @@ -58,7 +59,7 @@ public function testAddDataToStream() : void
'age' => 25,
];

$this->client->shouldReceive('rawCommand')->andReturn($key);
$this->client->shouldReceive('call')->andReturn($key);

$stream = new Stream($this->client, self::TEST_NAME_STREAM);

Expand Down Expand Up @@ -89,7 +90,7 @@ public function testReadStream() : void
]
];

$this->client->shouldReceive('rawCommand')->andReturn($data);
$this->client->shouldReceive('call')->andReturn($data);

$stream = new Stream($this->client, self::TEST_NAME_STREAM);

Expand All @@ -116,7 +117,7 @@ public function testDeleteMessage() : void
'age' => 25,
];

$this->client->shouldReceive('rawCommand')->andReturn($key);
$this->client->shouldReceive('call')->andReturn($key);

$stream = new Stream($this->client, self::TEST_NAME_STREAM);

Expand Down

0 comments on commit 3573d15

Please sign in to comment.