Passed
Push — master ( c59e68...e567a6 )
by Thierry
04:50 queued 04:04
created

UnsealResponseMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace lepiaf\SapientBundle\GuzzleHttp\Middleware;
5
6
use ParagonIE\ConstantTime\Base64UrlSafe;
7
use ParagonIE\Sapient\CryptographyKeys\SealingSecretKey;
8
use ParagonIE\Sapient\Sapient;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
class UnsealResponseMiddleware
13
{
14
    private $sapient;
15
    private $privateKey;
16
17 2
    public function __construct(Sapient $sapient, string $privateKey)
18
    {
19 2
        $this->sapient = $sapient;
20 2
        $this->privateKey = $privateKey;
21 2
    }
22
23 2
    public function __invoke(callable $handler)
24
    {
25
        return function(RequestInterface $request, array $options) use ($handler) {
26 2
            return $handler($request, $options)->then(
27
                function(ResponseInterface $response) {
28 2
                    $responseUnsealed = $this->sapient->unsealResponse(
29 2
                        $response,
30 2
                        new SealingSecretKey(Base64UrlSafe::decode($this->privateKey))
31
                    );
32
33 1
                    return $responseUnsealed;
34 2
                }
35
            );
36 2
        };
37
    }
38
}
39