1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dafiti\Datajet; |
4
|
|
|
|
5
|
|
|
use Dafiti\Datajet\Exception\ResourceNotFound; |
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Http client thats provide a simple way to use Datajet service in PHP. |
11
|
|
|
*/ |
12
|
|
|
class Client |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $resources = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var GuzzleHttp\Client |
21
|
|
|
*/ |
22
|
|
|
private $httpClient; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $config = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Psr\Log\LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
private $logger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Client accept a Guzzle Http Client into constructor param. |
36
|
|
|
* |
37
|
|
|
* @param \GuzzleHttp\Client $httpClient Guzzle Http client instance |
38
|
|
|
*/ |
39
|
4 |
|
public function __construct(HttpClient $httpClient, array $config = [], LoggerInterface $logger = null) |
40
|
|
|
{ |
41
|
4 |
|
$this->httpClient = $httpClient; |
|
|
|
|
42
|
4 |
|
$this->config = $config; |
43
|
4 |
|
$this->logger = $logger; |
|
|
|
|
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get return instance. |
48
|
|
|
* |
49
|
|
|
* @param string $resource Resource name |
50
|
|
|
* |
51
|
|
|
* @throws Dafiti\Datajet\Exception\ResourceNotFound |
52
|
|
|
* |
53
|
|
|
* @return Dafiti\Datajet\Resource\AbstractResource |
54
|
|
|
*/ |
55
|
3 |
|
public function __get($resource) |
56
|
|
|
{ |
57
|
3 |
|
if (isset($this->resources[$resource])) { |
58
|
1 |
|
return $this->resources[$resource]; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
$class = '\\Dafiti\\Datajet\\Resource\\'.ucfirst($resource); |
62
|
|
|
|
63
|
3 |
|
if (!class_exists($class)) { |
64
|
1 |
|
throw new ResourceNotFound($resource); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
$this->resources[$resource] = new $class($this->httpClient, $this->config, $this->logger); |
68
|
|
|
|
69
|
2 |
|
return $this->resources[$resource]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create a instance of Client with defined config. |
74
|
|
|
* |
75
|
|
|
* @param array $config Datajet request config |
76
|
|
|
* |
77
|
|
|
* @return \Dafiti\Datajet\Client |
78
|
|
|
*/ |
79
|
1 |
|
public static function create(array $config = [], LoggerInterface $logger = null) |
80
|
|
|
{ |
81
|
|
|
$httpConfig = [ |
82
|
1 |
|
'defaults' => [ |
83
|
|
|
'headers' => [ |
84
|
|
|
'Content-Type' => 'application/json', |
85
|
|
|
], |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
|
89
|
1 |
|
$httpClient = new HttpClient($httpConfig); |
90
|
|
|
|
91
|
1 |
|
return new self($httpClient, $config, $logger); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..