Passed
Push — master ( 4ca9bc...301a00 )
by Thierry
03:55 queued 43s
created

SealRequestMiddleware::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
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 lepiaf\SapientBundle\Service\PublicKeyGetter;
7
use ParagonIE\ConstantTime\Base64UrlSafe;
8
use ParagonIE\Sapient\CryptographyKeys\SealingPublicKey;
9
use ParagonIE\Sapient\Sapient;
10
use Psr\Http\Message\RequestInterface;
11
12
class SealRequestMiddleware
13
{
14
    /**
15
     * @var Sapient
16
     */
17
    private $sapient;
18
19
    /**
20
     * @var PublicKeyGetter
21
     */
22
    private $publicKeyGetter;
23
24
    public function __construct(Sapient $sapient, PublicKeyGetter $publicKeyGetter)
25
    {
26
        $this->sapient = $sapient;
27
        $this->publicKeyGetter = $publicKeyGetter;
28
    }
29
30
    public function __invoke(callable $handler): callable
31
    {
32
        return function(RequestInterface $request, array $options) use ($handler) {
33
            $publicKey = $this->publicKeyGetter->getSealingKey($request);
34
35
            return $handler(
36
                $this->sapient->sealRequest($request, new SealingPublicKey(Base64UrlSafe::decode($publicKey))),
37
                $options
38
            );
39
        };
40
    }
41
}
42