Completed
Push — 2761013-watchdog_pager ( 2d12f8 )
by Frédéric G.
02:46
created

EventController::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace Drupal\mongodb_watchdog;
4
5
use Drupal\Component\Utility\Unicode;
6
use Drupal\Core\Config\ConfigFactoryInterface;
7
use Drupal\Core\Datetime\DateFormatterInterface;
8
use Drupal\Core\Link;
9
use Drupal\Core\Url;
10
use Drupal\user\Entity\User;
11
12
/**
13
 * Class EventController provides query and render logic for Event occurrences.
14
 */
15
class EventController {
16
17
  /**
18
   * The length of the absolute home URL.
19
   *
20
   * @var int
21
   */
22
  protected $baseLength;
23
24
  /**
25
   * The date.formatter service.
26
   *
27
   * @var \Drupal\Core\Datetime\DateFormatterInterface
28
   */
29
  protected $dateFormatter;
30
31
  /**
32
   * The absolute path to the site home.
33
   *
34
   * @var string
35
   */
36
  protected $front;
37
38
  /**
39
   * An instance cache for user accounts, which are used in a loop.
40
   *
41
   * @var array
42
   */
43
  protected $userCache = [];
44
45
  /**
46
   * The MongoDB logger service, to load events.
47
   *
48
   * @var \Drupal\mongodb_watchdog\Logger
49
   */
50
  protected $watchdog;
51
52
  /**
53
   * EventController constructor.
54
   *
55
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
56
   *   The config.factory service.
57
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
58
   *   The core data.formatter service.
59
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
60
   *   The MongoDB logger service, to load events.
61
   */
62
  public function __construct(ConfigFactoryInterface $config,
63
    DateFormatterInterface $date_formatter,
64
    Logger $watchdog) {
65
    $this->anonymous = $config->get('user.settings')->get('anonymous');
0 ignored issues
show
Bug introduced by
The property anonymous does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
    $this->front = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
67
    $this->baseLength = Unicode::strlen($this->front) - 1;
0 ignored issues
show
Documentation Bug introduced by
It seems like \Drupal\Component\Utilit...trlen($this->front) - 1 can also be of type double. However, the property $baseLength is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
68
    $this->dateFormatter = $date_formatter;
69
    $this->watchdog = $watchdog;
70
  }
71
72
  /**
73
   * Provide a table row representation of an event occurrence.
74
   *
75
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
76
   *   The template for which the occurrence exists.
77
   * @param \Drupal\mongodb_watchdog\Event $event
78
   *   The event occurrence to represent.
79
   *
80
   * @return array
81
   *   A render array.
82
   */
83
  public function asTableRow(EventTemplate $template, Event $event) {
84
    $uid = intval($event->uid);
85
    if (!isset($this->userCache[$uid])) {
86
      $this->userCache[$uid] = $uid ? User::load($uid)->toLink() : $this->anonymous;
87
    }
88
89
    $ret = [
90
      $this->dateFormatter->format($event->timestamp, 'short'),
91
      $this->userCache[$uid],
92
      $template->asString($event->variables),
93
      // Locations generated from Drush/Console will not necessarily match the
94
      // site home URL, and will not therefore not necessarily be reachable, so
95
      // we only generate a link if the location is "within" the site.
96
      (Unicode::strpos($event->location, $this->front) === 0)
97
        ? Link::fromTextAndUrl(Unicode::substr($event->location, $this->baseLength), Url::fromUri($event->location))
98
        : $event->location,
99
      empty($event->referrer) ? '' : Link::fromTextAndUrl($event->referrer, Url::fromUri($event->referrer)),
100
      $event->hostname,
101
      isset($event->requestTracking_id)
102
        ? Link::createFromRoute(t('Request'), 'mongodb_watchdog.reports.request', ['unique_id' => $event->requestTracking_id])
103
        : '',
104
    ];
105
    return $ret;
106
  }
107
108
  /**
109
   * Load MongoDB watchdog events for a given event template.
110
   *
111
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
112
   *   The template for which to find events.
113
   * @param string $skip
114
   *   The string representation of the number of events to skip.
115
   * @param int $limit
116
   *   The limit on the number of events to return.
117
   *
118
   * @return \MongoDB\Driver\Cursor
119
   *   A cursor to the event occurrences.
120
   */
121
  public function find(EventTemplate $template, $skip, $limit) {
122
    $collection = $this->watchdog->eventCollection($template->_id);
123
    $selector = [];
124
    $options = [
125
      'skip' => $skip,
126
      'limit' => $limit,
127
      'sort' => ['$natural' => -1],
128
      'typeMap' => [
129
        'array' => 'array',
130
        'document' => 'array',
131
        'root' => 'Drupal\mongodb_watchdog\Event',
132
      ],
133
    ];
134
135
    $result = $collection->find($selector, $options);
136
    return $result;
137
  }
138
139
}
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...
140