Skip to content

Commit

Permalink
Rename TelegramRequestDecorator to TelegramRequestEnriched and requir…
Browse files Browse the repository at this point in the history
…e TelegramRequestInterface as parameters in ResponseInterface methods
  • Loading branch information
viktorprogger committed Jul 31, 2024
1 parent e26b370 commit 30b8cf4
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 71 deletions.
6 changes: 3 additions & 3 deletions docs/key-concepts/06-request-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Here is an instruction for:
1. Inside an action - add a tag to the message into `Response`:
```php
return new Response($update)
->withRequest(new TelegramRequestDecorator($message, 'foo'));
->withRequest($message, 'foo');
```
In this example we add a `foo` tag to the given message.
2. Create a tag handler. It should follow the [Extended Callable Definitions syntax](./04-extended-callable-definitions.md).
As argument list it should take
1. An `Update` object
2. A `TelegramRequestDecorator` object
2. A `TelegramRequestEnriched` object
3. An array containing Telegram API response
4. It also may take as additional arguments any dependency which can be resolved by a DI Container.

Expand All @@ -34,7 +34,7 @@ Here is an instruction for:
{
public function __construct(private FooRepository $repository) {}

public function handleSuccess(Update $update, TelegramRequestDecorator $request, array $response): void {
public function handleSuccess(Update $update, TelegramRequestEnriched $request, array $response): void {
$this->repository->saveMessageId($response['message_id']);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Event/RequestErrorEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Botasis\Runtime\Event;

use Botasis\Client\Telegram\Client\Exception\TelegramRequestException;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Update\Update;
use Psr\EventDispatcher\StoppableEventInterface;

Expand All @@ -16,7 +16,7 @@ final class RequestErrorEvent implements StoppableEventInterface
* @param bool $isPropagationStopped Set this to true to stop propagation of the event.
*/
public function __construct(
public readonly TelegramRequestDecorator $request,
public readonly TelegramRequestEnriched $request,
public readonly TelegramRequestException $exception,
public readonly Update $update,
public bool $suppressException = false,
Expand Down
4 changes: 2 additions & 2 deletions src/Event/RequestSuccessEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Botasis\Runtime\Event;

use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Update\Update;
use Psr\EventDispatcher\StoppableEventInterface;

final class RequestSuccessEvent implements StoppableEventInterface
{
public function __construct(
public readonly TelegramRequestDecorator $request,
public readonly TelegramRequestEnriched $request,
public readonly ?array $responseDecoded,
public readonly Update $update,
public bool $isPropagationStopped = false,
Expand Down
8 changes: 2 additions & 6 deletions src/Handler/RouteNotFoundUpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Botasis\Client\Telegram\Request\Message\Message;
use Botasis\Client\Telegram\Request\Message\MessageFormat;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\Response;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Update\Update;
Expand All @@ -22,11 +22,7 @@ public function handle(Update $update): ResponseInterface
{
if ($update->chat !== null) {
return (new Response($update))
->withRequest(
new TelegramRequestDecorator(
new Message($this->message, MessageFormat::TEXT, $update->chat->id)
)
);
->withRequest(new Message($this->message, MessageFormat::TEXT, $update->chat->id));
}

return new Response($update);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Botasis\Client\Telegram\Request\CallbackResponse;
use Botasis\Runtime\Middleware\MiddlewareInterface;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Update\Update;
use Botasis\Runtime\UpdateHandlerInterface;
Expand All @@ -17,7 +17,7 @@ public function process(Update $update, UpdateHandlerInterface $handler): Respon
{
$response = $handler->handle($update);
if ($update->callbackQueryId !== null && $response->hasCallbackResponse() === false) {
$response = $response->withRequest(new TelegramRequestDecorator(new CallbackResponse($update->callbackQueryId)));
$response = $response->withRequest(new CallbackResponse($update->callbackQueryId));
}

return $response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Botasis\Client\Telegram\Request\TelegramRequestInterface;

final readonly class TelegramRequestDecorator
final readonly class TelegramRequestEnriched
{
public array $responseTags;

Expand Down
25 changes: 13 additions & 12 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use Botasis\Client\Telegram\Request\CallbackResponse;
use Botasis\Client\Telegram\Request\TelegramRequestInterface;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Update\Update;

final class Response implements ResponseInterface
{
/** @var TelegramRequestDecorator[] */
/** @var TelegramRequestEnriched[] */
private array $requests = [];

private bool $hasCallbackResponse = false;
Expand All @@ -28,21 +28,22 @@ public function withUpdate(Update $update): ResponseInterface
return $instance;
}

public function withRequest(TelegramRequestDecorator $request): ResponseInterface
public function withRequest(TelegramRequestInterface $request, string ...$tags): ResponseInterface
{
$instance = clone $this;
$enriched = new TelegramRequestEnriched($request, ...$tags);

if ($request->request->getMethod() === CallbackResponse::METHOD) {
if ($request->getMethod() === CallbackResponse::METHOD) {
$instance->hasCallbackResponse = true;
array_unshift($instance->requests, $request);
array_unshift($instance->requests, $enriched);
} else {
$instance->requests[] = $request;
$instance->requests[] = $enriched;
}

return $instance;
}

public function withRequestReplaced(TelegramRequestInterface $search, ?TelegramRequestDecorator $replace): ResponseInterface
public function withRequestReplaced(TelegramRequestInterface $search, ?TelegramRequestInterface $replace, string ...$replaceTags): ResponseInterface
{
$instance = clone $this;

Expand All @@ -51,14 +52,14 @@ public function withRequestReplaced(TelegramRequestInterface $search, ?TelegramR
if ($request->request === $search) {
if ($replace === null) {
unset($requests[$index]);
} elseif ($request->request->getMethod() !== CallbackResponse::METHOD && $replace->request->getMethod() === CallbackResponse::METHOD) {
} elseif ($request->request->getMethod() !== CallbackResponse::METHOD && $replace->getMethod() === CallbackResponse::METHOD) {
unset($requests[$index]);
array_unshift($requests, $replace);
} elseif($replace->request->getMethod() !== CallbackResponse::METHOD && $request->request->getMethod() === CallbackResponse::METHOD) {
array_unshift($requests, new TelegramRequestEnriched($replace, ...$replaceTags));
} elseif($replace->getMethod() !== CallbackResponse::METHOD && $request->request->getMethod() === CallbackResponse::METHOD) {
unset($requests[$index]);
$requests[] = $replace;
$requests[] = new TelegramRequestEnriched($replace, ...$replaceTags);
} else {
$requests[$index] = $replace;
$requests[$index] = new TelegramRequestEnriched($replace, ...$replaceTags);
}

break;
Expand Down
19 changes: 15 additions & 4 deletions src/Response/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@
namespace Botasis\Runtime\Response;

use Botasis\Client\Telegram\Request\TelegramRequestInterface;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Update\Update;

interface ResponseInterface
{
public function withUpdate(Update $update): ResponseInterface;

public function withRequest(TelegramRequestDecorator $request): ResponseInterface;
public function withRequest(TelegramRequestInterface $request, string ...$tags): ResponseInterface;

public function withRequestReplaced(TelegramRequestInterface $search, ?TelegramRequestDecorator $replace): ResponseInterface;
/**
* @param TelegramRequestInterface $search Request to be replaced
* @param TelegramRequestInterface|null $replace Request to replace with
* @param string ...$replaceTags Old tags will be replaced with this tag set if $replace !== null
*
* @return ResponseInterface
*/
public function withRequestReplaced(
TelegramRequestInterface $search,
?TelegramRequestInterface $replace,
string ...$replaceTags
): ResponseInterface;

public function getUpdate(): Update;

/**
* @return TelegramRequestDecorator[]
* @return TelegramRequestEnriched[]
*/
public function getRequests(): array;

Expand Down
12 changes: 5 additions & 7 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Botasis\Runtime\Middleware\MiddlewareDispatcher;
use Botasis\Runtime\Middleware\MiddlewareFactory;
use Botasis\Runtime\Middleware\MiddlewareInterface;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\Response;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Router\CallableResolver;
Expand Down Expand Up @@ -56,11 +56,9 @@ public function handle(Update $update): ResponseInterface
);
return (new Response($update))
->withRequest(
new TelegramRequestDecorator(
$message->onSuccess(function () use ($handler, $message) {
$handler->successCheck = $message->text . '5';
}),
),
$message->onSuccess(function () use ($handler, $message) {
$handler->successCheck = $message->text . '5';
}),
);
}
};
Expand Down Expand Up @@ -156,7 +154,7 @@ public function process(Update $update, UpdateHandlerInterface $handler): Respon

$message = $response->getRequests()[0];

return $response->withRequestReplaced($message->request, new TelegramRequestDecorator($message->request->withText($message->request->text . $this->addition)));
return $response->withRequestReplaced($message->request, $message->request->withText($message->request->text . $this->addition));
}
};
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Middleware/MiddlewareDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Botasis\Runtime\Middleware\Event\BeforeMiddleware;
use Botasis\Runtime\Middleware\MiddlewareDispatcher;
use Botasis\Runtime\Middleware\MiddlewareFactory;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\Response;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Tests\Middleware\Support\FailMiddleware;
Expand All @@ -35,7 +35,7 @@ public function testCallableMiddlewareCalled(): void

$dispatcher = $this->createDispatcher()->withMiddlewares(
static function () use ($update): ResponseInterface {
return (new Response($update))->withRequest(new TelegramRequestDecorator(new CallbackResponse('middleware-id')));
return (new Response($update))->withRequest(new CallbackResponse('middleware-id'));
},
);

Expand Down Expand Up @@ -69,7 +69,7 @@ public function testMiddlewareFullStackCalled(): void
$middleware2 = static function (Update $update): ResponseInterface {
$callbackResponse = new CallbackResponse($update->getAttribute('middleware'));

return (new Response($update))->withRequest(new TelegramRequestDecorator($callbackResponse));
return (new Response($update))->withRequest($callbackResponse);
};

$dispatcher = $this->createDispatcher()->withMiddlewares($middleware1, $middleware2);
Expand All @@ -85,12 +85,12 @@ public function testMiddlewareStackInterrupted(): void
$middleware1 = static function () use ($update): ResponseInterface {
$callbackResponse = new CallbackResponse('first');

return (new Response($update))->withRequest(new TelegramRequestDecorator($callbackResponse));
return (new Response($update))->withRequest($callbackResponse);
};
$middleware2 = static function () use ($update): ResponseInterface {
$callbackResponse = new CallbackResponse('second');

return (new Response($update))->withRequest(new TelegramRequestDecorator($callbackResponse));
return (new Response($update))->withRequest($callbackResponse);
};

$dispatcher = $this->createDispatcher()->withMiddlewares($middleware1, $middleware2);
Expand Down Expand Up @@ -199,7 +199,7 @@ private function getRequestHandler(): UpdateHandlerInterface
return new class () implements UpdateHandlerInterface {
public function handle(Update $update): ResponseInterface
{
return (new Response($update))->withRequest(new TelegramRequestDecorator(new CallbackResponse('default-id')));
return (new Response($update))->withRequest(new CallbackResponse('default-id'));
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Middleware/MiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Botasis\Runtime\Middleware\MiddlewareFactory;
use Botasis\Runtime\Middleware\MiddlewareFactoryInterface;
use Botasis\Runtime\Middleware\MiddlewareInterface;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\Response;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Tests\Middleware\Support\InvalidController;
Expand Down Expand Up @@ -57,7 +57,7 @@ public function testCreateFromClosureResponse(): void
$update = $this->createTelegramUpdate();
$middleware = $this->getMiddlewareFactory($container)->create(
static function () use ($update): ResponseInterface {
return (new Response($update))->withRequest(new TelegramRequestDecorator(new CallbackResponse('418')));
return (new Response($update))->withRequest(new CallbackResponse('418'));
}
);
self::assertSame(
Expand Down Expand Up @@ -197,7 +197,7 @@ private function getRequestHandler(): UpdateHandlerInterface
return new class () implements UpdateHandlerInterface {
public function handle(Update $update): ResponseInterface
{
return (new Response($update))->withRequest(new TelegramRequestDecorator(new CallbackResponse('default-id')));
return (new Response($update))->withRequest(new CallbackResponse('default-id'));
}
};
}
Expand Down
8 changes: 2 additions & 6 deletions tests/Middleware/Support/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Botasis\Client\Telegram\Request\Message\Message;
use Botasis\Client\Telegram\Request\Message\MessageFormat;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\Response;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Update\Update;
Expand All @@ -16,10 +16,6 @@ final class TestController
public function index(Update $update): ResponseInterface
{
return (new Response($update))
->withRequest(
new TelegramRequestDecorator(
new Message('test message', MessageFormat::TEXT, 'chatId')
)
);
->withRequest(new Message('test message', MessageFormat::TEXT, 'chatId'));
}
}
4 changes: 2 additions & 2 deletions tests/Middleware/Support/TestMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Botasis\Client\Telegram\Request\CallbackResponse;
use Botasis\Runtime\Middleware\MiddlewareInterface;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\Response;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Update\Update;
Expand All @@ -16,6 +16,6 @@ final class TestMiddleware implements MiddlewareInterface
{
public function process(Update $update, UpdateHandlerInterface $handler): ResponseInterface
{
return (new Response($update))->withRequest(new TelegramRequestDecorator(new CallbackResponse('42')));
return (new Response($update))->withRequest(new CallbackResponse('42'));
}
}
8 changes: 2 additions & 6 deletions tests/Middleware/Support/UseParamsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Botasis\Client\Telegram\Request\Message\Message;
use Botasis\Client\Telegram\Request\Message\MessageFormat;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Update\Update;
use Botasis\Runtime\UpdateHandlerInterface;
Expand All @@ -17,10 +17,6 @@ public function index(Update $request, UpdateHandlerInterface $handler): Respons
{
return $handler
->handle($request)
->withRequest(
new TelegramRequestDecorator(
new Message('message text', MessageFormat::TEXT, $request->chat->id)
)
);
->withRequest(new Message('message text', MessageFormat::TEXT, $request->chat->id));
}
}
4 changes: 2 additions & 2 deletions tests/Middleware/Support/UseParamsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Botasis\Client\Telegram\Request\CallbackResponse;
use Botasis\Runtime\Middleware\MiddlewareInterface;
use Botasis\Runtime\Request\TelegramRequestDecorator;
use Botasis\Runtime\Request\TelegramRequestEnriched;
use Botasis\Runtime\Response\Response;
use Botasis\Runtime\Response\ResponseInterface;
use Botasis\Runtime\Update\Update;
Expand All @@ -16,6 +16,6 @@ final class UseParamsMiddleware implements MiddlewareInterface
{
public function process(Update $update, UpdateHandlerInterface $handler): ResponseInterface
{
return (new Response($update))->withRequest(new TelegramRequestDecorator(new CallbackResponse('fake-id')));
return (new Response($update))->withRequest(new CallbackResponse('fake-id'));
}
}
Loading

0 comments on commit 30b8cf4

Please sign in to comment.