NetworkService::getWeekWithLocation()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 5
nop 2
crap 2
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: uni
5
 * Date: 22.11.17
6
 * Time: 19:13
7
 */
8
9
namespace JPBernius\FMeat\Services;
10
11
use GuzzleHttp\Client as HttpClient;
12
use GuzzleHttp\Exception\RequestException;
13
use JPBernius\FMeat\Entities\{
14
    CalendarWeek,
15
    Week
16
};
17
use JPBernius\FMeat\Exeptions\NetworkingException;
18
use JPBernius\FMeat\Utilities\UrlBuilder;
19
20
/**
21
 * Class NetworkService
22
 * @package JPBernius\FMeat\Services
23
 */
24
class NetworkService
25
{
26
27
    /** @var HttpClient */
28
    private $httpClient;
29
30
    /** @var UrlBuilder */
31
    private $urlBuilder;
32
33
    /**
34
     * NetworkService constructor.
35
     * @param HttpClient $httpClient
36
     * @param UrlBuilder $urlBuilder
37
     */
38 6
    public function __construct(HttpClient $httpClient, UrlBuilder $urlBuilder)
39
    {
40 6
        $this->httpClient = $httpClient;
41 6
        $this->urlBuilder = $urlBuilder;
42 6
    }
43
44
    /**
45
     * @param CalendarWeek $calendarWeek
46
     * @param string $location
47
     * @return Week
48
     * @throws NetworkingException
49
     */
50 6
    public function getWeekWithLocation(CalendarWeek $calendarWeek, string $location): Week
51
    {
52 6
        $url = $this->urlBuilder->getUrlForLocationYearWeek($location, $calendarWeek);
53
        try {
54 6
            $response = $this->httpClient->get($url);
55 3
            $jsonResponse = (string)$response->getBody();
56 3
            $jsonObject = json_decode($jsonResponse);
57
58 3
            return Week::fromJson($jsonObject);
59 3
        } catch (RequestException $requestException) {
60 3
            throw new NetworkingException();
61
        }
62
    }
63
64
}