|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace judicaelpaquet\IzbergBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Guzzle\Http\Client; |
|
6
|
|
|
use Guzzle\Http\Exception\BadResponseException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class IzbergClient |
|
10
|
|
|
* @package judicaelpaquet\IzbergBundle\Service |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class IzbergClient |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var IzbergConnector |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $izbergConnector; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Client |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $httpClient; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $baseUri = 'https://api.sandbox.iceberg.technology'; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $nameSpace; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param IzbergConnector $izbergConnector |
|
33
|
|
|
* @param string $nameSpace |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function setIzbergConnector(IzbergConnector $izbergConnector) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->izbergConnector = $izbergConnector; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Client $httpClient |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setHttpClient(Client $httpClient) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->httpClient = $httpClient; |
|
46
|
|
|
$this->httpClient->setBaseUrl(self::$baseUri); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $namespace |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
|
|
public function setNameSpace(string $nameSpace) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->nameSpace = $nameSpace; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function generateHeader() |
|
61
|
|
|
{ |
|
62
|
|
|
$namespace = ''; |
|
63
|
|
|
if ($this->izbergConnector->getAuthentication()['username'] == 'Anonymous') { |
|
64
|
|
|
$namespace = $this->nameSpace; |
|
65
|
|
|
$namespace = ':'.$namespace; |
|
66
|
|
|
} |
|
67
|
|
|
return [ |
|
68
|
|
|
'Content-Type' => 'application/json', |
|
69
|
|
|
'Authorization' => vsprintf("Bearer %s%s:%s", [$this->izbergConnector->getAuthentication()['username'], $namespace, $this->izbergConnector->getAuthentication()['api_key']]), |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $url |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getObjects(string $url) |
|
78
|
|
|
{ |
|
79
|
|
|
$request = $this->httpClient->get($url); |
|
80
|
|
|
$request->setHeaders($this->generateHeader()); |
|
81
|
|
|
return $request->send()->json()['objects']; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $url |
|
86
|
|
|
* @param int $id |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getObject(string $url, int $id) |
|
90
|
|
|
{ |
|
91
|
|
|
$request = $this->httpClient->get($url.'/'.$id); |
|
92
|
|
|
$request->setHeaders($this->generateHeader()); |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
|
|
return $request->send()->json(); |
|
96
|
|
|
} catch(BadResponseException $e) { |
|
97
|
|
|
echo $e->getMessage(); |
|
98
|
|
|
echo 'There is no object with this id:'.$id; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $url |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getObjectSchema(string $url) |
|
107
|
|
|
{ |
|
108
|
|
|
$request = $this->httpClient->get($url.'/schema'); |
|
109
|
|
|
$request->setHeaders($this->generateHeader()); |
|
110
|
|
|
return $request->send()->json(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $functionName |
|
115
|
|
|
* @param $params |
|
116
|
|
|
* @return array|string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function __call($functionName, $params) |
|
119
|
|
|
{ |
|
120
|
|
|
$className = (new \ReflectionClass($this))->getShortName(); |
|
121
|
|
|
$apiNameCalled = str_replace('Izberg', '', $className); |
|
122
|
|
|
|
|
123
|
|
|
switch($functionName) { |
|
124
|
|
|
case 'get'.$apiNameCalled.'s': |
|
125
|
|
|
return $this->getObjects(static::$url); |
|
126
|
|
|
break; |
|
|
|
|
|
|
127
|
|
|
case 'get'.$apiNameCalled: |
|
128
|
|
|
return $this->getObject(static::$url, $params[0]); |
|
129
|
|
|
break; |
|
|
|
|
|
|
130
|
|
|
case 'get'.$apiNameCalled.'Schema': |
|
131
|
|
|
return $this->getObjectSchema(static::$url); |
|
132
|
|
|
break; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.