Lines 100.00% 13 / 13
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
TravelMapClient 100.00% 13 / 13 100.00% 2 / 2 3 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fetch 100.00% 12 / 12 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Client\Api\TravelMap;
6
7final readonly class TravelMapClient
8{
9    /** @param non-empty-string $url */
10    public function __construct(
11        private string $url = 'https://game.aq.com/game/api/data/travelmap',
12    ) {}
13
14    /** @throws TravelMapUnavailableException */
15    public function fetch(): TravelMap
16    {
17        $curl = curl_init($this->url);
18        curl_setopt_array($curl, [
19            CURLOPT_RETURNTRANSFER => true,
20            CURLOPT_FOLLOWLOCATION => true,
21            CURLOPT_TIMEOUT => 20,
22            CURLOPT_CONNECTTIMEOUT => 5,
23            CURLOPT_USERAGENT => 'Mozilla/5.0',
24        ]);
25
26        $response = curl_exec($curl);
27        if (!is_string($response)) {
28            throw new TravelMapUnavailableException('Could not reach the travel map API: ' . curl_error($curl));
29        }
30
31        return TravelMap::fromJson($response);
32    }
33}