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\TravelMap;
6
7use Lorekeeper\Client\Exception\TravelMapUnavailableException;
8
9final readonly class TravelMapClient
10{
11    /** @param non-empty-string $url */
12    public function __construct(
13        private string $url = 'https://game.aq.com/game/api/data/travelmap',
14    ) {}
15
16    /** @throws TravelMapUnavailableException */
17    public function fetch(): TravelMap
18    {
19        $curl = curl_init($this->url);
20        curl_setopt_array($curl, [
21            CURLOPT_RETURNTRANSFER => true,
22            CURLOPT_FOLLOWLOCATION => true,
23            CURLOPT_TIMEOUT => 20,
24            CURLOPT_CONNECTTIMEOUT => 5,
25            CURLOPT_USERAGENT => 'Mozilla/5.0',
26        ]);
27
28        $response = curl_exec($curl);
29        if (!is_string($response)) {
30            throw new TravelMapUnavailableException('Could not reach the travel map API: ' . curl_error($curl));
31        }
32
33        return TravelMap::fromJson($response);
34    }
35}