1
|
|
|
<?php |
2
|
|
|
namespace Carnage\EncryptedColumn\Dbal; |
3
|
|
|
|
4
|
|
|
use Carnage\EncryptedColumn\Service\EncryptionService; |
5
|
|
|
use Carnage\EncryptedColumn\ValueObject\ValueHolder; |
6
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
7
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use Carnage\EncryptedColumn\ValueObject\EncryptedColumn as EncryptedColumnVO; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Object type for reading from legacy encrypted column extensions and converting to a better format |
13
|
|
|
* |
14
|
|
|
* This type is a drop in replacement for the EncryptedColumn class but drops some strictness to allow for |
15
|
|
|
* reading data that is not in an expected format. You should only use this if you have existing data in |
16
|
|
|
* your database you wish to convert |
17
|
|
|
* |
18
|
|
|
* Class EncryptedColumn |
19
|
|
|
*/ |
20
|
|
|
class EncryptedColumnLegacySupport extends Type |
21
|
|
|
{ |
22
|
|
|
const ENCRYPTED = 'encrypted'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EncryptionService |
26
|
|
|
*/ |
27
|
|
|
private $encryptionService; |
28
|
|
|
|
29
|
1 |
View Code Duplication |
public static function create(EncryptionService $encryptionService) |
|
|
|
|
30
|
|
|
{ |
31
|
1 |
|
Type::addType(EncryptedColumnLegacySupport::ENCRYPTED, EncryptedColumnLegacySupport::class); |
32
|
|
|
/** @var EncryptedColumnLegacySupport $instance */ |
33
|
1 |
|
$instance = Type::getType(EncryptedColumnLegacySupport::ENCRYPTED); |
34
|
1 |
|
$instance->encryptionService = $encryptionService; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
1 |
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
38
|
|
|
{ |
39
|
1 |
|
return $platform->getClobTypeDeclarationSQL($fieldDeclaration); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
43
|
|
|
{ |
44
|
1 |
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function getName() |
48
|
|
|
{ |
49
|
1 |
|
return self::ENCRYPTED; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
53
|
|
|
{ |
54
|
2 |
|
if ($value === null) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
try { |
59
|
2 |
|
$decoded = $this->decodeJson($value); |
60
|
1 |
|
} catch (ConversionException $e) { |
61
|
|
|
//The data wasn't in the format we expected, assume it is legacy data which needs converting |
62
|
|
|
//Drop in some defaults to allow the library to handle it. |
63
|
|
|
$decoded = [ |
64
|
1 |
|
'data' => $value, |
65
|
|
|
'classname' => ValueHolder::class, |
66
|
1 |
|
'serializer' => 'legacy', |
67
|
1 |
|
'encryptor' => 'legacy', |
68
|
|
|
'keyid' => 'legacy', |
69
|
|
|
]; |
70
|
|
|
} |
71
|
2 |
|
|
72
|
|
|
return $this->encryptionService->decryptField(EncryptedColumnVO::fromArray($decoded)); |
73
|
|
|
} |
74
|
1 |
|
|
75
|
|
View Code Duplication |
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
|
|
|
76
|
1 |
|
{ |
77
|
|
|
if ($value === null) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
1 |
|
|
81
|
|
|
return json_encode($this->encryptionService->encryptField($value)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Based on: https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/JsonDeserializationVisitor.php |
86
|
|
|
* |
87
|
|
|
* @param $value |
88
|
|
|
* @return mixed |
89
|
|
|
* @throws ConversionException |
90
|
2 |
|
*/ |
91
|
|
View Code Duplication |
private function decodeJson($value) |
|
|
|
|
92
|
2 |
|
{ |
93
|
|
|
$decoded = json_decode($value, true); |
94
|
2 |
|
|
95
|
2 |
|
switch (json_last_error()) { |
96
|
1 |
|
case JSON_ERROR_NONE: |
97
|
|
|
if (!is_array($decoded)) { |
98
|
|
|
throw ConversionException::conversionFailed($value, 'Json was not an array'); |
99
|
1 |
|
} |
100
|
1 |
|
return $decoded; |
101
|
|
|
case JSON_ERROR_DEPTH: |
102
|
1 |
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, maximum stack depth exceeded.'); |
103
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
104
|
1 |
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, underflow or the nodes mismatch.'); |
105
|
|
|
case JSON_ERROR_CTRL_CHAR: |
106
|
1 |
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, unexpected control character found.'); |
107
|
1 |
|
case JSON_ERROR_SYNTAX: |
108
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, syntax error - malformed JSON.'); |
109
|
|
|
case JSON_ERROR_UTF8: |
110
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)'); |
111
|
|
|
default: |
112
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode Json'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.