EventTemplateConverter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
c 1
b 0
f 1
dl 0
loc 76
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A convert() 0 27 4
A applies() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb_watchdog;
6
7
use Drupal\Core\ParamConverter\ParamConverterInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\ParamConverter\ParamConverterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\Routing\Route;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Routing\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
/**
12
 * Class EventTemplateConverter load MongoDB watchdog event templates by id.
13
 */
14
class EventTemplateConverter implements ParamConverterInterface {
15
  const PARAM_TYPE = 'mongodb_watchdog_event_template';
16
17
  /**
18
   * The core logger channel service.
19
   *
20
   * @var \Psr\Log\LoggerInterface
21
   */
22
  protected $logger;
23
24
  /**
25
   * The MongoDB logger service, to load events.
26
   *
27
   * @var \Drupal\mongodb_watchdog\Logger
28
   */
29
  protected $watchdog;
30
31
  /**
32
   * EventTemplateConverter constructor.
33
   *
34
   * @param \Psr\Log\LoggerInterface $logger
35
   *   The logger service, to log intervening events.
36
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
37
   *   The MongoDB logger, to load event templates.
38
   */
39
  public function __construct(LoggerInterface $logger, Logger $watchdog) {
40
    $this->logger = $logger;
41
    $this->watchdog = $watchdog;
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   *
47
   * @param mixed $value
48
   *   The value to convert.
49
   * @param mixed $definition
50
   *   The parameter definition. Not used.
51
   * @param string $name
52
   *   The parameter name.
53
   * @param array<string,mixed> $defaults
54
   *   The route defaults array.
55
   */
56
  public function convert(mixed $value, $definition, $name, array $defaults): ?EventTemplate {
57
    if (!is_string($value)) {
58
      $this->logger->notice('Non-string event template id: %id', [
59
        '%id' => var_export($value, TRUE),
60
      ]);
61
      return NULL;
62
    }
63
64
    $selector = [
65
      '_id' => $value,
66
    ];
67
    $options = [
68
      'typeMap' => [
69
        'array' => 'array',
70
        'document' => 'array',
71
        'root' => 'Drupal\mongodb_watchdog\EventTemplate',
72
      ],
73
    ];
74
75
    // Returns null if there is no match, as expected by ParamConverter.
76
    // Never returns an array as findOne() could, because of $options.
77
    $template = $this->watchdog->templateCollection()->findOne($selector, $options);
78
    if (empty($template)) {
79
      $this->logger->notice('Invalid event template id: %id', ['%id' => $value]);
80
    }
81
    assert(is_null($template) || $template instanceof EventTemplate);
82
    return $template;
83
  }
84
85
  /**
86
   * {@inheritdoc}
87
   */
88
  public function applies($definition, $name, Route $route): bool {
89
    return $definition['type'] === static::PARAM_TYPE;
90
  }
91
92
}
93