Skip to content

Commit 38f2fd5

Browse files
authored
Merge pull request #4 from AmsterdamPHP/task/assign-host
Assign Host to Event
2 parents e5a5159 + 4b3fd7b commit 38f2fd5

File tree

7 files changed

+159
-30
lines changed

7 files changed

+159
-30
lines changed

.circleci/config.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
version: 2
2+
3+
jobs:
4+
unit-tests:
5+
docker:
6+
- image: circleci/php:7.0
7+
steps:
8+
- run: sudo composer self-update && sudo composer self-update --1
9+
- checkout
10+
- restore_cache:
11+
keys:
12+
- dependencies-{{ checksum "composer.lock" }}
13+
- composer-cache
14+
15+
- run: composer install -n -o --no-scripts
16+
- save_cache:
17+
key: dependencies-{{ checksum "composer.lock" }}
18+
paths:
19+
- vendor
20+
- save_cache:
21+
key: composer-cache
22+
paths:
23+
- ~/.composer/cache
24+
25+
- run: mkdir phpunit
26+
- run: ./vendor/bin/phpunit --log-junit phpunit/junit.xml
27+
- store_test_results:
28+
path: phpunit
29+
- store_artifacts:
30+
path: phpunit
31+
32+
deploy:
33+
machine:
34+
enabled: true
35+
docker:
36+
- image: circleci/php:7.0
37+
steps:
38+
- add_ssh_keys:
39+
fingerprints:
40+
- "00:49:b0:d3:41:35:99:f2:a7:d1:02:46:02:07:06:09"
41+
- run: sudo composer self-update && sudo composer self-update --1
42+
- checkout
43+
- restore_cache:
44+
keys:
45+
- dependencies-{{ checksum "composer.lock" }}
46+
- composer-cache
47+
48+
- run: composer install -n -o --no-scripts
49+
- run: ./vendor/bin/dep deploy
50+
51+
workflows:
52+
version: 2
53+
build-and-deploy:
54+
jobs:
55+
- unit-tests
56+
- deploy:
57+
requires:
58+
- unit-tests
59+
filters:
60+
branches:
61+
only: master
62+

circle.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

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
]));

deploy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
require 'recipe/composer.php';
55

66
// Set configurations
7-
set('repository', 'git@github.com:AmsterdamPHP/console.git');
7+
set('repository', 'https://github.com/AmsterdamPHP/console.git');
88
set('shared_files', ['.env']);
99
set('shared_dirs', []);
1010
set('writable_dirs', []);
1111

1212
// Configure servers
13-
server('production', 'amsterdamphp.nl')
14-
->user('phpamst01')
15-
->identityFile()
13+
server('production', '87.233.177.218')
14+
->user('webdev')
15+
->identityFile(null, '~/.ssh/id_rsa_0049b0d3413599f2a7d1024602070609')
1616
->env('deploy_path', '/data/www/console');
1717

1818
after('deploy:update_code', 'deploy:shared');

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)