Passed
Push — master ( 1d51bb...57a30c )
by Thomas
12:26
created

Test_EncryptedModel   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 3
eloc 25
c 5
b 1
f 2
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A writeBaseRecord() 0 5 1
A getField() 0 3 1
A setField() 0 3 1
1
<?php
2
3
namespace LeKoala\Encrypt\Test;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Dev\TestOnly;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Member;
9
use LeKoala\Encrypt\EncryptedFile;
10
use LeKoala\Encrypt\EncryptedDBJson;
11
use LeKoala\Encrypt\EncryptedDBText;
12
use LeKoala\Encrypt\EncryptedDBField;
13
use LeKoala\Encrypt\EncryptedDBVarchar;
14
use LeKoala\Encrypt\HasEncryptedFields;
15
use ParagonIE\CipherSweet\JsonFieldMap;
16
use LeKoala\Encrypt\EncryptedDBHTMLText;
17
use LeKoala\Encrypt\EncryptedNumberField;
18
use LeKoala\Encrypt\EncryptHelper;
19
20
/**
21
 * A test model for our encryption
22
 *
23
 * @property string $Name
24
 * @property string $MyText
25
 * @property string $MyHTMLText
26
 * @property string $MyVarchar
27
 * @property string $MyNumber
28
 * @property string $MyVarcharWithIndex
29
 * @property string $MyJson
30
 * @property string $MyEncryptedJson
31
 * @property int $RegularFileID
32
 * @property int $EncryptedFileID
33
 */
34
class Test_EncryptedModel extends DataObject implements TestOnly
35
{
36
    use HasEncryptedFields;
37
38
    private static $table_name = 'EncryptedModel';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
39
40
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
41
        "Name" => 'Varchar',
42
        "MyText" => EncryptedDBText::class,
43
        "MyHTMLText" => EncryptedDBHTMLText::class,
44
        "MyVarchar" => EncryptedDBVarchar::class,
45
        "MyNumber" => EncryptedNumberField::class . '(["output_size" => 4, "domain_size" => 10])',
46
        "MyIndexedVarchar" => EncryptedDBField::class,
47
        "MyJson" => EncryptedDBJson::class,
48
        "MyEncryptedJson" => EncryptedDBJson::class . "(['map' => '7551830f{\"fields\":{\"$6e616d65\":\"string\",\"$616374697665\":\"bool\",\"$616765\":\"int\"}}'])",
49
    ];
50
51
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
52
        "RegularFile" => File::class,
53
        // We use regular File class for encrypted files to keep only one table
54
        "EncryptedFile" => File::class,
55
        "EncryptedFileClass" => EncryptedFile::class,
56
        "Member" => Member::class,
57
    ];
58
59
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
60
        'MyIndexedVarcharBlindIndex' => true,
61
        'MyNumberBlindIndex' => true,
62
        'MyNumberLastFourBlindIndex' => true,
63
    ];
64
65
    protected function writeBaseRecord($baseTable, $now)
66
    {
67
        parent::writeBaseRecord($baseTable, $now);
68
        // After base record is written, we have an ID and therefore AAD has changed
69
        $this->resetFieldValues();
70
    }
71
72
    public function getField($field)
73
    {
74
        return $this->getEncryptedField($field);
75
    }
76
77
    public function setField($fieldName, $val)
78
    {
79
        return $this->setEncryptedField($fieldName, $val);
80
    }
81
}
82