Completed
Pull Request — master (#1)
by Victor DA COSTA
01:57
created

WindguruAPI::call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like $messageFactory ?: \Http...actoryDiscovery::find() can also be of type object<Http\Message\MessageFactory>. However, the property $_messageFactory is declared as type object<voidtek\WindguruIO\MessageFactory>. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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
}