Passed
Push — master ( 5679ef...aad9e9 )
by Kirill
04:11
created

HeaderTransport::commitToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 2
nop 4
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Auth\Transport;
13
14
use Psr\Http\Message\ResponseInterface as Response;
15
use Psr\Http\Message\ServerRequestInterface as Request;
16
use Spiral\Auth\HttpTransportInterface;
17
18
/**
19
 * Reads and writes auth tokens via headers.
20
 */
21
final class HeaderTransport implements HttpTransportInterface
22
{
23
    /** @var string */
24
    private $header;
25
26
    /**
27
     * @param string $header
28
     */
29
    public function __construct(string $header = 'X-Auth-Token')
30
    {
31
        $this->header = $header;
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function fetchToken(Request $request): ?string
38
    {
39
        if ($request->hasHeader($this->header)) {
40
            return $request->getHeaderLine($this->header);
41
        }
42
43
        return null;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function commitToken(
50
        Request $request,
51
        Response $response,
52
        string $tokenID,
53
        \DateTimeInterface $expiresAt = null
54
    ): Response {
55
        if ($request->hasHeader($this->header) && $request->getHeaderLine($this->header) === $tokenID) {
56
            return $response;
57
        }
58
59
        return $response->withAddedHeader($this->header, $tokenID);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function removeToken(Request $request, Response $response, string $tokenID): Response
66
    {
67
        return $response;
68
    }
69
}
70