Skip to content

Commit 47df282

Browse files
committed
Assign Host to Event
This API operation is not supported by the really old and now abandoned library we use. I added it to a new client without touching any of the old code for the sake of speed and.. well everything needs to be re-written cause its running on all kinda of things that do not exist anymore...
1 parent e5a5159 commit 47df282

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

config/container.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
use AmsterdamPHP\Console\Api\JoindInClient;
24
use Crummy\Phlack\Phlack;
35
use DI\ContainerBuilder;
46
use DMS\Service\Meetup\AbstractMeetupClient;
@@ -13,6 +15,7 @@
1315

1416
// Define services
1517
$container->set(Client::class, new Client($config['joindin']));
18+
$container->set(JoindInClient::class, new JoindInClient($config['joindin']));
1619
$container->set(AbstractMeetupClient::class, \DMS\Service\Meetup\MeetupKeyAuthClient::factory([
1720
'key' => $config['meetup_api_key']
1821
]));

src/Api/JoindInClient.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace AmsterdamPHP\Console\Api;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Collection;
7+
use function json_encode;
8+
use function var_dump;
9+
10+
final class JoindInClient extends Client
11+
{
12+
const DEFAULT_BASE_URL = 'https://api.joind.in/v2.1';
13+
14+
/**
15+
* Constructor
16+
*/
17+
public function __construct(array $config) {
18+
$defaults = array('base_url' => self::DEFAULT_BASE_URL);
19+
$required = array('base_url');
20+
21+
$configuration = Collection::fromConfig($config, $defaults, $required);
22+
23+
parent::__construct($configuration->toArray());
24+
25+
if ($configuration->get('access_token')) {
26+
$this->setDefaultOption('headers/Authorization', 'OAuth ' . $configuration->get('access_token'));
27+
}
28+
$this->setDefaultOption('headers/Accept-Charset', 'utf-8');
29+
$this->setDefaultOption('headers/Accept', 'application/json');
30+
$this->setDefaultOption('headers/Content-Type', 'application/json');
31+
}
32+
33+
public function addEventHost($eventId, $eventHost)
34+
{
35+
$result = $this->post('v2.1/events/'.$eventId.'/hosts', ['body' => json_encode(['host_name' => $eventHost])]);
36+
return $result;
37+
}
38+
}

src/Command/CreateJoindInCommand.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AmsterdamPHP\Console\Command;
44

5+
use AmsterdamPHP\Console\Api\JoindInClient;
56
use Codeliner\ArrayReader\ArrayReader;
67
use Crummy\Phlack\Phlack;
78
use DMS\Service\Meetup\AbstractMeetupClient;
@@ -10,6 +11,7 @@
1011
use Symfony\Component\Console\Command\Command;
1112
use Symfony\Component\Console\Input\InputInterface;
1213
use Symfony\Component\Console\Output\OutputInterface;
14+
use function var_dump;
1315

1416
class CreateJoindInCommand extends Command
1517
{
@@ -29,18 +31,25 @@ class CreateJoindInCommand extends Command
2931
*/
3032
protected $joindinEvents;
3133

34+
/**
35+
* @var JoindInClient
36+
*/
37+
private $joindinApi;
38+
3239
/**
3340
* CreateJoindInCommand constructor.
3441
*
3542
* @param AbstractMeetupClient $meetup
3643
* @param Phlack $slack
3744
* @param Client $joindin
45+
* @param JoindInClient $joindinApi
3846
*/
39-
public function __construct(AbstractMeetupClient $meetup, Phlack $slack, Client $joindin)
47+
public function __construct(AbstractMeetupClient $meetup, Phlack $slack, Client $joindin, JoindInClient $joindinApi)
4048
{
4149
$this->meetup = $meetup;
4250
$this->slack = $slack;
4351
$this->joindinEvents = $joindin->getService(new Events());
52+
$this->joindinApi = $joindinApi;
4453
parent::__construct();
4554
}
4655

@@ -97,8 +106,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
97106
];
98107

99108
$result = $this->joindinEvents->submit($event);
109+
$output->writeln(sprintf("<comment>=> Joind.in event created.</comment>"));
100110

