. */ namespace SP\Infrastructure\Security\Repositories; use SP\DataModel\EventlogData; use SP\DataModel\ItemSearchData; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Security\Ports\EventlogRepositoryInterface; use SP\Infrastructure\Common\Repositories\BaseRepository; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; /** * Class EventlogRepository * * @package SP\Infrastructure\Security\Repositories */ final class EventlogBaseRepository extends BaseRepository implements EventlogRepositoryInterface { /** * Clears the event log * * @return bool con el resultado * @throws QueryException * @throws ConstraintException */ public function clear(): bool { $queryData = new QueryData(); $queryData->setQuery('TRUNCATE TABLE EventLog'); $queryData->setOnErrorMessage(__u('Error while clearing the event log out')); return $this->db->doQuery($queryData)->getAffectedNumRows() > 0; } /** * Searches for items by a given filter * * @param ItemSearchData $itemSearchData * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function search(ItemSearchData $itemSearchData): QueryResult { $queryData = new QueryData(); $queryData->setSelect('id, FROM_UNIXTIME(date) AS date, action, level, login, ipAddress, description'); $queryData->setFrom('EventLog'); $queryData->setOrder('id DESC'); if (!empty($itemSearchData->getSeachString())) { $queryData->setWhere('action LIKE ? OR login LIKE ? OR ipAddress LIKE ? OR description LIKE ?'); $search = '%'.$itemSearchData->getSeachString().'%'; $queryData->setParams(array_fill(0, 4, $search)); } $queryData->setLimit( '?,?', [$itemSearchData->getLimitStart(), $itemSearchData->getLimitCount()] ); return $this->db->doSelect($queryData, true); } /** * @param EventlogData $eventlogData * * @return int * @throws ConstraintException * @throws QueryException */ public function create(EventlogData $eventlogData): int { $sql = 'INSERT INTO EventLog SET `date` = UNIX_TIMESTAMP(), login = ?, userId = ?, ipAddress = ?, `action` = ?, description = ?, `level` = ?'; $queryData = new QueryData(); $queryData->setQuery($sql); $queryData->setParams([ $eventlogData->getLogin(), $eventlogData->getUserId(), $eventlogData->getIpAddress(), $eventlogData->getAction(), $eventlogData->getDescription(), $eventlogData->getLevel(), ] ); return $this->db->doQuery($queryData)->getLastId(); } }