refactor: remove duplicate code in write()

This commit is contained in:
kenjis 2022-02-15 14:06:17 +09:00
parent 43f2453472
commit 9cf3b5e452
No known key found for this signature in database
GPG Key ID: BD254878922AF198
2 changed files with 13 additions and 52 deletions

View File

@ -69,58 +69,11 @@ class PostgreHandler extends DatabaseHandler
}
/**
* Writes the session data to the session storage.
*
* @param string $id The session ID
* @param string $data The encoded session data
* Prepare data to insert/update
*/
public function write($id, $data): bool
protected function prepareData(string $data): string
{
if ($this->lock === false) {
return $this->fail();
}
if ($this->sessionID !== $id) {
$this->rowExists = false;
$this->sessionID = $id;
}
if ($this->rowExists === false) {
$insertData = [
'id' => $id,
'ip_address' => $this->ipAddress,
'data' => '\x' . bin2hex($data),
];
if (! $this->db->table($this->table)->set('timestamp', 'now()', false)->insert($insertData)) {
return $this->fail();
}
$this->fingerprint = md5($data);
$this->rowExists = true;
return true;
}
$builder = $this->db->table($this->table)->where('id', $id);
if ($this->matchIP) {
$builder = $builder->where('ip_address', $this->ipAddress);
}
$updateData = [];
if ($this->fingerprint !== md5($data)) {
$updateData['data'] = '\x' . bin2hex($data);
}
if (! $builder->set('timestamp', 'now()', false)->update($updateData)) {
return $this->fail();
}
$this->fingerprint = md5($data);
return true;
return '\x' . bin2hex($data);
}
/**

View File

@ -163,7 +163,7 @@ class DatabaseHandler extends BaseHandler
$insertData = [
'id' => $id,
'ip_address' => $this->ipAddress,
'data' => $data,
'data' => $this->prepareData($data),
];
if (! $this->db->table($this->table)->set('timestamp', 'now()', false)->insert($insertData)) {
@ -185,7 +185,7 @@ class DatabaseHandler extends BaseHandler
$updateData = [];
if ($this->fingerprint !== md5($data)) {
$updateData['data'] = $data;
$updateData['data'] = $this->prepareData($data);
}
if (! $builder->set('timestamp', 'now()', false)->update($updateData)) {
@ -197,6 +197,14 @@ class DatabaseHandler extends BaseHandler
return true;
}
/**
* Prepare data to insert/update
*/
protected function prepareData(string $data): string
{
return $data;
}
/**
* Closes the current session.
*/