1
|
|
|
<?php |
2
|
|
|
namespace Sourceout\LastFm\Http; |
3
|
|
|
|
4
|
|
|
use Http\Client\HttpClient; |
5
|
|
|
use Http\Discovery\HttpClientDiscovery; |
6
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
7
|
|
|
use Http\Message\MessageFactory; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
|
|
|
9
|
|
|
use Sourceout\LastFm\Exception\HttpTransferException; |
10
|
|
|
|
11
|
|
|
class Http implements HttpInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** @var HttpClient */ |
15
|
|
|
private $httpClient; |
16
|
|
|
|
17
|
|
|
/** @var MessageFactory */ |
18
|
|
|
private $messageFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
|
|
*/ |
23
|
6 |
|
public function sendRequest( |
24
|
|
|
string $method, |
25
|
|
|
$uri, |
26
|
|
|
$body = null, |
27
|
|
|
array $headers = [], |
28
|
|
|
string $protocolVersion = '1.1' |
29
|
|
|
) : ResponseInterface { |
30
|
|
|
// @codeCoverageIgnoreStart |
31
|
|
|
$request = $this->getMessageFactory()->createRequest( |
32
|
|
|
$method, |
33
|
|
|
$uri, |
34
|
|
|
$headers, |
35
|
|
|
$body, |
36
|
|
|
$protocolVersion |
37
|
|
|
); |
38
|
|
|
// @codeCoverageIgnoreEnd |
39
|
|
|
|
40
|
|
|
try { |
41
|
6 |
|
return $this->getHttpClient()->sendRequest($request); |
42
|
3 |
|
} catch (\Exception $e) { |
43
|
3 |
|
throw new HttpTransferException( |
44
|
3 |
|
"Error while requesting data".$e->getMessage(), |
45
|
3 |
|
$e->getCode() |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
8 |
|
public function setHttpClient(HttpClient $httpClient) : void |
54
|
|
|
{ |
55
|
8 |
|
$this->httpClient = $httpClient; |
56
|
8 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
7 |
|
public function getHttpClient() : HttpClient |
62
|
|
|
{ |
63
|
7 |
|
$this->httpClient = $this->httpClient ?: HttpClientDiscovery::find(); |
64
|
7 |
|
return $this->httpClient; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
8 |
|
public function setMessageFactory(MessageFactory $messageFactory) : void |
71
|
|
|
{ |
72
|
8 |
|
$this->messageFactory = $messageFactory; |
73
|
8 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
7 |
|
public function getMessageFactory() : MessageFactory |
79
|
|
|
{ |
80
|
7 |
|
$this->messageFactory = $this->messageFactory ?: MessageFactoryDiscovery::find(); |
81
|
7 |
|
return $this->messageFactory; |
82
|
|
|
} |
83
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: