1 | <?php |
||
23 | class NetworkService |
||
24 | { |
||
25 | |||
26 | /** @var HttpClient */ |
||
27 | private $httpClient; |
||
28 | |||
29 | /** @var UrlBuilder */ |
||
30 | private $urlBuilder; |
||
31 | |||
32 | /** @var YearWeekUtil */ |
||
33 | private $yearWeekUtil; |
||
34 | |||
35 | /** |
||
36 | * NetworkService constructor. |
||
37 | * @param HttpClient $httpClient |
||
38 | * @param UrlBuilder $urlBuilder |
||
39 | * @param YearWeekUtil $yearWeekUtil |
||
40 | */ |
||
41 | 3 | public function __construct(HttpClient $httpClient, UrlBuilder $urlBuilder, YearWeekUtil $yearWeekUtil) |
|
47 | |||
48 | /** |
||
49 | * @param int $week |
||
50 | * @param int $year |
||
51 | * @param string $location |
||
52 | * @return Week |
||
53 | * @throws NetworkingException |
||
54 | */ |
||
55 | 3 | public function getWeekWithYearAndLocation(int $week, int $year, string $location): Week |
|
56 | { |
||
57 | 3 | $url = $this->urlBuilder->getUrlForLocationYearWeek($location, $year, $week); |
|
58 | try { |
||
59 | 3 | $response = $this->httpClient->get($url); |
|
60 | 3 | $jsonResponse = (string)$response->getBody(); |
|
61 | 3 | $jsonObject = json_decode($jsonResponse); |
|
62 | |||
63 | 3 | return Week::fromJson($jsonObject); |
|
64 | } catch (RequestException $requestException) { |
||
65 | throw new NetworkingException(); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $location |
||
71 | * @return Week |
||
72 | */ |
||
73 | public function getCurrentWeekWithLocation(string $location): Week |
||
80 | |||
81 | } |