UnsealResponseMiddleware::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
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
    /**
15
     * @var Sapient
16
     */
17
    private $sapient;
18
19
    /**
20
     * @var string
21
     */
22
    private $privateKey;
23
24 2
    public function __construct(Sapient $sapient, string $privateKey)
25
    {
26 2
        $this->sapient = $sapient;
27 2
        $this->privateKey = $privateKey;
28 2
    }
29
30 2
    public function __invoke(callable $handler)
31
    {
32
        return function(RequestInterface $request, array $options) use ($handler) {
33 2
            return $handler($request, $options)->then(
34
                function(ResponseInterface $response) {
35 2
                    $responseUnsealed = $this->sapient->unsealResponse(
36 2
                        $response,
37 2
                        new SealingSecretKey(Base64UrlSafe::decode($this->privateKey))
38
                    );
39
40 1
                    return $responseUnsealed;
41 2
                }
42
            );
43 2
        };
44
    }
45
}
46