Passed
Push — master ( 461cd7...06c7d6 )
by Thomas
02:35
created

Test_EncryptionKey   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 2
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyPair() 0 10 2
A getForMember() 0 4 1
1
<?php
2
3
namespace LeKoala\Encrypt\Test;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Dev\TestOnly;
7
use ParagonIE\ConstantTime\Hex;
8
use SilverStripe\ORM\DataObject;
9
10
/**
11
 * @property string $EncryptionKey
12
 * @property int $MemberID
13
 */
14
class Test_EncryptionKey extends DataObject implements TestOnly
15
{
16
    private static $table_name = 'EncryptionKey';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
17
18
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        // A 256 bit key (32 bytes = 32*8)
20
        // 32 bytes = 64 chars in hex (eg using bin2hex)
21
        "EncryptionKey" => 'Varchar',
22
23
        // X25519 Keypair (2x 32 bytes keys)
24
        "SecretKey" => 'Varchar',
25
        "PublicKey" => 'Varchar',
26
    ];
27
28
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
29
        "Member" => File::class,
30
    ];
31
32
    public static function getForMember($ID)
33
    {
34
        $rec = self::get()->filter('MemberID', $ID)->first();
35
        return $rec->EncryptionKey ?? null;
36
    }
37
38
    public static function getKeyPair($ID)
39
    {
40
        $rec = self::get()->filter('MemberID', $ID)->first();
41
        if ($rec) {
42
            return [
43
                'public' => Hex::decode($rec->PublicKey),
0 ignored issues
show
Bug introduced by
It seems like $rec->PublicKey can also be of type null; however, parameter $encodedString of ParagonIE\ConstantTime\Hex::decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

43
                'public' => Hex::decode(/** @scrutinizer ignore-type */ $rec->PublicKey),
Loading history...
44
                'secret' => Hex::decode($rec->SecretKey),
45
            ];
46
        }
47
        return false;
48
    }
49
}
50