1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carnage\EncryptedColumn; |
4
|
|
|
|
5
|
|
|
use Carnage\EncryptedColumn\Container\KeyContainer; |
6
|
|
|
use Carnage\EncryptedColumn\Container\VersionedContainer; |
7
|
|
|
use Carnage\EncryptedColumn\Dbal\EncryptedColumn; |
8
|
|
|
use Carnage\EncryptedColumn\Dbal\EncryptedColumnLegacySupport; |
9
|
|
|
use Carnage\EncryptedColumn\Encryptor\HaliteEncryptor; |
10
|
|
|
use Carnage\EncryptedColumn\Encryptor\LegacyEncryptor; |
11
|
|
|
use Carnage\EncryptedColumn\Serializer\LegacySerializer; |
12
|
|
|
use Carnage\EncryptedColumn\Serializer\PhpSerializer; |
13
|
|
|
use Carnage\EncryptedColumn\Service\EncryptionService; |
14
|
|
|
use Carnage\EncryptedColumn\ValueObject\Key; |
15
|
|
|
use Carnage\EncryptedColumn\ValueObject\KeyIdentity; |
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
|
18
|
|
|
final class Setup |
19
|
|
|
{ |
20
|
|
|
private $keyPath; |
21
|
1 |
|
private $enableLegacy = false; |
22
|
|
|
private $legacyKey; |
23
|
1 |
|
private $keyContainer; |
24
|
1 |
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->keyContainer = new KeyContainer(); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
1 |
|
public function register(EntityManagerInterface $em) |
31
|
|
|
{ |
32
|
1 |
|
if ($this->enableLegacy) { |
33
|
1 |
|
$this->doRegisterLegacy($em); |
34
|
1 |
|
} else { |
35
|
|
|
$this->doRegister($em); |
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
View Code Duplication |
public function enableLegacy(string $legacyKey) |
40
|
1 |
|
{ |
41
|
|
|
$this->enableLegacy = true; |
42
|
|
|
$this->legacyKey = $legacyKey; |
43
|
1 |
|
|
44
|
|
|
$key = new Key($legacyKey); |
45
|
1 |
|
$this->keyContainer->addKey($key); |
46
|
1 |
|
$this->keyContainer->tagKey('legacy', $key->getIdentifier()->toString()); |
47
|
1 |
|
|
48
|
1 |
|
return $this; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function withKeyPath(string $keypath) |
52
|
|
|
{ |
53
|
|
|
$this->keyPath = $keypath; |
54
|
|
|
|
55
|
1 |
|
$key = new Key($keypath); |
56
|
|
|
$this->keyContainer->addKey($key); |
57
|
1 |
|
$this->keyContainer->tagKey('default', $key->getIdentifier()->toString()); |
58
|
1 |
|
|
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function withKey(string $key, array $tags = []) |
63
|
|
|
{ |
64
|
|
|
$key = new Key($key); |
65
|
1 |
|
$keyId = $key->getIdentifier()->toString(); |
66
|
|
|
$this->keyContainer->addKey($key); |
67
|
1 |
|
|
68
|
1 |
|
foreach ($tags as $tag) { |
69
|
1 |
|
$this->keyContainer->tagKey($tag, $keyId); |
70
|
|
|
} |
71
|
1 |
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function buildEncryptionService(): EncryptionService |
76
|
|
|
{ |
77
|
|
|
$encryptors = self::buildEncryptorsContainer(); |
78
|
|
|
$serializers = self::buildSerilaizerContainer(); |
79
|
|
|
return new EncryptionService( |
80
|
|
|
$encryptors->get(HaliteEncryptor::IDENTITY), |
81
|
|
|
$serializers->get(PhpSerializer::IDENTITY), |
82
|
|
|
$encryptors, |
83
|
|
|
$serializers, |
84
|
|
|
$this->keyContainer |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function buildEncryptorsContainer(): VersionedContainer |
89
|
|
|
{ |
90
|
1 |
|
$services = [new HaliteEncryptor($this->keyPath)]; |
|
|
|
|
91
|
|
|
if ($this->enableLegacy) { |
92
|
1 |
|
$services[] = new LegacyEncryptor($this->legacyKey); |
|
|
|
|
93
|
1 |
|
} |
94
|
1 |
|
//@TODO add legacy encryptor, throw exceptions if required keys aren't specified |
95
|
1 |
|
return new VersionedContainer(...$services); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
private function buildSerilaizerContainer(): VersionedContainer |
99
|
|
|
{ |
100
|
|
|
$services = [new PhpSerializer()]; |
101
|
|
|
if ($this->enableLegacy) { |
102
|
|
|
$services[] = new LegacySerializer(); |
103
|
|
|
} |
104
|
|
|
return new VersionedContainer(...$services); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param EntityManagerInterface $em |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
private function doRegister(EntityManagerInterface $em) |
111
|
|
|
{ |
112
|
|
|
EncryptedColumn::create($this->buildEncryptionService()); |
113
|
|
|
$conn = $em->getConnection(); |
114
|
|
|
$conn->getDatabasePlatform()->registerDoctrineTypeMapping( |
115
|
|
|
EncryptedColumn::ENCRYPTED, |
116
|
|
|
EncryptedColumn::ENCRYPTED |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param EntityManagerInterface $em |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
private function doRegisterLegacy(EntityManagerInterface $em) |
124
|
|
|
{ |
125
|
|
|
EncryptedColumnLegacySupport::create($this->buildEncryptionService()); |
126
|
|
|
$conn = $em->getConnection(); |
127
|
|
|
$conn->getDatabasePlatform()->registerDoctrineTypeMapping( |
128
|
|
|
EncryptedColumnLegacySupport::ENCRYPTED, |
129
|
|
|
EncryptedColumnLegacySupport::ENCRYPTED |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.