Skip to content

Commit 3974652

Browse files
committed
Upgrade to 8.1
Rewrote all deps that no longer exist and used same pattern for all Api clients.
1 parent be5ea12 commit 3974652

15 files changed

+649
-134
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
composer.phar
33
/vendor/
44
.env
5+
.phpunit.result.cache

config/config.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
// Load ENV vars
44
if (file_exists(__DIR__ . '/../.env')) {
5-
$container['env'] = new \Dotenv\Dotenv(__DIR__.'/../', '.env');
6-
$container['env']->load();
5+
$container['env'] = Dotenv\Dotenv::createImmutable(__DIR__.'/../', '.env');
6+
$container['env']->safeLoad();
77
}
88

99
return [
10-
'slack_url' => getenv('SLACK_URL'),
11-
'meetup_api_key' => getenv('MEETUP_API_KEY'),
10+
'slack' => [
11+
'webhookUrl' => $_ENV['SLACK_URL'],
12+
],
13+
'meetup' => [
14+
'baseUrl' => $_ENV['MEETUP_API_URL'],
15+
'apiKey' => $_ENV['MEETUP_API_KEY'],
16+
],
1217
'joindin' => [
13-
'base_url' => getenv('JOINDIN_URL'),
14-
'access_token' => getenv('JOINDIN_TOKEN')
18+
'baseUrl' => $_ENV['JOINDIN_URL'],
19+
'accessToken' => $_ENV['JOINDIN_TOKEN'],
1520
],
1621
];

config/container.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?php
22

33
use AmsterdamPHP\Console\Api\JoindInClient;
4-
use Crummy\Phlack\Phlack;
4+
use AmsterdamPHP\Console\Api\MeetupClient;
5+
use AmsterdamPHP\Console\Api\Middleware\JsonAwareResponse;
6+
use AmsterdamPHP\Console\Api\SlackWebhookClient;
57
use DI\ContainerBuilder;
6-
use DMS\Service\Meetup\AbstractMeetupClient;
7-
use Joindin\Api\Client;
8+
use GuzzleHttp\HandlerStack;
9+
use GuzzleHttp\Middleware;
10+
use Psr\Http\Message\ResponseInterface;
811

912
// Load configuration
1013
$config = require __DIR__ . '/config.php';
@@ -13,11 +16,11 @@
1316
$containerBuilder = new ContainerBuilder();
1417
$container = $containerBuilder->build();
1518

19+
// Define Deps
20+
21+
1622
// Define services
17-
$container->set(Client::class, new Client($config['joindin']));
18-
$container->set(JoindInClient::class, new JoindInClient($config['joindin']));
19-
$container->set(AbstractMeetupClient::class, \DMS\Service\Meetup\MeetupKeyAuthClient::factory([
20-
'key' => $config['meetup_api_key']
21-
]));
22-
$container->set(Phlack::class, Phlack::factory($config['slack_url']));
23+
$container->set(JoindInClient::class, new JoindInClient($config['joindin']['accessToken'], $config['joindin']['baseUrl']));
24+
$container->set(MeetupClient::class, new MeetupClient($config['meetup']['apiKey'], $config['meetup']['baseUrl']));
25+
$container->set(SlackWebhookClient::class, new SlackWebhookClient($config['slack']['webhookUrl']));
2326
return $container;

src/Api/JoindInClient.php

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,73 @@
22

33
namespace AmsterdamPHP\Console\Api;
44

5+
use AmsterdamPHP\Console\Api\Middleware\DefaultStackFactory;
6+
use AmsterdamPHP\Console\Api\Middleware\JsonAwareResponse;
57
use GuzzleHttp\Client;
6-
use GuzzleHttp\Collection;
8+
use GuzzleHttp\ClientInterface;
9+
use GuzzleHttp\Exception\GuzzleException;
10+
use JsonException;
711
use function json_encode;
8-
use function var_dump;
12+
use function preg_match;
13+
use const JSON_THROW_ON_ERROR;
914

