Skip to content

Commit

Permalink
Refactor tableMapFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
JASchilz committed Nov 23, 2015
1 parent c40cad7 commit 4c08ad1
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/EncryptionBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,26 @@ public function allowMultiple() {

public function tableMapFilter(&$script) {
$table = $this->getTable();
$aggregateColumn = $table->getColumn($this->getParameter('column_name'));
$columnPhpName = $aggregateColumn->getPhpName();

$encryptedColumnsDeclarationLocation = strpos($script, "ENCRYPTED_COLUMNS");

// If there is not yet an encrypted column declared in this map...
if ($encryptedColumnsDeclarationLocation === False) {
foreach ($this->getEncryptedColumnNames() as $columnName) {
$column = $table->getColumn($columnName);
$columnPhpName = $column->getPhpName();

// Insert after the CLASS_NAME declaration
$insertLocation = strpos($script, ";", strpos($script, "const CLASS_NAME")) + 1;
$encryptedColumnsDeclarationLocation = strpos($script, "ENCRYPTED_COLUMNS");

$insertContent = <<<EOT
if ($encryptedColumnsDeclarationLocation === False) {
// If there is not yet an encrypted column declared in this map...

// Insert after the CLASS_NAME declaration
$insertLocation = strpos($script, ";", strpos($script, "const CLASS_NAME")) + 1;
$insertContent = $this->makeEncryptedColumnsDeclaration($columnPhpName);

/**
* Those columns encrypted by UWDOEM/Encryption
*/
const ENCRYPTED_COLUMNS = '$columnPhpName';
EOT;

} else {
// If there is already an encrypted column declared in this map...
$insertLocation = strpos($script, "'", $encryptedColumnsDeclarationLocation) + 1;
$insertContent = "$columnPhpName ";
}
$script = substr_replace($script, $insertContent, $insertLocation, 0);
// If there is already an encrypted column declared in this map...
} else {
$insertLocation = strpos($script, "'", $encryptedColumnsDeclarationLocation) + 1;
$script = substr_replace($script, "$columnPhpName ", $insertLocation, 0);
}

}
Expand Down Expand Up @@ -76,6 +71,21 @@ public function objectFilter(&$script) {
$script = substr_replace($script, $this->decryptVariable($variableName), $insertionStart, $insertionLength);
}

protected function getEncryptedColumnNames() {
return [$this->getParameter('column_name')];
}

protected function makeEncryptedColumnsDeclaration($columnPhpName) {
return <<<EOT
/**
* Those columns encrypted by UWDOEM/Encryption
*/
const ENCRYPTED_COLUMNS = '$columnPhpName';
EOT;
}

protected function encryptVariable($variableName) {
return <<<EOT
Expand Down

0 comments on commit 4c08ad1

Please sign in to comment.