Skip to content

Commit

Permalink
Removed RedisCounter and added a native counter
Browse files Browse the repository at this point in the history
  • Loading branch information
PeeHaa committed Mar 4, 2017
1 parent c6bc004 commit 58eb617
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 298 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"nikic/fast-route": "^1",
"rdlowrey/auryn": "^1",
"amphp/aerys": "^0.4.4",
"amphp/artax": "^2",
"amphp/redis": "^0.2.5"
"amphp/artax": "^2"
},
"require-dev": {
"phpunit/phpunit": "^5",
Expand Down
52 changes: 1 addition & 51 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Auryn\Injector;
use ekinhbayar\GitAmp\Github\Credentials;
use ekinhbayar\GitAmp\Storage\Counter;
use ekinhbayar\GitAmp\Storage\RedisCounter;
use ekinhbayar\GitAmp\Storage\NativeCounter;
use ekinhbayar\GitAmp\Websocket\Handler;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
Expand All @@ -14,13 +14,11 @@
use function Aerys\router;
use function Aerys\websocket;

//require_once __DIR__ . '/vendor/autoload.php';

$configuration = require_once __DIR__ . '/config.php';

$injector = new Injector;

$injector->alias(Counter::class, RedisCounter::class);
$injector->alias(Counter::class, NativeCounter::class);

$injector->alias(Credentials::class, get_class($configuration['github']));

Expand Down
14 changes: 9 additions & 5 deletions src/Storage/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

use Amp\Promise;

interface Counter {
public function increment(string $key): Promise;
public function decrement(string $key): Promise;
public function get(string $key): Promise;
public function set(string $key, int $val): Promise;
interface Counter
{
public function increment();

public function decrement();

public function get(): int;

public function set(int $val);
}
28 changes: 28 additions & 0 deletions src/Storage/NativeCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace ekinhbayar\GitAmp\Storage;

class NativeCounter implements Counter
{
private $counter = 0;

public function increment()
{
$this->counter++;
}

public function decrement()
{
$this->counter--;
}

public function get(): int
{
return $this->counter;
}

public function set(int $val)
{
$this->counter = $val;
}
}
59 changes: 0 additions & 59 deletions src/Storage/RedisCounter.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Storage/StorageFailedException.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Websocket/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function onStart(Endpoint $endpoint)
{
$this->endpoint = $endpoint;

$this->counter->set('connected_users', 0);
$this->counter->set(0);

repeat(function() {
$this->emit(yield $this->gitamp->listen());
Expand All @@ -55,9 +55,9 @@ public function onOpen(int $clientId, $handshakeData)
{
$this->emit(yield $this->gitamp->listen());

yield $this->counter->increment('connected_users');
$this->counter->increment();

$this->sendConnectedUsersCount(yield $this->counter->get('connected_users'));
$this->sendConnectedUsersCount($this->counter->get());
}

private function emit(Results $events)
Expand All @@ -81,9 +81,9 @@ public function onData(int $clientId, Websocket\Message $msg)

public function onClose(int $clientId, int $code, string $reason)
{
yield $this->counter->decrement('connected_users');
$this->counter->decrement();

$this->sendConnectedUsersCount(yield $this->counter->get('connected_users'));
$this->sendConnectedUsersCount($this->counter->get());
}

public function onStop()
Expand Down
61 changes: 61 additions & 0 deletions tests/Storage/NativeCounterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace ekinhbayar\GitAmpTests\Storage;

use ekinhbayar\GitAmp\Storage\NativeCounter;
use PHPUnit\Framework\TestCase;

class NativeCounterTest extends TestCase
{
public function testGet()
{
$nativeCounter = new NativeCounter();

$this->assertSame(0, $nativeCounter->get());
}

public function testSet()
{
$nativeCounter = new NativeCounter();

$nativeCounter->set(5);

$this->assertSame(5, $nativeCounter->get());
}

public function testIncrement()
{
$nativeCounter = new NativeCounter();

$nativeCounter->increment();

$this->assertSame(1, $nativeCounter->get());

$nativeCounter->increment();

$this->assertSame(2, $nativeCounter->get());

$nativeCounter->increment();

$this->assertSame(3, $nativeCounter->get());
}

public function testDecrement()
{
$nativeCounter = new NativeCounter();

$nativeCounter->set(5);

$nativeCounter->decrement();

$this->assertSame(4, $nativeCounter->get());

$nativeCounter->decrement();

$this->assertSame(3, $nativeCounter->get());

$nativeCounter->decrement();

$this->assertSame(2, $nativeCounter->get());
}
}
Loading

0 comments on commit 58eb617

Please sign in to comment.