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

Key::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 3
crap 2
1
<?php
2
3
namespace Flipbox\Relay\HubSpot\Middleware\Authorization;
4
5
use Flipbox\Relay\Middleware\AbstractMiddleware;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
class Key extends AbstractMiddleware
10
{
11
12
    /**
13
     * @var string
14
     */
15
    public $key;
16
17
    /**
18
     * @inheritdoc
19
     */
20
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next = null)
21
    {
22
        return $next($this->prepRequest($request), $response);
23
    }
24
25
    /**
26
     * @param RequestInterface $request
27
     * @return RequestInterface
28
     */
29
    private function prepRequest(RequestInterface $request)
30
    {
31
        // Requested URI
32
        $uri = $request->getUri();
33
34
        // Get Query
35
        $query = $uri->getQuery();
36
37
        // Append to?
38
        if (!empty($query)) {
39
            $query .= '&';
40
        }
41
42
        // Add our key
43
        $query .= http_build_query([
44
            'hapikey' => $this->key
45
        ]);
46
47
        return $request->withUri(
48
            $uri->withQuery($query)
49
        );
50
    }
51
}
52