101-
$output->writeln(sprintf("<comment>=> Joind.in event created, awaiting approval</comment>"));
111+
$hostResult = $this->joindinApi->addEventHost($this->extractIdFromLocation($result['url']), 'amsterdamphp');
112+
$output->writeln("<comment>=> Host Add request returned " . $hostResult->getStatusCode() . "</comment>");
102113

103114
$this->sendSlackMsg(
104115
sprintf(
@@ -151,4 +162,16 @@ protected function getCurrentMonthlyMeetingCandidates()
151162
return strpos($event['name'], 'Monthly Meeting') !== false;
152163
});
153164
}
165+
166+
protected function extractIdFromLocation($locationUrl)
167+
{
168+
$matches = [];
169+
preg_match(
170+
"/events\/([0-9]*)/",
171+
$locationUrl,
172+
$matches
173+
);
174+
175+
return $matches[1];
176+
}
154177
}

test/Command/CreateJoindInCommandTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<?php
22
namespace AmsterdamPHP\Console;
33

4+
use AmsterdamPHP\Console\Api\JoindInClient;
45
use AmsterdamPHP\Console\Command\CreateJoindInCommand;
56
use Crummy\Phlack\Builder\MessageBuilder;
67
use Crummy\Phlack\Message\Message;
78
use Crummy\Phlack\Phlack;
89
use DMS\Service\Meetup\AbstractMeetupClient;
910
use DMS\Service\Meetup\Response\MultiResultResponse;
11+
use Guzzle\Http\Message\Header;
12+
use GuzzleHttp\Message\Response;
13+
use GuzzleHttp\Adapter\MockAdapter;
14+
use GuzzleHttp\Event\Emitter;
1015
use Joindin\Api\Client;
1116
use Mockery\Mock;
1217
use Mockery\MockInterface;
@@ -36,20 +41,36 @@ class CreateJoindInCommandTest extends \PHPUnit_Framework_TestCase
3641
*/
3742
protected $command;
3843

44+
/**
45+
* @var MockAdapter
46+
*/
47+
protected $adapter;
48+
49+
/**
50+
* @var JoindInClient
51+
*/
52+
protected $joindinClient;
53+
3954
protected function setUp()
4055
{
4156
parent::setUp();
4257
$this->meetup = \Mockery::mock(AbstractMeetupClient::class);
4358
$this->slack = \Mockery::mock(Phlack::class);
4459
$this->joindinEvents = \Mockery::mock(Client::class);
4560
$this->joindinEvents->shouldReceive('getService')->andReturnSelf();
61+
$this->adapter = new MockAdapter();
62+
$this->joindinClient = new JoindInClient([
63+
'adapter' => $this->adapter,
64+
'emitter' => new Emitter()
65+
]);
4666

47-
$this->command = new CreateJoindInCommand($this->meetup, $this->slack, $this->joindinEvents);
67+
$this->command = new CreateJoindInCommand($this->meetup, $this->slack, $this->joindinEvents, $this->joindinClient);
4868
}
4969

5070
public function testCommand()
5171
{
5272
$meetupResponse = \Mockery::mock(MultiResultResponse::class)->shouldDeferMissing();
73+
$meetupResponse->addHeader('Content-Type', new Header('Content-Type', 'application/json'));
5374
$meetupResponse->setData([
5475
[
5576
'name' => 'Monthly Meeting',
@@ -61,7 +82,9 @@ public function testCommand()
6182

6283
$this->meetup->shouldReceive('getEvents')->andReturn($meetupResponse)->once();
6384

64-
$this->joindinEvents->shouldReceive('submit')->once();
85+
$this->joindinEvents->shouldReceive('submit')
86+
->andReturn(['url' => 'http://some.path.to.api/v2.1/events/34'])
87+
->once();
6588

6689
$this->slack->shouldReceive('getMessageBuilder')->andReturn(new MessageBuilder())->once();
6790
$this->slack->shouldReceive('send')->with(
@@ -73,6 +96,8 @@ function (Message $param) {
7396
)
7497
);
7598

99+
$this->adapter->setResponse(new Response(200));
100+
76101
$input = new ArrayInput([]);
77102
$output = new DummyOutput();
78103

0 commit comments

Comments
 (0)