Passed
Push — master ( e9ba59...c2c890 )
by Tony Karavasilev (Тони
19:20
created

TokenAsymmetricTransformationTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 45
ccs 0
cts 23
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extractAuthenticationToken() 0 3 1
A generateAuthenticationToken() 0 22 2
1
<?php
2
3
/**
4
 * Trait implementation for the generation and the transformation of authentication tokens.
5
 */
6
7
namespace CryptoManana\Core\Traits\Containers;
8
9
use \CryptoManana\Core\Abstractions\Randomness\AbstractGenerator as RandomnessSource;
10
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractAsymmetricEncryptionAlgorithm as AsymmetricEncryption;
11
use \CryptoManana\Core\Interfaces\MessageEncryption\DataEncryptionInterface as DataEncryption;
12
use \CryptoManana\Core\Interfaces\Containers\TokenTransformationInterface as TokenTransformationSpecification;
13
use \CryptoManana\DataStructures\AuthenticationToken as AuthenticationTokenStructure;
14
15
/**
16
 * Trait TokenAsymmetricTransformationTrait - Reusable implementation of `TokenTransformationInterface`.
17
 *
18
 * @see \CryptoManana\Core\Interfaces\Containers\TokenTransformationInterface The abstract specification.
19
 *
20
 * @package CryptoManana\Core\Traits\Containers
21
 *
22
 * @property RandomnessSource $randomnessSource The randomness generator.
23
 * @property AsymmetricEncryption|DataEncryption|null $asymmetricCipherSource The message asymmetric encryption service.
24
 *
25
 * @mixin TokenTransformationSpecification
26
 */
27
trait TokenAsymmetricTransformationTrait
28
{
29
    /**
30
     * Generate a secure token and create an encrypt representation.
31
     *
32
     * @param int $length The desired output string length in bytes (default => 64).
33
     *
34
     * @return AuthenticationTokenStructure The authentication token data structure object.
35
     * @throws \Exception Validation errors.
36
     */
37
    public function generateAuthenticationToken($length = 64)
38
    {
39
        $length = filter_var(
40
            $length,
41
            FILTER_VALIDATE_INT,
42
            [
43
                "options" => [
44
                    "min_range" => 1,
45
                    "max_range" => PHP_INT_MAX,
46
                ],
47
            ]
48
        );
49
50
        if ($length === false) {
51
            throw new \LengthException(
52
                'The internal length of the desired secure token must me at least 1 character long.'
53
            );
54
        }
55
56
        $token = $this->randomnessSource->getAlphaNumeric($length, true);
57
58
        return new AuthenticationTokenStructure($token, $this->asymmetricCipherSource->encryptData($token));
2 ignored issues
show
Bug introduced by
The method encryptData() does not exist on null. ( Ignorable by Annotation )

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

58
        return new AuthenticationTokenStructure($token, $this->asymmetricCipherSource->/** @scrutinizer ignore-call */ encryptData($token));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method encryptData() does not exist on CryptoManana\Core\Abstra...tricEncryptionAlgorithm. It seems like you code against a sub-type of CryptoManana\Core\Abstra...tricEncryptionAlgorithm such as CryptoManana\Core\Abstra...n\AbstractRsaEncryption. ( Ignorable by Annotation )

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

58
        return new AuthenticationTokenStructure($token, $this->asymmetricCipherSource->/** @scrutinizer ignore-call */ encryptData($token));
Loading history...
59
    }
60
61
    /**
62
     * Extracts the token from the cipher data via a predefined configuration.
63
     *
64
     * @param string $cipherToken The encrypted token string.
65
     *
66
     * @return string The decrypted authentication token.
67
     * @throws \Exception Validation errors.
68
     */
69
    public function extractAuthenticationToken($cipherToken)
70
    {
71
        return $this->asymmetricCipherSource->decryptData($cipherToken);
1 ignored issue
show
Bug introduced by
The method decryptData() does not exist on CryptoManana\Core\Abstra...tricEncryptionAlgorithm. It seems like you code against a sub-type of CryptoManana\Core\Abstra...tricEncryptionAlgorithm such as CryptoManana\Core\Abstra...n\AbstractRsaEncryption. ( Ignorable by Annotation )

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

71
        return $this->asymmetricCipherSource->/** @scrutinizer ignore-call */ decryptData($cipherToken);
Loading history...
72
    }
73
}
74