Completed
Push — master ( a505a0...5d1dca )
by Fèvre
20s
created

EncryptedSecurityType::marshal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace App\Database\Type;
3
4
use Cake\Core\Configure;
5
use Cake\Database\Driver;
6
use Cake\Database\Type;
7
use Cake\Utility\Security;
8
use PDO;
9
10
class EncryptedSecurityType extends Type
11
{
12
    /**
13
     * Convert string values to PHP integers
14
     *
15
     * @param mixed $value The value to convert.
16
     * @param Driver $driver The driver instance to convert with.
17
     * @return string|null
18
     */
19
    public function toPHP($value, Driver $driver)
20
    {
21
        if ($value === null || empty($value)) {
22
            return null;
23
        }
24
25
        return Security::decrypt(base64_decode($value), Configure::read('Security.key'));
26
    }
27
28
    /**
29
     * Marshalls request data into PHP strings.
30
     *
31
     * @param mixed $value The value to convert.
32
     * @return mixed Converted value.
33
     */
34
    public function marshal($value)
35
    {
36
        if ($value === null) {
37
            return $value;
38
        }
39
40
        return base64_encode(Security::encrypt($value, Configure::read('Security.key')));
41
    }
42
43
    /**
44
     * Convert string data into the database format.
45
     *
46
     * @param mixed $value The value to convert.
47
     * @param Driver $driver The driver instance to convert with.
48
     * @return string|null
49
     */
50
    public function toDatabase($value, Driver $driver)
51
    {
52
        if ($value === null) {
53
            return null;
54
        }
55
56
        return $value;
57
    }
58
59
    /**
60
     * Get the correct PDO binding type for string data.
61
     *
62
     * @param mixed $value The value being bound.
63
     * @param Driver $driver The driver.
64
     * @return int
65
     */
66
    public function toStatement($value, Driver $driver)
67
    {
68
        if ($value === null) {
69
            return PDO::PARAM_NULL;
70
        }
71
72
        return PDO::PARAM_STR;
73
    }
74
}
75