Completed
Pull Request — master (#25)
by Patrick
14:02
created

Bearer::authenticate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Starweb\HttpClient\Authentication;
4
5
use Http\Message\Authentication;
6
use Psr\Http\Message\RequestInterface;
7
use Starweb\Api\Authentication\TokenManager;
8
9
final class Bearer implements Authentication
10
{
11
    /**
12
     * @var TokenManager
13
     */
14
    private $tokenManager;
15
16
    /**
17
     * @param string $token
18
     */
19
    public function __construct(TokenManager $tokenManager)
20
    {
21
        $this->tokenManager = $tokenManager;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function authenticate(RequestInterface $request)
28
    {
29
        if ($request->hasHeader('Authorization')) {
30
            return $request;
31
        }
32
33
        $token = $this->tokenManager->getToken()->__toString();
34
        $header = sprintf('Bearer %s', $token);
35
36
        return $request->withHeader('Authorization', $header);
37
    }
38
}
39