Skip to content

Commit

Permalink
Merge pull request #4 from christyjacob4/master
Browse files Browse the repository at this point in the history
Change the way we check query execution status
  • Loading branch information
eldadfux authored Dec 21, 2020
2 parents 5000fdb + 78bd6bc commit 154a850
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Audit/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ abstract public function getLogsByResource(string $resource): array;
abstract public function getLogsByUserAndActions(string $userId, array $actions): array;

/**
* Delete all logs older than $seconds seconds
* Delete all logs older than $timestamp seconds
*
* @param int $seconds
* @param int $timestamp
*
* @return bool
*/
abstract public function cleanup(int $seconds): bool;
abstract public function cleanup(int $timestamp): bool;
}
18 changes: 9 additions & 9 deletions src/Audit/Adapters/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function log(string $userId, string $event, string $resource, string $use
$st->bindValue(':location', $location, PDO::PARAM_STR);
$st->bindValue(':data', $data, PDO::PARAM_STR);

$st->execute();
$response = $st->execute();

return ('00000' == $st->errorCode()) ? true : false;
return $response == true ;
}

public function getLogsByUser(string $userId):array
Expand Down Expand Up @@ -117,22 +117,22 @@ public function getLogsByUserAndActions(string $userId, array $actions):array
}

/**
* Delete logs older than $seconds seconds
* Delete logs older than $timestamp seconds
*
* @param int $seconds
* @param int $timestamp
*
* @return bool
*/
public function cleanup(int $seconds):bool
public function cleanup(int $timestamp):bool
{
$st = $this->getPDO()->prepare('DELETE
FROM `'.$this->getNamespace().'.audit.audit`
WHERE (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`time`)) > :seconds');
WHERE UNIX_TIMESTAMP(`time`) < :timestamp');

$st->bindValue(':seconds', $seconds, PDO::PARAM_INT);
$st->execute();
$st->bindValue(':timestamp', $timestamp, PDO::PARAM_INT);
$response = $st->execute();

return ('00000' == $st->errorCode()) ? true : false;
return $response == true;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Audit/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ public function getLogsByUserAndActions(string $userId, array $actions): array
}

/**
* Delete all logs older than $seconds seconds
* Delete all logs older than $timestamp seconds
*
* @param int $seconds
* @param int $timestamp
*
* @return bool
*/
public function cleanup(int $seconds): bool
public function cleanup(int $timestamp): bool
{
return $this->adapter->cleanup($seconds);
return $this->adapter->cleanup($timestamp);
}
}
4 changes: 2 additions & 2 deletions tests/Audit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testGetLogsByResource()
public function testCleanup() {
sleep(3);
// First delete all the logs
$status = $this->audit->cleanup(1);
$status = $this->audit->cleanup(time());
$this->assertEquals($status, true);

// Check that all logs have been deleted
Expand All @@ -118,7 +118,7 @@ public function testCleanup() {
sleep(5);

// DELETE logs older than 10 seconds and check that status is true
$status = $this->audit->cleanup(10);
$status = $this->audit->cleanup(time()-10);
$this->assertEquals($status, true);

// Check if 1 log has been deleted
Expand Down

0 comments on commit 154a850

Please sign in to comment.