1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File WindguruAPI.php |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package WindguruIO |
9
|
|
|
* @author Voidtek <[email protected]> |
10
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
11
|
|
|
* @link https://github.com/voidtek/windguru.io |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace voidtek\WindguruIO; |
15
|
|
|
|
16
|
|
|
use Http\Client\Common\Plugin; |
17
|
|
|
use Http\Client\HttpClient; |
18
|
|
|
use Http\Message\UriFactory; |
19
|
|
|
use Http\Discovery\HttpClientDiscovery; |
20
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* WindguruAPI Class |
25
|
|
|
* |
26
|
|
|
* @category Class |
27
|
|
|
* @package WindguruIO |
28
|
|
|
* @author Voidtek <[email protected]> |
29
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
30
|
|
|
* @link https://github.com/voidtek/windguru.io |
31
|
|
|
*/ |
32
|
|
|
class WindguruAPI |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The default Windguru endpoint template. |
37
|
|
|
* Example: 'https://www.windguru.cz/int/iapi.php?q=ads_spot&id_spot%5B%5D=42' |
38
|
|
|
* |
39
|
|
|
* @var string; |
40
|
|
|
*/ |
41
|
|
|
const ENDPOINT='http://www.windguru.cz'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Private HttpClient |
45
|
|
|
* |
46
|
|
|
* @var HttpClient |
47
|
|
|
*/ |
48
|
|
|
private $_httpClient; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Private MessageFactory |
52
|
|
|
* |
53
|
|
|
* @var MessageFactory |
54
|
|
|
*/ |
55
|
|
|
private $_messageFactory; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor WindguruAPI |
59
|
|
|
* |
60
|
|
|
* @param HttpClient|null $httpClient The HttpClient parameter. |
61
|
|
|
* @param MessageFactory|null $messageFactory The messageFactory parameter. |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
HttpClient $httpClient = null, |
65
|
|
|
MessageFactory $messageFactory = null |
66
|
|
|
) { |
67
|
|
|
|
68
|
|
|
$this->_httpClient = $httpClient ?: HttpClientDiscovery::find(); |
69
|
|
|
$this->_messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Call an API endpoint. |
74
|
|
|
* |
75
|
|
|
* @param string $someArgument The Argument for this call. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function call($someArgument) |
80
|
|
|
{ |
81
|
|
|
$request = $this->_messageFactory |
82
|
|
|
->createRequest( |
83
|
|
|
'GET', |
84
|
|
|
self::ENDPOINT.'/some_operation?argument='.$someArgument |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
$response = $this->_httpClient->sendRequest($request); |
89
|
|
|
} catch (\Http\Client\Exception $e) { |
90
|
|
|
throw new \RuntimeException('Something happened during HTTP request'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return (string) $response->getBody(); |
94
|
|
|
} |
95
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.