Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
Add and tag the commands for Symfony 3.4. Closes #28 (#33)
Browse files Browse the repository at this point in the history
* Add and tag the commands for Symfony 3.4. Closes #28

Also a fix to use the Console/Helper/Table, not via the helper

* Set services in the constructor. Closes #29

Can also remove the use of ContainerAwareCommand, and instead use
Symfony\Component\Console\Command\Command
  • Loading branch information
alister authored and DZunke committed Oct 24, 2017
1 parent c9524a7 commit bc80d22
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 30 deletions.
35 changes: 27 additions & 8 deletions Command/BotMessagingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,40 @@

use DZunke\SlackBundle\Event;
use DZunke\SlackBundle\Events;
use DZunke\SlackBundle\Slack\Channels;
use DZunke\SlackBundle\Slack\Client;
use DZunke\SlackBundle\Slack\Entity\Message;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class BotMessagingCommand extends ContainerAwareCommand
class BotMessagingCommand extends Command
{
protected static $defaultName = 'slack:run-bot';

const PROCESS_ITERATION_SLEEP = 1;

protected static $defaultName = 'slack:run-bot';

/**
* @var Channels
*/
private $channels;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

public function __construct(Channels $channels, EventDispatcherInterface $eventDispatcher)
{
$this->channels = $channels;
$this->eventDispatcher = $eventDispatcher;
parent::__construct();
}

protected function configure()
{
$this
Expand All @@ -27,15 +46,15 @@ protected function configure()
->addArgument(
'channel',
InputArgument::REQUIRED,
'Channel wo Watch over'
'Channel to Watch over'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$logger = new Logger(new StreamHandler('php://output'));

$channel = $this->getContainer()->get('dz.slack.channels')->getId($input->getArgument('channel'));
$channel = $this->channels->getId($input->getArgument('channel'));
if (empty($channel)) {
$logger->error('channel "' . $channel . '" does not exists');
return;
Expand All @@ -45,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
while (true) {

try {
$latestMessages = $this->getContainer()->get('dz.slack.channels')->history($channel, $lastTimestamp);
$latestMessages = $this->channels->history($channel, $lastTimestamp);

foreach ($latestMessages as $message) {
if ($message->isBot() === true) {
Expand All @@ -69,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$logger->debug('Dispatching "' . $event . '"');

$this->getContainer()->get('event_dispatcher')->dispatch(
$this->eventDispatcher->dispatch(
$event,
new Events\MessageEvent($channel, $message)
);
Expand Down
22 changes: 17 additions & 5 deletions Command/ChangeChannelsTopicCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@

namespace DZunke\SlackBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use DZunke\SlackBundle\Slack\Channels;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ChangeChannelsTopicCommand extends ContainerAwareCommand
class ChangeChannelsTopicCommand extends Command
{

protected static $defaultName = 'slack:channels:topic';

/**
* @var Channels
*/
private $channels;

public function __construct(Channels $channels)
{
$this->channels = $channels;
parent::__construct();
}

protected function configure()
{
Expand All @@ -25,14 +38,13 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$channels = $this->getContainer()->get('dz.slack.channels');
if ($input->getOption('discover')) {
$channelId = $channels->getId($input->getArgument('channel'));
$channelId = $this->channels->getId($input->getArgument('channel'));
} else {
$channelId = $input->getArgument('channel');
}

$response = $channels->setTopic(
$response = $this->channels->setTopic(
$channelId,
$input->getArgument('topic')
);
Expand Down
11 changes: 8 additions & 3 deletions Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace DZunke\SlackBundle\Command;

use DZunke\SlackBundle\Slack\Client;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DebugCommand extends ContainerAwareCommand
class DebugCommand extends Command
{
protected static $defaultName = 'slack:debug';

Expand All @@ -28,6 +28,12 @@ class DebugCommand extends ContainerAwareCommand
*/
protected $client;

public function __construct(Client $client)
{
$this->client = $client;
parent::__construct();
}

protected function configure()
{
$this
Expand All @@ -40,7 +46,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->client = $this->getContainer()->get('dz.slack.client');

$config = $this->getContainer()->getParameter('d_zunke_slack.config');

Expand Down
15 changes: 11 additions & 4 deletions Command/MessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

namespace DZunke\SlackBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use DZunke\SlackBundle\Slack\Messaging;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MessageCommand extends ContainerAwareCommand
class MessageCommand extends Command
{
protected static $defaultName = 'slack:message';
private $messenger;

public function __construct(Messaging $messenger)
{
$this->messenger = $messenger;
parent::__construct();
}

protected function configure()
{
Expand All @@ -24,8 +32,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$messenger = $this->getContainer()->get('dz.slack.messaging');
$response = $messenger->message(
$response = $this->messenger->message(
$input->getArgument('channel'),
$input->getArgument('message'),
$input->getArgument('username')
Expand Down
30 changes: 20 additions & 10 deletions Command/UsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

namespace DZunke\SlackBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use DZunke\SlackBundle\Slack\Users;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class UsersCommand extends ContainerAwareCommand
class UsersCommand extends Command
{
protected static $defaultName = 'slack:users';

/**
* @var Users
*/
private $api;

public function __construct(Users $users)
{
$this->api = $users;
parent::__construct();
}

protected function configure()
{
Expand All @@ -26,20 +39,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$formatter = $this->getHelper('formatter');

try {
$api = $this->getContainer()->get('dz.slack.users');

if ($input->getOption('only-active')) {
$response = $api->getActiveUsers();
$response = $this->api->getActiveUsers();
} elseif ($input->getOption('only-deleted')) {
$response = $api->getDeletedUsers();
$response = $this->api->getDeletedUsers();
} elseif ($input->getOption('user')) {
$response = $api->getUser($input->getOption('user'));
$response = $this->api->getUser($input->getOption('user'));

if (is_array($response)) {
$response = [$response['name'] => $response];
}
} else {
$response = $api->getUsers();
$response = $this->api->getUsers();
}

if (empty($response)) {
Expand All @@ -58,11 +69,10 @@ function (&$row) {
}
);

$table = $this->getHelper('table');
$table = new Table($output);
$table->setHeaders(array_keys(reset($response)))->setRows($response);
$table->render($output);


} catch (\Exception $e) {
$output->writeln(
$formatter->formatBlock(
Expand Down
20 changes: 20 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ services:
dz.slack.users:
class: DZunke\SlackBundle\Slack\Users
arguments: ["@dz.slack.client"]

DZunke\SlackBundle\Command\BotMessagingCommand:
arguments: ["@dz.slack.channels", "@event_dispatcher"]
tags: ['console.command']

DZunke\SlackBundle\Command\ChangeChannelsTopicCommand:
arguments: ["@dz.slack.channels"]
tags: ['console.command']

DZunke\SlackBundle\Command\DebugCommand:
arguments: ["@dz.slack.client"]
tags: ['console.command']

DZunke\SlackBundle\Command\MessageCommand:
arguments: ["@dz.slack.messaging"]
tags: ['console.command']

DZunke\SlackBundle\Command\UsersCommand:
arguments: ["@dz.slack.users"]
tags: ['console.command']

0 comments on commit bc80d22

Please sign in to comment.