Updated models for encryption.

This commit is contained in:
James Cole
2015-05-23 08:46:46 +02:00
parent 1c2cbd5b40
commit f05002c729
10 changed files with 221 additions and 25 deletions

View File

@@ -62,6 +62,25 @@ class PiggyBank extends Model
return ['created_at', 'updated_at', 'deleted_at', 'startdate', 'targetdate'];
}
/**
* @codeCoverageIgnore
*
* @param $value
*
* @return string
*/
public function getNameAttribute($value)
{
if (intval($this->encrypted) == 1) {
return Crypt::decrypt($value);
}
// @codeCoverageIgnoreStart
return $value;
// @codeCoverageIgnoreEnd
}
/**
* @codeCoverageIgnore
*
@@ -74,6 +93,22 @@ class PiggyBank extends Model
return intval($value) == 1;
}
/**
* @param $value
*
* @return float|int
*/
public function getTargetamountAttribute($value)
{
if (is_null($this->targetamount_encrypted)) {
return $value;
}
$value = intval(Crypt::decrypt($this->targetamount_encrypted));
$value = $value / 100;
return $value;
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\HasMany
@@ -104,21 +139,13 @@ class PiggyBank extends Model
}
/**
* @codeCoverageIgnore
*
* @param $value
*
* @return string
*/
public function getNameAttribute($value)
public function setTargetamountAttribute($value)
{
if (intval($this->encrypted) == 1) {
return Crypt::decrypt($value);
}
// @codeCoverageIgnoreStart
return $value;
// @codeCoverageIgnoreEnd
// save in cents:
$value = intval($value * 100);
$this->attributes['targetamount_encrypted'] = Crypt::encrypt($value);
$this->attributes['targetamount'] = ($value / 100);
}
}