1
|
|
|
<?php |
2
|
|
|
namespace Carnage\EncryptedColumn\Dbal; |
3
|
|
|
|
4
|
|
|
use Carnage\EncryptedColumn\Service\EncryptionService; |
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use Carnage\EncryptedColumn\ValueObject\EncryptedColumn as EncryptedColumnVO; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Object type for storing an encrypted column object as json in the db |
12
|
|
|
* |
13
|
|
|
* Class EncryptedColumn |
14
|
|
|
*/ |
15
|
|
|
class EncryptedColumn extends Type |
16
|
|
|
{ |
17
|
|
|
const ENCRYPTED = 'encrypted'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var EncryptionService |
21
|
|
|
*/ |
22
|
|
|
private $encryptionService; |
23
|
|
|
|
24
|
4 |
View Code Duplication |
public static function create(EncryptionService $encryptionService) |
|
|
|
|
25
|
|
|
{ |
26
|
4 |
|
Type::addType(EncryptedColumn::ENCRYPTED, EncryptedColumn::class); |
27
|
|
|
/** @var EncryptedColumn $instance */ |
28
|
4 |
|
$instance = Type::getType(EncryptedColumn::ENCRYPTED); |
29
|
4 |
|
$instance->encryptionService = $encryptionService; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
4 |
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
33
|
|
|
{ |
34
|
4 |
|
return $platform->getClobTypeDeclarationSQL($fieldDeclaration); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
38
|
|
|
{ |
39
|
4 |
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
public function getName() |
43
|
|
|
{ |
44
|
4 |
|
return self::ENCRYPTED; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
48
|
|
|
{ |
49
|
3 |
|
if ($value === null) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
$decoded = $this->decodeJson($value); |
54
|
|
|
|
55
|
3 |
|
return $this->encryptionService->decryptField(EncryptedColumnVO::fromArray($decoded)); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
View Code Duplication |
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
|
|
|
59
|
|
|
{ |
60
|
4 |
|
if ($value === null) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
return json_encode($this->encryptionService->encryptField($value)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Based on: https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/JsonDeserializationVisitor.php |
69
|
|
|
* |
70
|
|
|
* @param $value |
71
|
|
|
* @return mixed |
72
|
|
|
* @throws ConversionException |
73
|
|
|
*/ |
74
|
3 |
View Code Duplication |
private function decodeJson($value) |
|
|
|
|
75
|
|
|
{ |
76
|
3 |
|
$decoded = json_decode($value, true); |
77
|
|
|
|
78
|
3 |
|
switch (json_last_error()) { |
79
|
3 |
|
case JSON_ERROR_NONE: |
80
|
3 |
|
if (!is_array($decoded)) { |
81
|
|
|
throw ConversionException::conversionFailed($value, 'Json was not an array'); |
82
|
|
|
} |
83
|
3 |
|
return $decoded; |
84
|
|
|
case JSON_ERROR_DEPTH: |
85
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, maximum stack depth exceeded.'); |
86
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
87
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, underflow or the nodes mismatch.'); |
88
|
|
|
case JSON_ERROR_CTRL_CHAR: |
89
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, unexpected control character found.'); |
90
|
|
|
case JSON_ERROR_SYNTAX: |
91
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, syntax error - malformed JSON.'); |
92
|
|
|
case JSON_ERROR_UTF8: |
93
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)'); |
94
|
|
|
default: |
95
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode Json'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.