PasetoService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Bone\Paseto;
4
5
use DateInterval;
6
use DateTime;
7
use ParagonIE\Paseto\Builder;
8
use ParagonIE\Paseto\JsonToken;
9
use ParagonIE\Paseto\Keys\SymmetricKey;
10
use ParagonIE\Paseto\Parser;
11
use ParagonIE\Paseto\Protocol\Version2;
12
use ParagonIE\Paseto\ProtocolCollection;
13
use ParagonIE\Paseto\Purpose;
14
use ParagonIE\Paseto\Rules\IssuedBy;
15
use ParagonIE\Paseto\Rules\NotExpired;
16
17
class PasetoService
18
{
19
    /** @var string $sharedKey */
20
    private $sharedKey;
21
22
    /**
23
     * PasetoService constructor.
24
     * @param SymmetricKey $sharedKey
25
     */
26 2
    public function __construct(SymmetricKey $sharedKey)
27
    {
28 2
        $this->sharedKey = $sharedKey;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sharedKey of type ParagonIE\Paseto\Keys\SymmetricKey is incompatible with the declared type string of property $sharedKey.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
    }
30
31
    /**
32
     * @param array $data
33
     * @param string $duration
34
     * @return string
35
     * @throws \ParagonIE\Paseto\Exception\InvalidKeyException
36
     * @throws \ParagonIE\Paseto\Exception\InvalidPurposeException
37
     * @throws \ParagonIE\Paseto\Exception\PasetoException
38
     */
39 1
    public function encryptToken(array $data, string $duration = 'P01D')
40
    {
41 1
        $token = (new Builder())
42 1
            ->setKey($this->sharedKey)
0 ignored issues
show
Bug introduced by
$this->sharedKey of type string is incompatible with the type ParagonIE\Paseto\SendingKey expected by parameter $key of ParagonIE\Paseto\Builder::setKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            ->setKey(/** @scrutinizer ignore-type */ $this->sharedKey)
Loading history...
43 1
            ->setVersion(new Version2())
44 1
            ->setPurpose(Purpose::local())
45 1
            ->setExpiration((new DateTime())->add(new DateInterval($duration)))
46 1
            ->setClaims($data);
47
48 1
        return $token->toString();
49
    }
50
51
    /**
52
     * @param $providedToken
53
     * @return JsonToken
54
     * @throws \ParagonIE\Paseto\Exception\InvalidPurposeException
55
     */
56 1
    public function decryptToken($providedToken): JsonToken
57
    {
58 1
        $parser = (new Parser())
59 1
            ->setKey($this->sharedKey)
0 ignored issues
show
Bug introduced by
$this->sharedKey of type string is incompatible with the type ParagonIE\Paseto\ReceivingKey expected by parameter $key of ParagonIE\Paseto\Parser::setKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            ->setKey(/** @scrutinizer ignore-type */ $this->sharedKey)
Loading history...
60 1
            ->addRule(new NotExpired())
61 1
            ->setPurpose(Purpose::local())
62 1
            ->setAllowedVersions(ProtocolCollection::v2());
63 1
        $token = $parser->parse($providedToken);
64
65 1
        return $token;
66
    }
67
}
68