RequestSigner   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 47
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A signRequest() 0 23 3
1
<?php
2
3
namespace TreeHouse\Keystone\Client;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Psr7\Uri;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Log\LoggerAwareInterface;
9
use Psr\Log\LoggerAwareTrait;
10
use TreeHouse\Keystone\Client\Exception\TokenException;
11
12
class RequestSigner implements LoggerAwareInterface
13
{
14
    use LoggerAwareTrait;
15
16
    /**
17
     * @var TokenPool
18
     */
19
    protected $pool;
20
21
    /**
22
     * @param TokenPool $pool
23
     */
24 11
    public function __construct(TokenPool $pool)
25
    {
26 11
        $this->pool = $pool;
27 11
    }
28
29
    /**
30
     * @param RequestInterface $request
31
     * @param bool             $forceNew
32
     *
33
     * @return RequestInterface
34
     */
35 7
    public function signRequest(RequestInterface $request, $forceNew = false)
36
    {
37
        try {
38
            // force-get a new token if it was requested, if not, the regular
39
            // caching mechanism will be used so the call is not necessary here.
40 7
            if (true === $forceNew) {
41 5
                $this->pool->getToken($forceNew);
42 3
            }
43
44
            // create a new request with the new uri and the token added to the headers
45 6
            $uri = Uri::resolve(
46 6
                new Uri($this->pool->getEndpointUrl()),
47 6
                $request->getUri()
48 6
            );
49
50
            return $request
51 6
                ->withUri($uri)
52 6
                ->withHeader('X-Auth-Token', $this->pool->getTokenId())
53 6
            ;
54 2
        } catch (TokenException $e) {
55 2
            throw new ClientException('Could not obtain token', $request, null, $e);
56
        }
57
    }
58
}
59