10-
final class JoindInClient extends Client
15+
class JoindInClient
1116
{
12-
const DEFAULT_BASE_URL = 'https://api.joind.in/v2.1';
17+
private ClientInterface $client;
1318

1419
/**
1520
* Constructor
1621
*/
17-
public function __construct(array $config) {
18-
$defaults = array('base_url' => self::DEFAULT_BASE_URL);
19-
$required = array('base_url');
22+
public function __construct(string $token, string $baseUrl) {
23+
$this->client = new Client([
24+
'base_uri' => $baseUrl,
25+
'headers' => [
26+
'Authorization' => 'OAuth ' . $token,
27+
'Accept-Charset' => 'utf-8',
28+
'Accept' => 'application/json',
29+
'Content-Type' => 'application/json',
30+
],
31+
'handler' => DefaultStackFactory::createJsonHandlingStack(),
32+
]);
33+
}
34+
35+
/**
36+
* @throws GuzzleException
37+
* @throws JsonException
38+
*/
39+
public function addEventHost($eventId, $eventHost): JsonAwareResponse
40+
{
41+
/** @var JsonAwareResponse $result */
42+
$result = $this->client->post('v2.1/events/'.$eventId.'/hosts', [
43+
'body' => json_encode(['host_name' => $eventHost], JSON_THROW_ON_ERROR)
44+
]);
2045

21-
$configuration = Collection::fromConfig($config, $defaults, $required);
46+
return $result;
47+
}
2248

23-
parent::__construct($configuration->toArray());
49+
/**
50+
* @throws GuzzleException
51+
* @throws JsonException
52+
*/
53+
public function submitEvent($event): string
54+
{
55+
/** @var JsonAwareResponse $result */
56+
$result = $this->client->post('v2.1/events', [
57+
'body' => json_encode($event, JSON_THROW_ON_ERROR)
58+
]);
2459

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');
60+
return $this->extractIdFromLocation($result->getLocationHeader());
3161
}
3262

33-
public function addEventHost($eventId, $eventHost)
63+
private function extractIdFromLocation($locationUrl): string
3464
{
35-
$result = $this->post('v2.1/events/'.$eventId.'/hosts', ['body' => json_encode(['host_name' => $eventHost])]);
36-
return $result;
65+
$matches = [];
66+
preg_match(
67+
"/events\/(\d*)/",
68+
$locationUrl,
69+
$matches
70+
);
71+
72+
return $matches[1] ?? '';
3773
}
3874
}

src/Api/MeetupClient.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace AmsterdamPHP\Console\Api;
4+
5+
use AmsterdamPHP\Console\Api\Middleware\DefaultStackFactory;
6+
use AmsterdamPHP\Console\Api\Middleware\JsonAwareResponse;
7+
use GuzzleHttp\Client;
8+
use GuzzleHttp\ClientInterface;
9+
use GuzzleHttp\Exception\GuzzleException;
10+
use JsonException;
11+
use Ramsey\Collection\Collection;
12+
use Ramsey\Collection\CollectionInterface;
13+
14+
class MeetupClient
15+
{
16+
private ClientInterface $client;
17+
private string $meetupKey;
18+
19+
public function __construct(string $meetupKey, string $baseUrl)
20+
{
21+
$this->meetupKey = $meetupKey;
22+
$this->client = new Client([
23+
'base_uri' => $baseUrl,
24+
'handler' => DefaultStackFactory::createJsonHandlingStack(),
25+
]);
26+
}
27+
28+
/**
29+
* @throws GuzzleException
30+
* @throws JsonException
31+
*/
32+
public function getUpcomingEventsForGroup(string $group): CollectionInterface
33+
{
34+
/** @var JsonAwareResponse $result */
35+
$result = $this->client->get('/2/events', [
36+
'query' => [
37+
'key' => [$this->meetupKey],
38+
'group_urlname' => $group,
39+
'status' => 'upcoming',
40+
'text_format' => 'plain',
41+
'time' => '0m,1m',
42+
],
43+
]);
44+
45+
return new Collection('array', $result->getJson()['results']);
46+
}
47+
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace AmsterdamPHP\Console\Api\Middleware;
4+
5+
use GuzzleHttp\HandlerStack;
6+
use GuzzleHttp\Middleware;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
final class DefaultStackFactory
10+
{
11+
public static function createJsonHandlingStack(): HandlerStack
12+
{
13+
$stack = HandlerStack::create();
14+
$stack->push(Middleware::mapResponse(
15+
static function (ResponseInterface $response) {
16+
return new JsonAwareResponse(
17+
$response->getStatusCode(),
18+
$response->getHeaders(),
19+
$response->getBody(),
20+
$response->getProtocolVersion(),
21+
$response->getReasonPhrase()
22+
);
23+
}), 'json_decode_middleware');
24+
25+
return $stack;
26+
}
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace AmsterdamPHP\Console\Api\Middleware;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use JsonException;
7+
use Psr\Http\Message\StreamInterface;
8+
use function array_shift;
9+
use function json_decode;
10+
use function var_dump;
11+
use const JSON_THROW_ON_ERROR;
12+
13+
final class JsonAwareResponse extends Response
14+
{
15+
private ?array $cachedJson = null;
16+
17+
/**
18+
* @throws JsonException
19+
*/
20+
public function getJson(): array|StreamInterface
21+
{
22+
if ($this->cachedJson) {
23+
return $this->cachedJson;
24+
}
25+
26+
$body = $this->getBody();
27+
28+
if (!str_contains($this->getHeaderLine('Content-Type'), 'application/json')) {
29+
return $body;
30+
}
31+
32+
if ($body->getSize() === 0) {
33+
return [];
34+
}
35+
36+
$this->cachedJson = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
37+
return $this->cachedJson;
38+
}
39+
40+
public function getLocationHeader(): string
41+
{
42+
$headerValue = $this->getHeader('Location');
43+
return array_shift($headerValue) ?? "";
44+
}
45+
}

src/Api/SlackWebhookClient.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace AmsterdamPHP\Console\Api;
4+
5+
use AmsterdamPHP\Console\Api\Middleware\JsonAwareResponse;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\ClientInterface;
8+
use GuzzleHttp\Exception\GuzzleException;
9+
use JsonException;
10+
use Psr\Http\Message\ResponseInterface;
11+
use function json_encode;
12+
13+
class SlackWebhookClient
14+
{
15+
private ClientInterface $client;
16+
17+
public function __construct(private readonly string $webhookUrl)
18+
{
19+
$this->client = new Client();
20+
}
21+
22+
/**
23+
* @throws GuzzleException
24+
* @throws JsonException
25+
*/
26+
public function sendMessage(array $message): ResponseInterface
27+
{
28+
/** @var JsonAwareResponse $result */
29+
$result = $this->client->post($this->webhookUrl, [
30+
'body' => json_encode($message, JSON_THROW_ON_ERROR),
31+
]);
32+
33+
return $result;
34+
}
35+
}

0 commit comments

Comments
 (0)