Skip to content

Commit

Permalink
Split adding session data and sending a log
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Oct 24, 2023
1 parent 7997e58 commit 11c7f30
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions api/src/Logger/SessionDataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,43 @@ public function __construct(
$this->entityManager = $entityManager;
}

public function __invoke(array $record): array
/**
* Update the context with data from the session and the request stack.
*
* @param array $context The context to update.
*
* @return array The updated context.
*/
public function updateContext($context): array
{
$context['session'] = $this->session->getId();
$context['process'] = $this->session->has('process') ? $this->session->get('process') : '';
$context['endpoint'] = $this->session->has('endpoint') ? $this->session->get('endpoint') : '';
$context['schema'] = $this->session->has('schema') ? $this->session->get('schema') : '';
$context['object'] = $this->session->has('object') === true ? $this->session->get('object') : '';
$context['cronjob'] = $this->session->has('cronjob') ? $this->session->get('cronjob') : '';
$context['action'] = $this->session->has('cronjob') ? $this->session->get('action') : '';
$context['mapping'] = $this->session->has('mapping') ? $this->session->get('mapping') : '';
$context['source'] = $this->session->has('source') ? $this->session->get('source') : '';
$context['plugin'] = isset($record['data']['plugin']) === true ? $record['data']['plugin'] : '';
$context['user'] = $this->session->has('user') ? $this->session->get('user') : '';
$context['organization'] = $this->session->has('organization') ? $this->session->get('organization') : '';
$context['application'] = $this->session->has('application') ? $this->session->get('application') : '';
$context['host'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getHost() : '';
$context['ip'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getClientIp() : '';

$record['context']['session'] = $this->session->getId();
$record['context']['process'] = $this->session->has('process') ? $this->session->get('process') : '';
$record['context']['endpoint'] = $this->session->has('endpoint') ? $this->session->get('endpoint') : '';
$record['context']['schema'] = $this->session->has('schema') ? $this->session->get('schema') : '';
$record['context']['object'] = $this->session->has('object') === true ? $this->session->get('object') : '';
$record['context']['cronjob'] = $this->session->has('cronjob') ? $this->session->get('cronjob') : '';
$record['context']['action'] = $this->session->has('cronjob') ? $this->session->get('action') : '';
$record['context']['mapping'] = $this->session->has('mapping') ? $this->session->get('mapping') : '';
$record['context']['source'] = $this->session->has('source') ? $this->session->get('source') : '';
$record['context']['plugin'] = isset($record['data']['plugin']) === true ? $record['data']['plugin'] : '';
$record['context']['user'] = $this->session->has('user') ? $this->session->get('user') : '';
$record['context']['organization'] = $this->session->has('organization') ? $this->session->get('organization') : '';
$record['context']['application'] = $this->session->has('application') ? $this->session->get('application') : '';
$record['context']['host'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getHost() : '';
$record['context']['ip'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getClientIp() : '';

return $context;
}

/**
* Dispatches a log create action.
*
* @param array $record The log record that is created.
*
* @return array The resulting log record after the action.
*/
public function dispatchLogCreateAction(array $record): array
{
if ($this->entityManager->getConnection()->isConnected() === true
&& in_array(
$this->entityManager->getConnection()->getDatabase(),
Expand All @@ -74,4 +91,18 @@ public function __invoke(array $record): array

return $record;
}

/**
* Updates the log record with data from the session, request and from actions.
*
* @param array $record The log record.
*
* @return array The updated log record.
*/
public function __invoke(array $record): array
{
$record['context'] = $this->updateContext($record['context']);

return $this->dispatchLogCreateAction($record);
}
}

0 comments on commit 11c7f30

Please sign in to comment.