Completed
Push — master ( 481c33...0c5dca )
by Nate
04:03
created

Client::syncResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 6
nop 2
crap 6
1
<?php
2
3
/**
4
 * REST Middleware
5
 *
6
 * @package    Force
7
 * @author     Flipbox Factory <[email protected]>
8
 * @copyright  2010-2016 Flipbox Digital Limited
9
 * @license    https://flipboxfactory.com/software/craft/force/license
10
 * @version    Release: 1.3.0
11
 * @link       https://github.com/FlipboxFactory/Force
12
 * @since      Class available since Release 1.0.0
13
 */
14
15
namespace Flipbox\Relay\HubSpot\Middleware;
16
17
use Flipbox\Relay\Middleware\AbstractMiddleware;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Flipbox\Relay\HubSpot\Mi...ware\AbstractMiddleware.

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...
18
use GuzzleHttp\Client as GuzzleHttpClient;
19
use GuzzleHttp\Exception\ClientException;
20
use GuzzleHttp\Psr7\Response as HttpResponse;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
24
class Client extends AbstractMiddleware
25
{
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next = null)
31
    {
32
        // Prepare request
33
        $request = $this->prepRequest($request);
34
35
        // Call API
36
        $response = $this->call($request, $response);
37
38
        // Onward
39
        $response = $next($request, $response);
40
41
        return $response;
42
    }
43
44
    /**
45
     * Call the API
46
     *
47
     * @param RequestInterface $request
48
     * @param ResponseInterface $response
49
     * @return ResponseInterface
50
     */
51
    private function call(RequestInterface $request, ResponseInterface $response)
52
    {
53
54
        try {
55
            // todo is there a way to append to an existing guzzle response?
56
            $client = new GuzzleHttpClient();
57
            $httpResponse = $client->send($request);
58
        } catch (ClientException $e) {
59
            $this->error(
60
                "API Exception",
61
                [
62
                    'exception' => $e
63
                ]
64
            );
65
            $httpResponse = $e->getResponse();
66
        }
67
68
        // Sync responses
69
        return $this->syncResponse($httpResponse, $response);
0 ignored issues
show
Documentation introduced by
$httpResponse is of type object<Psr\Http\Message\ResponseInterface>|null, but the function expects a object<GuzzleHttp\Psr7\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
    }
71
72
    /**
73
     * @param RequestInterface $request
74
     * @return RequestInterface
75
     */
76
    private function prepRequest(RequestInterface $request)
77
    {
78
        return $request->withHeader('Content-Type', 'application/json')
79
            ->withHeader('Accept', 'application/json');
80
    }
81
82
    /**
83
     * @param HttpResponse $httpResponse
84
     * @param ResponseInterface $response
85
     * @return ResponseInterface
86
     */
87
    private function syncResponse(HttpResponse $httpResponse, ResponseInterface $response)
88
    {
89
        // Add headers
90
        foreach ($httpResponse->getHeaders() as $name => $value) {
91
            $response = $response->withHeader($name, $value);
92
        }
93
94
        return $response->withStatus($httpResponse->getStatusCode(), $httpResponse->getReasonPhrase())
95
            ->withBody($httpResponse->getBody())
96
            ->withProtocolVersion($httpResponse->getProtocolVersion());
97
    }
98
}
99