Skip to content

Commit

Permalink
extended api class
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoulgrave committed Nov 18, 2016
1 parent 32ef49f commit 4a49bd7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Provider/TelegramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Telegram\Bot\Api;
use Telegram\Bot\Silex\Service\TelegramService;

class TelegramServiceProvider implements ServiceProviderInterface
{
Expand All @@ -16,13 +16,13 @@ public function register(Container $app)
if (!isset($app['telegram.bot_api'])) {
throw new \Exception('telegram.bot_api parameter must be set.');
}
$botApi = new Api($app['telegram.bot_api']);
$botApi = new TelegramService($app, $app['telegram.bot_api']);
if (
isset($app['telegram.commands'])
&& is_array($app['telegram.commands'])
) {
foreach ($app['telegram.commands'] as $command) {
$botApi->addCommand(new $command($app));
$botApi->addCommand($command);
}
}
return $botApi;
Expand Down
28 changes: 28 additions & 0 deletions src/Service/TelegramService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Telegram\Bot\Silex\Service;

use Pimple\Container;
use Telegram\Bot\Api;

class TelegramService extends Api
{
/**
* @var Container
*/
private $app;

public function __construct(Container $app, $token = null, $async = false, $http_client_handler = null)
{
parent::__construct($token, $async, $http_client_handler);
$this->app = $app;
}

/**
* @param string $command
* @return \Telegram\Bot\Commands\CommandBus
*/
public function addCommand($command)
{
return parent::addCommand(new $command($this->app));
}
}
4 changes: 2 additions & 2 deletions tests/Command/ApplicationAwareCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use PHPUnit\Framework\TestCase;
use Silex\Application;
use Telegram\Bot\Api;
use Telegram\Bot\Silex\Provider\TelegramServiceProvider;
use Telegram\Bot\Silex\Service\TelegramService;

class ApplicationAwareCommandTest extends TestCase
{
Expand All @@ -20,7 +20,7 @@ public function testAppInjection()
TestCommand::class
]
]);
/* @var $api Api */
/* @var $api TelegramService */
$api = $app['telegram'];
foreach ($api->getCommands() as $command) {
$this->assertInstanceOf(Application::class, $command->getApplication());
Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use PHPUnit\Framework\TestCase;
use Silex\Application;
use Telegram\Bot\Api;
use Telegram\Bot\Silex\Provider\TelegramServiceProvider;
use Telegram\Bot\Silex\Service\TelegramService;

class ServiceProviderTest extends TestCase
{
Expand All @@ -28,6 +28,6 @@ public function testCommandCount()
$app->register(new TelegramServiceProvider(), [
'telegram.bot_api' => 'testapicode'
]);
$this->assertInstanceOf(Api::class, $app['telegram']);
$this->assertInstanceOf(TelegramService::class, $app['telegram']);
}
}

0 comments on commit 4a49bd7

Please sign in to comment.