Permalink
Browse files

Add , Parser for Slack input, Slack Client

  • Loading branch information...
stringerbell committed May 29, 2016
1 parent 2ebf40b commit 12beab35c3f5cbe67d28c77be4d77bcc82683071
@@ -1,5 +1,8 @@
<?php
use App\Client\SlackClient;
use App\Pipeline\SlackJiraPipeline;
use App\Validator\Slack\ParseSlackJiraInput;
use App\Validator\Slack\ParseSlackJiraInputFactory;
use App\Validator\Slack\ValidateSlackToken;
use App\Validator\Slack\ValidateSlackTokenFactory;
use App\Validator\ValidateBody;
@@ -22,10 +25,12 @@
],
// Use 'factories' for services provided by callbacks/factory classes.
'factories' => [
Application::class => ApplicationFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
SlackJiraPipeline::class => SlackJiraPipeline::class,
ValidateSlackToken::class => ValidateSlackTokenFactory::class,
Application::class => ApplicationFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
SlackJiraPipeline::class => SlackJiraPipeline::class,
ValidateSlackToken::class => ValidateSlackTokenFactory::class,
ParseSlackJiraInput::class => ParseSlackJiraInputFactory::class,
SlackClient::class => SlackClient::class,
],
],
];
@@ -0,0 +1,14 @@
<?php
namespace App\Client;
use GuzzleHttp\Client;
use Interop\Container\ContainerInterface;
class SlackClient
{
public function __invoke(ContainerInterface $container)
{
return new Client(['headers' => ['Content-Type' => 'application/json']]);
}
}
@@ -2,6 +2,7 @@
namespace App\Pipeline;
use App\Validator\Slack\ParseSlackJiraInput;
use App\Validator\Slack\ValidateSlackToken;
use App\Validator\ValidateBody;
use Interop\Container\ContainerInterface;
@@ -14,6 +15,7 @@ public function __invoke(ContainerInterface $container)
$pipeline = new MiddlewarePipe();
$pipeline->pipe($container->get(ValidateBody::class));
$pipeline->pipe($container->get(ValidateSlackToken::class));
$pipeline->pipe($container->get(ParseSlackJiraInput::class));
return $pipeline;
}
@@ -0,0 +1,66 @@
<?php
namespace App\Validator\Slack;
use App\Utility\ArgvParser;
use GuzzleHttp\Client;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Console\Exception\RuntimeException;
use Zend\Console\Getopt;
use Zend\Stratigility\Http\ResponseInterface;
class ParseSlackJiraInput
{
/**
* @var Client
*/
private $slackClient;
public function __construct(Client $slackClient)
{
$this->slackClient = $slackClient;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$body = $request->getParsedBody();
$text = $body['text'] ?? '';
$args = ArgvParser::parseString($text);
$_SERVER['argv'][0] = "/jira show issuesKey(s)[]"; // will show up as usage message
// you have to set register_argc_argv => On for this to work with Getopt, or you can dump things into $_SERVER['argv']
$opts = new Getopt(
[
'p|public' => 'publicly post option',
],
$args
);
try {
if (!$opts->getOptions() && count($opts->getArguments()) == 1) {
throw new RuntimeException("Invalid arguments", $opts->getUsageMessage());
}
if (!$opts->getRemainingArgs()) {
throw new RuntimeException("Invalid arguments", $opts->getUsageMessage());
}
$body['args'] = $opts->getRemainingArgs();
// set options in body for later
$body['response_type'] = $opts->public ? 'in_channel' : 'ephemeral';
} catch (RuntimeException $e) {
$responseBody = [
'text' => "Tried running: `{$body['command']} {$body['text']}` \n" . $e->getUsageMessage(),
];
$this->slackClient->post(
$body['response_url'] ?? '',
[
'body' => json_encode($responseBody),
'headers' => [
'Content-Type' => 'application/json',
],
]
);
$error = $e->getUsageMessage();
}
return $next($request->withParsedBody($body), $response, $error ?? null);
}
}
@@ -0,0 +1,14 @@
<?php
namespace App\Validator\Slack;
use App\Client\SlackClient;
use Interop\Container\ContainerInterface;
class ParseSlackJiraInputFactory
{
public function __invoke(ContainerInterface $container)
{
return new ParseSlackJiraInput($container->get(SlackClient::class));
}
}
@@ -28,4 +28,4 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $next($request, $response, $error ?? null);
}
}
}

0 comments on commit 12beab3

Please sign in to comment.