Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add , Parser for Slack input, Slack Client
  • Loading branch information
stringerbell committed May 29, 2016
1 parent 2ebf40b commit 12beab3
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 5 deletions.
13 changes: 9 additions & 4 deletions config/autoload/dependencies.global.php
@@ -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;
Expand All @@ -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,
],
],
];
14 changes: 14 additions & 0 deletions src/App/Client/SlackClient.php
@@ -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 changes: 2 additions & 0 deletions src/App/Pipeline/SlackJiraPipeline.php
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
66 changes: 66 additions & 0 deletions src/App/Validator/Slack/ParseSlackJiraInput.php
@@ -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);
}
}
14 changes: 14 additions & 0 deletions src/App/Validator/Slack/ParseSlackJiraInputFactory.php
@@ -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));
}
}
2 changes: 1 addition & 1 deletion src/App/Validator/Slack/ValidateSlackToken.php
Expand Up @@ -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.