EventTemplateConverter.php in MongoDB 8.2
File
modules/mongodb_watchdog/src/EventTemplateConverter.php
View source
<?php
declare (strict_types=1);
namespace Drupal\mongodb_watchdog;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Route;
class EventTemplateConverter implements ParamConverterInterface {
const PARAM_TYPE = 'mongodb_watchdog_event_template';
protected $logger;
protected $watchdog;
public function __construct(LoggerInterface $logger, Logger $watchdog) {
$this->logger = $logger;
$this->watchdog = $watchdog;
}
public function convert($value, $definition, $name, array $defaults) : ?EventTemplate {
if (!is_string($value)) {
$this->logger
->notice('Non-string event template id: %id', [
'%id' => var_export($value, TRUE),
]);
return NULL;
}
$selector = [
'_id' => $value,
];
$options = [
'typeMap' => [
'array' => 'array',
'document' => 'array',
'root' => 'Drupal\\mongodb_watchdog\\EventTemplate',
],
];
$template = $this->watchdog
->templateCollection()
->findOne($selector, $options);
if (empty($template)) {
$this->logger
->notice('Invalid event template id: %id', [
'%id' => $value,
]);
}
assert(is_null($template) || $template instanceof EventTemplate);
return $template;
}
public function applies($definition, $name, Route $route) : bool {
return $definition['type'] === static::PARAM_TYPE;
}
}