Completed
Push — master ( 12c461...346425 )
by Jan Philip
03:11
created

NetworkService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 64.71%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 59
ccs 11
cts 17
cp 0.6471
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCurrentWeekWithLocation() 0 7 1
A getWeekWithYearAndLocation() 0 13 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
12
use GuzzleHttp\Client as HttpClient;
13
use GuzzleHttp\Exception\RequestException;
14
use JPBernius\FMeat\Entities\Week;
15
use JPBernius\FMeat\Exeptions\NetworkingException;
16
use JPBernius\FMeat\Utilities\UrlBuilder;
17
use JPBernius\FMeat\Utilities\YearWeekUtil;
18
19
/**
20
 * Class NetworkService
21
 * @package JPBernius\FMeat\Services
22
 */
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)
42
    {
43 3
        $this->httpClient = $httpClient;
44 3
        $this->urlBuilder = $urlBuilder;
45 3
        $this->yearWeekUtil = $yearWeekUtil;
46 3
    }
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
74
    {
75
        $currentYear = $this->yearWeekUtil->getCurrentYear();
76
        $currentWeek = $this->yearWeekUtil->getCurrentCalendarWeek();
77
78
        return $this->getWeekWithYearAndLocation($currentWeek, $currentYear, $location);
79
    }
80
81
}