Completed
Branch dev (9c0c80)
by Jakob
14:36
created

Client   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A query() 0 17 2
1
<?php declare(strict_types=1);
2
  
3
namespace JSKOS;
4
  
5
use Http\Client\HttpClient;
6
use Http\Discovery\HttpClientDiscovery;
7
use Http\Discovery\MessageFactoryDiscovery;
8
use Http\Client\Exception as ClientException;
9
10
/**
11
 * JSKOS API Client
12
 */
13
class Client extends Service
14
{
15
    protected $baseUrl;
16
	protected $httpClient;
17
    protected $requestFactory;
18
19
    public function __construct(
20
        string $baseUrl,
21
        HttpClient $client=null, 
22
        RequestFactory $requestFactory=null
23
    )
24
    {
25
		$this->baseUrl        = $baseUrl;
26
		$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
27
		$this->httpClient     = $client ?: HttpClientDiscovery::find();
28
	}
29
30
	public function query(array $query=[], string $path=''): Result {
31
		$url = $this->baseUrl . $path;
32
                
33
		if (count($query)) {
34
			$url .= '?' . http_build_query($query);
35
		}
36
37
        $request = $this->requestFactory->createRequest('GET', $url, []);
38
		$response = $this->httpClient->sendRequest($request);
39
40
        # TOOD: detect and throw error
41
42
        $data = json_decode((string)$response->getBody(),true);
43
44
		# TODO: add total, offset, limit of page
45
        return new Result($data ?? []);
46
	}
47
}
48