Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afup#1499 Récupération des meetups via GraphQL #1592

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"captioning/captioning": "^2.6",
"ccmbenchmark/ting_bundle": "3.4.1",
"cocur/slugify": "^2.3",
"cuyz/valinor": "^0.17.1",
"doctrine/dbal": "^2.5",
"ekino/newrelic-bundle": "^1.4",
"erusev/parsedown": "^1.6",
Expand Down
77 changes: 76 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sources/AppBundle/Event/Model/Meetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getDate()
}

/**
* @param DateTime $date
* @param \DateTimeInterface $date
*
* @return Meetup
*/
Expand Down
19 changes: 19 additions & 0 deletions sources/AppBundle/Indexation/Meetups/GraphQL/Edge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace AppBundle\Indexation\Meetups\GraphQL;

final class Edge
{
private Node $node;

public function __construct(Node $node)
{
$this->node = $node;
}

public function getNode(): Node
{
return $this->node;
}
}
26 changes: 26 additions & 0 deletions sources/AppBundle/Indexation/Meetups/GraphQL/Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace AppBundle\Indexation\Meetups\GraphQL;

final class Events
{
/** @var Edge[] */
private array $edges;

/**
* @param Edge[] $edges
*/
public function __construct(array $edges)
{
$this->edges = $edges;
}

/**
* @return Edge[]
*/
public function getEdges(): array
{
return $this->edges;
}
}
27 changes: 27 additions & 0 deletions sources/AppBundle/Indexation/Meetups/GraphQL/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace AppBundle\Indexation\Meetups\GraphQL;

final class Group
{
private Events $upcomingEvents;
private Events $pastEvents;

public function __construct(Events $upcomingEvents, Events $pastEvents)
{
$this->upcomingEvents = $upcomingEvents;
$this->pastEvents = $pastEvents;
}

public function getUpcomingEvents(): Events
{
return $this->upcomingEvents;
}

public function getPastEvents(): Events
{
return $this->pastEvents;
}
}
48 changes: 48 additions & 0 deletions sources/AppBundle/Indexation/Meetups/GraphQL/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace AppBundle\Indexation\Meetups\GraphQL;

use DateTimeImmutable;

final class Node
{
private string $id;
private string $title;
private string $eventUrl;
private DateTimeImmutable $dateTime;

/**
* @param string $id
* @param string $title
* @param string $eventUrl
* @param DateTimeImmutable $dateTime
*/
public function __construct(string $id, string $title, string $eventUrl, DateTimeImmutable $dateTime)
{
$this->id = $id;
$this->title = $title;
$this->eventUrl = $eventUrl;
$this->dateTime = $dateTime;
}

public function getId(): string
{
return $this->id;
}

public function getTitle(): string
{
return $this->title;
}

public function getEventUrl(): string
{
return $this->eventUrl;
}

public function getDateTime(): DateTimeImmutable
{
return $this->dateTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace AppBundle\Indexation\Meetups\GraphQL;

final class QueryGroupsResponse
{
/** @var array<string, Group> */
private array $data;

/**
* @param array<string, Group> $data
*/
public function __construct(array $data)
{
$this->data = $data;
}

/**
* @return array<string, Group>
*/
public function getData(): array
{
return $this->data;
}
}
110 changes: 110 additions & 0 deletions sources/AppBundle/Indexation/Meetups/MeetupClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);

namespace AppBundle\Indexation\Meetups;

use AppBundle\Event\Model\Meetup;
use AppBundle\Indexation\Meetups\GraphQL\QueryGroupsResponse;
use AppBundle\Offices\OfficesCollection;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Source\Source;
use CuyZ\Valinor\MapperBuilder;
use GuzzleHttp\Client;

/**
* @phpstan-import-type OfficeType from OfficesCollection
*/
final class MeetupClient
{
private Client $client;

public function __construct(Client $client)
{
$this->client = $client;
}

/**
* @return Meetup[]
*/
public function getEvents(): array
{
$response = $this->client->request('POST', 'https://www.meetup.com/gql', [
'body' => $this->getEventsQuery(),
'headers' => [
'Content-Type' => 'application/json',
],
]);

try {
/** @var QueryGroupsResponse $groupResponse */
$groupResponse = (new MapperBuilder())
->mapper()
->map(QueryGroupsResponse::class, Source::json($response->getBody()->getContents()));
} catch (MappingError $error) {
throw new \Exception("todo", 0, $error);
}

$meetups = [];

foreach ($groupResponse->getData() as $nameAntenne => $group) {
foreach ($group->getUpcomingEvents()->getEdges() as $edge) {
$meetup = new Meetup();
$meetup->setId((int)$edge->getNode()->getId());
$meetup->setTitle($edge->getNode()->getTitle());
$meetup->setDate($edge->getNode()->getDateTime());
$meetup->setAntenneName($nameAntenne);

$meetups[] = $meetup;
}

foreach ($group->getPastEvents()->getEdges() as $edge) {
$meetup = new Meetup();
$meetup->setId((int)$edge->getNode()->getId());
$meetup->setTitle($edge->getNode()->getTitle());
$meetup->setDate($edge->getNode()->getDateTime());
$meetup->setAntenneName($nameAntenne);

$meetups[] = $meetup;
}
}

return $meetups;
}

private function getEventsQuery(): string
{
$query = "query {\n";

foreach ((new OfficesCollection())->getAll() as $name => $office) {
$meetupId = $office['meetup_id'];
$query .= "$name: group(id: $meetupId) { ...GroupFragment }\n";
}

$query .= <<<GRAPHQL
}

fragment EventFragment on Event {
id
title
eventUrl
dateTime
}

fragment GroupFragment on Group {
upcomingEvents(input: {last: 10}) {
edges {
node { ... EventFragment }
}
}

pastEvents(input: {first: 2}, sortOrder: DESC) {
edges {
node { ... EventFragment }
}
}
}
GRAPHQL;

return $query;
}
}
Loading