MeetupApiService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Meetup;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Psr7\Request;
9
10
class MeetupApiService
11
{
12
    private $httpClient;
13
    private $key;
14
15
    const API_URL = 'https://api.meetup.com/2/rsvps?event_id=%s&key=%s';
16
17
    /**
18
     * MeetupApiService constructor.
19
     *
20
     * @param $key
21
     */
22
    public function __construct($key)
23
    {
24
        $this->httpClient = new Client();
25
        $this->key = $key;
26
    }
27
28
    /**
29
     * If something goes wrong on the meetup.com API
30
     * we want to continue and render our page anyway.
31
     *
32
     * @param  $eventUrl     URL from meetup.com web site
33
     *
34
     * @return array
35
     */
36
    public function getAttendees($eventUrl)
37
    {
38
        if (strpos($eventUrl, 'meetup.com') === false) {
39
            return [];
40
        }
41
42
        try {
43
            $parts = explode('/', $eventUrl);
44
            $meetupId = $parts[count($parts) - 2];
45
            $uri = sprintf(self::API_URL, $meetupId, $this->key);
46
            $request = new Request('GET', $uri);
47
            $response = $this->httpClient->send($request);
48
            $data = \GuzzleHttp\json_decode($response->getBody()->getContents());
49
            $attendees = $data->results;
50
            shuffle($attendees);
51
52
            return $attendees;
53
        } catch (\Exception $e) {
54
            return [];
55
        }
56
    }
57
}
58