Skip to content

Commit

Permalink
First draft Conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
TiiFuchs committed May 14, 2022
1 parent 88cf722 commit b80e6fe
Show file tree
Hide file tree
Showing 27 changed files with 351 additions and 102 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require": {
"php": "^8.1",
"guzzlehttp/guzzle": "^7.0",
"cache/simple-cache-bridge": "^1.2"
"cache/simple-cache-bridge": "^1.2",
"league/container": "^4.2"
},
"bin": [
"./telepathy"
Expand Down
190 changes: 136 additions & 54 deletions composer.lock

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

30 changes: 30 additions & 0 deletions src/Cache/UsesCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tii\Telepath\Cache;

use Cache\Bridge\SimpleCache\SimpleCacheBridge;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

trait UsesCache
{

protected ?CacheInterface $cache = null;

public function enableCaching(CacheInterface|CacheItemPoolInterface $cache): static
{
if ($cache instanceof CacheItemPoolInterface) {
$cache = new SimpleCacheBridge($cache);
}

$this->cache = $cache;

return $this;
}

public function cache(): ?CacheInterface
{
return $this->cache;
}

}
56 changes: 56 additions & 0 deletions src/Conversations/Conversation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tii\Telepath\Conversations;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;

abstract class Conversation
{

protected ?string $next = null;

public function __construct(
protected TelegramBot $bot
) {}

public static function conversationKey(Update $update)
{
return "telepath.conversation.{$update->user()->id}.{$update->chat()->id}";
}

public function next(string $method)
{
$cache = $this->bot->cache();
$conversationKey = static::conversationKey($this->bot->latestUpdate());

$this->next = $method;
$cache->set($conversationKey, $this);
}

public function end()
{
$cache = $this->bot->cache();
$conversationKey = static::conversationKey($this->bot->latestUpdate());

$cache->delete($conversationKey);
}

public function __serialize(): array
{
$data = get_object_vars($this);
unset($data['bot']);

return $data;
}

public function __invoke(Update $update)
{
if ($this->next === null) {
return null;
}

return $this->{$this->next}($update);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Tii\Telepath\Handler\CallbackQuery;
namespace Tii\Telepath\Handlers\CallbackQuery;

use Tii\Telepath\Handler\CallbackQuery;
use Tii\Telepath\Handlers\CallbackQuery;
use Tii\Telepath\MatchMaker\MatchMaker;
use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/ChatMember.php → src/Handlers/ChatMember.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Handler.php → src/Handlers/Handler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tii\Telepath\Handler;
namespace Tii\Telepath\Handlers;

use Tii\Telepath\Telegram\Update;
use Tii\Telepath\TelegramBot;
Expand Down
Loading

0 comments on commit b80e6fe

Please sign in to comment.