Passed
Push — master ( 5383e7...548176 )
by Thomas
02:56
created

EncryptedNumberField::writeToManipulation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
rs 9.9
cc 2
nc 2
nop 1
1
<?php
2
3
namespace LeKoala\Encrypt;
4
5
use ParagonIE\CipherSweet\BlindIndex;
6
use ParagonIE\CipherSweet\EncryptedField;
7
use ParagonIE\CipherSweet\Transformation\LastFourDigits;
8
9
/**
10
 * Value will be set on parent record through built in getField
11
 * mechanisms for composite fields
12
 *
13
 * This can be useful to store phone numbers, national numbers...
14
 * We keep two indexes:
15
 * - One with the full record
16
 * - One with the last 4 numbers (so if your phone number is +00 471 123 456, it will be searchable with 3456)
17
 */
18
class EncryptedNumberField extends EncryptedDBField
19
{
20
    /**
21
     * @param array
22
     */
23
    private static $composite_db = array(
0 ignored issues
show
introduced by
The private property $composite_db is not used, and could be removed.
Loading history...
24
        "Value" => "Varchar(191)",
25
        "BlindIndex" => 'Varchar(32)',
26
        "LastFourBlindIndex" => 'Varchar(16)',
27
    );
28
29
    /**
30
     * @param int $default
31
     * @return int
32
     */
33
    public function getLastFourIndexSize($default = null)
34
    {
35
        if (array_key_exists('last_four_index_size', $this->options)) {
36
            return $this->options['last_four_index_size'];
37
        }
38
        return $default;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getLastFourBlindIndexField()
45
    {
46
        return $this->getField('LastFourBlindIndex');
47
    }
48
49
    /**
50
     * @return $this
51
     */
52
    public function setLastFourBlindIndexField($value, $markChanged = true)
53
    {
54
        return $this->setField('LastFourBlindIndex', $value, $markChanged);
55
    }
56
57
    /**
58
     * @param CipherSweet $engine
0 ignored issues
show
Bug introduced by
The type LeKoala\Encrypt\CipherSweet was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
     * @return EncryptedField
60
     */
61
    public function getEncryptedField($engine = null)
62
    {
63
        if ($engine === null) {
64
            $engine = EncryptHelper::getCipherSweet();
65
        }
66
        $lastFourIndexSize = $this->getLastFourIndexSize(self::SMALL_INDEX_SIZE);
67
        $indexSize = $this->getIndexSize(self::LARGE_INDEX_SIZE);
68
        // fieldName needs to match exact db name for row rotator to work properly
69
        $encryptedField = (new EncryptedField($engine, $this->tableName, $this->name . "Value"))
70
            ->addBlindIndex(new BlindIndex($this->name . "LastFourBlindIndex", [new LastFourDigits()], $lastFourIndexSize))
71
            ->addBlindIndex(new BlindIndex($this->name . "BlindIndex", [], $indexSize));
72
        return $encryptedField;
73
    }
74
75
    public function addToQuery(&$query)
76
    {
77
        parent::addToQuery($query);
78
        $query->selectField(sprintf('"%sValue"', $this->name));
79
        $query->selectField(sprintf('"%sBlindIndex"', $this->name));
80
        $query->selectField(sprintf('"%sLastFourBlindIndex"', $this->name));
81
    }
82
}
83