Skip to content

Commit

Permalink
Some codacy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoLouwerse committed Nov 23, 2023
1 parent de9d304 commit e92115c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
33 changes: 26 additions & 7 deletions api/migrations/Version20231123112218.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,49 @@
*/
final class Version20231123112218 extends AbstractMigration
{


/**
* Description.
*
* @return string Description.
*/
public function getDescription(): string
{
return '';
}
return 'Changed Gateway->logging boolean to Gateway->configLogging array';

}//end getDescription()


/**
* Migrate up.
*
* @param Schema $schema Schema.
* @return void
*/
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE gateway ALTER logging TYPE TEXT');
$this->addSql('ALTER TABLE gateway ALTER logging SET DEFAULT \'a:10:{s:10:"callMethod";b:1;s:7:"callUrl";b:1;s:9:"callQuery";b:1;s:15:"callContentType";b:1;s:8:"callBody";b:1;s:18:"responseStatusCode";b:1;s:19:"responseContentType";b:1;s:12:"responseBody";b:1;s:16:"maxCharCountBody";i:500;s:21:"maxCharCountErrorBody";i:2000;}\'');
$this->addSql('UPDATE gateway SET logging = \'a:10:{s:10:"callMethod";b:1;s:7:"callUrl";b:1;s:9:"callQuery";b:1;s:15:"callContentType";b:1;s:8:"callBody";b:1;s:18:"responseStatusCode";b:1;s:19:"responseContentType";b:1;s:12:"responseBody";b:1;s:16:"maxCharCountBody";i:500;s:21:"maxCharCountErrorBody";i:2000;}\'');
$this->addSql('ALTER TABLE gateway ALTER logging SET NOT NULL');
$this->addSql('COMMENT ON COLUMN gateway.logging IS \'(DC2Type:array)\'');
$this->addSql('ALTER TABLE gateway RENAME COLUMN logging TO logging_config');
}
}//end up()

/**
* Migrate down.
*
* @param Schema $schema Schema.
* @return void
*/
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE gateway RENAME COLUMN logging_config TO logging');
$this->addSql('ALTER TABLE gateway ALTER logging DROP NOT NULL');
$this->addSql('ALTER TABLE gateway ALTER logging DROP DEFAULT');
$this->addSql('UPDATE gateway SET logging = NULL');
$this->addSql('ALTER TABLE gateway ALTER logging TYPE BOOLEAN USING logging::boolean');
$this->addSql('COMMENT ON COLUMN gateway.logging IS NULL');
}
}
}//end down()
}//end class
32 changes: 16 additions & 16 deletions api/src/Logger/SessionDataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ public function updateContext(array $record): array
$context['mapping'] = $this->session->has('mapping') === true ? $this->session->get('mapping') : '';
$context['source'] = $this->session->has('source') === true ? $this->session->get('source') : '';

// Add more to context if we are dealing with a log containing sourceCall data
if (isset($context['sourceCall'])) {
// Add more to context if we are dealing with a log containing sourceCall data.
if (isset($context['sourceCall']) === true) {
$context = $this->updateSourceCallContext($context, $record['level_name']);
}

$context['user'] = $this->session->has('user') === true ? $this->session->get('user') : '';
$context['organization'] = $this->session->has('organization') === true ? $this->session->get('organization') : '';
$context['application'] = $this->session->has('application') === true ? $this->session->get('application') : '';
$context['host'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getHost() : '';
$context['ip'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getClientIp() : '';
$context['method'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getMethod() : '';
$context['host'] = $this->requestStack->getMainRequest() !== null ? $this->requestStack->getMainRequest()->getHost() : '';
$context['ip'] = $this->requestStack->getMainRequest() !== null ? $this->requestStack->getMainRequest()->getClientIp() : '';
$context['method'] = $this->requestStack->getMainRequest() !== null ? $this->requestStack->getMainRequest()->getMethod() : '';

// Add more to context for higher level logs
// Add more to context for higher level logs.
if (in_array($record['level_name'], ['ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY']) === true) {
$context = $this->addErrorContext($context, $record['level_name']);
}
Expand All @@ -87,18 +87,18 @@ public function updateContext(array $record): array
/**
* Update the context for Source call logs.
*
* @param array $context The log context we are updating.
* @param array $context The log context we are updating.
* @param string $levelName The level name of the log record we are updating the context for.
*
* @return array The updated context.
*/
private function updateSourceCallContext(array $context, string $levelName): array
{
if (in_array($levelName, ['ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY']) === true) {
$maxStrLength = $context['sourceCall']['maxCharCountErrorBody'] ?? 2000;
$maxStrLength = ($context['sourceCall']['maxCharCountErrorBody'] ?? 2000);
unset($context['sourceCall']['maxCharCountBody']);
} else {
$maxStrLength = $context['sourceCall']['maxCharCountBody'] ?? 500;
$maxStrLength = ($context['sourceCall']['maxCharCountBody'] ?? 500);
unset($context['sourceCall']['maxCharCountErrorBody']);
}

Expand All @@ -110,11 +110,11 @@ private function updateSourceCallContext(array $context, string $levelName): arr
}

if (isset($context['sourceCall']['callBody']) === true && strlen($context['sourceCall']['callBody']) > $maxStrLength) {
$context['sourceCall']['callBody'] = substr($context['sourceCall']['callBody'], 0, $maxStrLength) . '...';
$context['sourceCall']['callBody'] = substr($context['sourceCall']['callBody'], 0, $maxStrLength).'...';
}

if (isset($context['sourceCall']['responseBody']) === true && strlen($context['sourceCall']['responseBody']) > $maxStrLength) {
$context['sourceCall']['responseBody'] = substr($context['sourceCall']['responseBody'], 0, $maxStrLength) . '...';
$context['sourceCall']['responseBody'] = substr($context['sourceCall']['responseBody'], 0, $maxStrLength).'...';
}

return $context;
Expand All @@ -131,13 +131,13 @@ private function updateSourceCallContext(array $context, string $levelName): arr
*/
private function addErrorContext(array $context, string $levelName): array
{
$context['pathRaw'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getPathInfo() : '';
$context['querystring'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getQueryString() : '';
$context['pathRaw'] = $this->requestStack->getMainRequest() !== null ? $this->requestStack->getMainRequest()->getPathInfo() : '';
$context['querystring'] = $this->requestStack->getMainRequest() !== null ? $this->requestStack->getMainRequest()->getQueryString() : '';
$context['mongoDBFilter'] = $this->session->has('mongoDBFilter') === true ? json_encode($this->session->get('mongoDBFilter')) : '';
$context['contentType'] = $this->requestStack->getMainRequest() ? $this->requestStack->getMainRequest()->getContentType() : '';
$context['contentType'] = $this->requestStack->getMainRequest() !== null ? $this->requestStack->getMainRequest()->getContentType() : '';

// Do not log entire body for normal errors, only critical and higher
if ($this->requestStack->getMainRequest() && $levelName !== 'ERROR') {
// Do not log entire body for normal errors, only critical and higher.
if ($this->requestStack->getMainRequest() !== null && $levelName !== 'ERROR') {
try {
$context['body'] = $this->requestStack->getMainRequest()->toArray();
} catch (Exception $exception) {
Expand Down

0 comments on commit e92115c

Please sign in to comment.