Http   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 71
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setHttpClient() 0 3 1
A getMessageFactory() 0 4 2
A getHttpClient() 0 4 2
A setMessageFactory() 0 3 1
A sendRequest() 0 23 2
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Sourceout\LastFm\Http\ResponseInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
}