EventController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\mongodb_watchdog;
6
7
use Drupal\Core\Config\ConfigFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Config\ConfigFactoryInterface 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 Drupal\Core\Datetime\DateFormatterInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Datetime\DateFormatterInterface 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 Drupal\Core\Link;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Link 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
use Drupal\Core\StringTranslation\StringTranslationTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\StringTransl...\StringTranslationTrait 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...
11
use Drupal\Core\Url;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Url 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...
12
use Drupal\user\Entity\User;
0 ignored issues
show
Bug introduced by
The type Drupal\user\Entity\User 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...
13
use MongoDB\Driver\Cursor;
14
15
/**
16
 * Class EventController provides query and render logic for Event occurrences.
17
 *
18
 * It is not a page/Response controller, hence its location outside the
19
 * Controller namespace.
20
 */
21
class EventController {
22
  use StringTranslationTrait;
23
24
  /**
25
   * The name of the anonymous user account.
26
   *
27
   * @var string
28
   */
29
  protected $anonymous;
30
31
  /**
32
   * The length of the absolute home URL.
33
   *
34
   * @var int
35
   */
36
  protected $baseLength;
37
38
  /**
39
   * The date.formatter service.
40
   *
41
   * @var \Drupal\Core\Datetime\DateFormatterInterface
42
   */
43
  protected $dateFormatter;
44
45
  /**
46
   * The absolute path to the site home.
47
   *
48
   * @var string
49
   */
50
  protected $front;
51
52
  /**
53
   * An instance cache for user accounts, which are used in a loop.
54
   *
55
   * @var array<int,\Drupal\Core\Link|string>
56
   */
57
  protected $userCache = [];
58
59
  /**
60
   * The MongoDB logger service, to load events.
61
   *
62
   * @var \Drupal\mongodb_watchdog\Logger
63
   */
64
  protected $watchdog;
65
66
  /**
67
   * EventController constructor.
68
   *
69
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
70
   *   The config.factory service.
71
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
72
   *   The core data.formatter service.
73
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
74
   *   The MongoDB logger service, to load events.
75
   */
76
  public function __construct(
77
    ConfigFactoryInterface $config,
78
    DateFormatterInterface $dateFormatter,
79
    Logger $watchdog) {
80
    // Needed for other values so build it first.
81
    $this->front = Url::fromRoute('<front>', [], ['absolute' => TRUE])
82
      ->toString();
83
84
    $this->anonymous = $config->get('user.settings')->get('anonymous');
85
    $this->baseLength = mb_strlen($this->front) - 1;
86
    $this->dateFormatter = $dateFormatter;
87
    $this->watchdog = $watchdog;
88
  }
89
90
  /**
91
   * Provide a table row representation of an event occurrence.
92
   *
93
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
94
   *   The template for which the occurrence exists.
95
   * @param \Drupal\mongodb_watchdog\Event $event
96
   *   The event occurrence to represent.
97
   *
98
   * @return array<int,\Drupal\Core\Link|\Drupal\Core\StringTranslation\TranslatableMarkup|string|null>
99
   *   A render array for a table row.
100
   *
101
   * @throws \Drupal\Core\Entity\EntityMalformedException
102
   */
103
  public function asTableRow(EventTemplate $template, Event $event): array {
104
    $uid = $event->uid();
105
    if (!isset($this->userCache[$uid])) {
106
      $this->userCache[$uid] = $uid ? User::load($uid)->toLink() : $this->anonymous;
107
    }
108
109
    $location = $event->location();
110
    $ret = [
111
      $this->dateFormatter->format($event->timestamp, 'short'),
112
      $this->userCache[$uid],
113
      $template->asString($event->variables),
0 ignored issues
show
Bug introduced by
It seems like $event->variables can also be of type null; however, parameter $variables of Drupal\mongodb_watchdog\EventTemplate::asString() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
      $template->asString(/** @scrutinizer ignore-type */ $event->variables),
Loading history...
114
      // Locations generated from Drush will not necessarily match the
115
      // site home URL, and will not therefore not necessarily be reachable, so
116
      // we only generate a link if the location is "within" the site.
117
      (mb_strpos($location, $this->front) === 0)
118
        ? Link::fromTextAndUrl(mb_substr($location, $this->baseLength), Url::fromUri($location))
119
        : $location,
120
      empty($event->referrer) ? '' : Link::fromTextAndUrl($event->referrer, Url::fromUri($event->referrer)),
121
      $event->hostname,
122
      (isset($event->requestTracking_id) && $event->requestTracking_id !== Logger::INVALID_REQUEST)
123
        ? Link::createFromRoute($this->t('Request'),
124
          'mongodb_watchdog.reports.request',
125
          ['uniqueId' => $event->requestTracking_id])
126
        : '',
127
    ];
128
129
    return $ret;
130
  }
131
132
  /**
133
   * Load MongoDB watchdog events for a given event template.
134
   *
135
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
136
   *   The template for which to find events.
137
   * @param int $skip
138
   *   The number of events to skip.
139
   * @param int $limit
140
   *   The limit on the number of events to return.
141
   *
142
   * @return \MongoDB\Driver\Cursor
143
   *   A cursor to the event occurrences.
144
   */
145
  public function find(EventTemplate $template, int $skip, int $limit): Cursor {
146
    $collection = $this->watchdog->eventCollection($template->_id);
147
    $selector = [];
148
    $options = [
149
      'skip' => $skip,
150
      'limit' => $limit,
151
      'sort' => ['$natural' => -1],
152
      'typeMap' => [
153
        'array' => 'array',
154
        'document' => 'array',
155
        'root' => 'Drupal\mongodb_watchdog\Event',
156
      ],
157
    ];
158
159
    $result = $collection->find($selector, $options);
160
    return $result;
161
  }
162
163
}
164