Completed
Pull Request — 8.x-2.x (#11)
by Frédéric G.
05:10 queued 02:13
created

EventTemplateConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 66
rs 10
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B convert() 0 24 3
A applies() 0 3 1
1
<?php
2
3
namespace Drupal\mongodb_watchdog;
4
5
use Drupal\Core\ParamConverter\ParamConverterInterface;
6
use Psr\Log\LoggerInterface;
7
use Symfony\Component\Routing\Route;
8
9
/**
10
 * Class EventTemplateConverter load MongoDB watchdog event templates by id.
11
 */
12
class EventTemplateConverter implements ParamConverterInterface {
13
  const PARAM_TYPE = 'mongodb_watchdog_event_template';
14
15
  /**
16
   * The core logger channel service.
17
   *
18
   * @var \Psr\Log\LoggerInterface
19
   */
20
  protected $logger;
21
22
  /**
23
   * The MongoDB logger service, to load events.
24
   *
25
   * @var \Drupal\mongodb_watchdog\Logger
26
   */
27
  protected $watchdog;
28
29
  /**
30
   * EventTemplateConverter constructor.
31
   *
32
   * @param \Psr\Log\LoggerInterface $logger
33
   *   The logger service, to log intervening events.
34
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
35
   *   The MongoDB logger, to load event templates.
36
   */
37
  public function __construct(LoggerInterface $logger, Logger $watchdog) {
38
    $this->logger = $logger;
39
    $this->watchdog = $watchdog;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public function convert($value, $definition, $name, array $defaults) {
46
    if (!is_string($value)) {
47
      $this->logger->notice('Non-string event template id: %id', ['%id' => var_export($value, TRUE)]);
48
      return NULL;
49
    }
50
51
    $selector = [
52
      '_id' => $value,
53
    ];
54
    $options = [
55
      'typeMap' => [
56
        'array' => 'array',
57
        'document' => 'array',
58
        'root' => 'Drupal\mongodb_watchdog\EventTemplate',
59
      ],
60
    ];
61
62
    // Returns null if there is no match, as expected by ParamConverter.
63
    $template = $this->watchdog->templateCollection()->findOne($selector, $options);
64
    if (empty($template)) {
65
      $this->logger->debug('Invalid event template id: %id', ['%id' => $value]);
66
    }
67
    return $template;
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function applies($definition, $name, Route $route) {
74
    return $definition['type'] === static::PARAM_TYPE;
75
  }
76
77
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
78