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 ac5260a commit bb02983
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/EncryptionBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,15 @@ public function tableMapFilter(&$script) {

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

// Modify the setter to include encryption
$setterLocation = strpos($script, "set$columnPhpName");

$start = strpos($script, "(", $setterLocation) + 1;
$length = strpos($script, ")", $setterLocation) - $start;
$variableName = substr($script, $start, $length);

$insertionStart = strpos($script, "{", $setterLocation) + 1;
$script = substr_replace($script, $this->encryptVariable($variableName), $insertionStart, 0);

// Modify the getter to include decryption
$getterLocation = strpos($script, "get$columnPhpName");
foreach ($this->getEncryptedColumnNames() as $columnName) {
$aggregateColumn = $table->getColumn($columnName);
$columnPhpName = $aggregateColumn->getPhpName();

$start = strpos($script, "return", $getterLocation) + 7;
$length = strpos($script, ";", $getterLocation) - $start;
$variableName = substr($script, $start, $length);
$this->modifySetterWithEncryption($script, $columnPhpName);
$this->modifyGetterWithDecryption($script, $columnPhpName);

$insertionStart = strpos($script, "return", $getterLocation);
$insertionLength = strpos($script, ";", $insertionStart) - $insertionStart + 1;
$script = substr_replace($script, $this->decryptVariable($variableName), $insertionStart, $insertionLength);
}
}

protected function getEncryptedColumnNames() {
Expand All @@ -86,6 +72,17 @@ protected function makeEncryptedColumnsDeclaration($columnPhpName) {
EOT;
}

protected function modifySetterWithEncryption(&$script, $columnPhpName) {
$setterLocation = strpos($script, "set$columnPhpName");

$start = strpos($script, "(", $setterLocation) + 1;
$length = strpos($script, ")", $setterLocation) - $start;
$variableName = substr($script, $start, $length);

$insertionStart = strpos($script, "{", $setterLocation) + 1;
$script = substr_replace($script, $this->encryptVariable($variableName), $insertionStart, 0);
}

protected function encryptVariable($variableName) {
return <<<EOT
Expand All @@ -95,6 +92,18 @@ protected function encryptVariable($variableName) {
EOT;
}

protected function modifyGetterWithDecryption(&$script, $columnPhpName) {
$getterLocation = strpos($script, "get$columnPhpName");

$start = strpos($script, "return", $getterLocation) + 7;
$length = strpos($script, ";", $getterLocation) - $start;
$variableName = substr($script, $start, $length);

$insertionStart = strpos($script, "return", $getterLocation);
$insertionLength = strpos($script, ";", $insertionStart) - $insertionStart + 1;
$script = substr_replace($script, $this->decryptVariable($variableName), $insertionStart, $insertionLength);
}

protected function decryptVariable($variableName) {
return <<<EOT
// Decrypt the variable, per \UWDOEM\Encryption\EncryptionBehavior.
Expand Down

0 comments on commit bb02983

Please sign in to comment.