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

EventController::asTableRow()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 14
c 2
b 0
f 1
nc 12
nop 2
dl 0
loc 22
rs 8.6737
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 rows 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),
91
      $this->userCache[$uid],
92
      // Locations generated from Drush/Console will not necessarily match the
93
      // site home URL, and will not therefore not necessarily be reachable, so
94
      // we only generate a link if the location is "within" the site.
95
      (Unicode::strpos($event->location, $this->front) === 0)
96
        ? Link::fromTextAndUrl(Unicode::substr($event->location, $this->baseLength), Url::fromUri($event->location))
97
        : $event->location,
98
      empty($event->referrer) ? '' : Link::fromTextAndUrl($event->referrer, Url::fromUri($event->referrer)),
99
      $event->hostname,
100
      $template->asString($event->variables),
101
      [],
102
    ];
103
    return $ret;
104
  }
105
106
  /**
107
   * Load MongoDB watchdog events for a given event template.
108
   *
109
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
110
   *   The template for which to find events.
111
   * @param string $first_id
112
   *   The string representation of the first event to match.
113
   * @param int $limit
114
   *   The limit on the number of objects to return.
115
   *
116
   * @return \MongoDB\Driver\Cursor
117
   *   A cursor to the event occurrences.
118
   */
119
  public function find(EventTemplate $template, $first_id, $limit) {
0 ignored issues
show
Unused Code introduced by
The parameter $first_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    $collection = $this->watchdog->eventCollection($template->_id);
121
    $selector = [];
122
    $options = [
123
      'limit' => $limit,
124
      'sort' => ['timestamp' => -1],
125
      'typeMap' => [
126
        'array' => 'array',
127
        'document' => 'array',
128
        'root' => 'Drupal\mongodb_watchdog\Event',
129
      ],
130
    ];
131
132
    $result = $collection->find($selector, $options);
133
    return $result;
134
  }
135
136
  /**
137
   * Return the number of event occurrences for a given template.
138
   *
139
   * @param \Drupal\mongodb_watchdog\EventTemplate $template
140
   *   The template for which to count events.
141
   *
142
   * @return int
143
   *   The number of occurrences.
144
   */
145
  public function count(EventTemplate $template) {
146
    $collection = $this->watchdog->eventCollection($template->_id);
147
    $ret = $collection->count();
148
    return $ret;
149
  }
150
151
}
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...
152