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

AdminController::detail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @file
5
 * Contains AdminController.
6
 */
7
8
namespace Drupal\mongodb_watchdog\Controller;
9
10
use Drupal\Component\Utility\Unicode;
11
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12
use Drupal\Core\Url;
13
use Drupal\mongodb_watchdog\Logger;
14
use MongoDB\Database;
15
use Psr\Log\LogLevel;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
/**
20
 * Class AdminController.
21
 *
22
 * @package Drupal\mongodb_watchdog
23
 */
24
class AdminController implements ContainerInjectionInterface {
25
26
  /**
27
   * The MongoDB database for the logger alias.
28
   *
29
   * @var \MongoDB
30
   */
31
  protected $database;
32
33
  /**
34
   * The core logger channel, to log intervening events.
35
   *
36
   * @var \Psr\Log\LoggerInterface
37
   */
38
  protected $logger;
39
40
  /**
41
   * The MongoDB logger, to load events.
42
   *
43
   * @var \Drupal\mongodb_watchdog\Logger
44
   */
45
  protected $watchdog;
46
47
  /**
48
   * Constructor.
49
   *
50
   * @param \MongoDB\Database $database
51
   *   The watchdog database.
52
   * @param \Psr\Log\LoggerInterface $logger
53
   *   The logger service, to log intervening events.
54
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
55
   *   The MongoDB logger, to load stored events.
56
   */
57
  public function __construct(Database $database, LoggerInterface $logger, Logger $watchdog) {
58
    $this->database = $database;
0 ignored issues
show
Documentation Bug introduced by
It seems like $database of type object<MongoDB\Database> is incompatible with the declared type object<MongoDB> of property $database.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
    $this->logger = $logger;
60
    $this->watchdog = $watchdog;
61
  }
62
63
  /**
64
   * Controller for mongodb_watchdog.overview.
65
   *
66
   * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,string|array>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
67
   *   A render array.
68
   */
69
  public function overview() {
70
    $icons = array(
0 ignored issues
show
Unused Code introduced by
$icons is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
      LogLevel::DEBUG     => '',
72
      LogLevel::INFO      => '',
73
      LogLevel::NOTICE    => '',
74
      LogLevel::WARNING   => ['#theme' => 'image', 'path' => 'misc/watchdog-warning.png', 'alt' => t('warning'), 'title' => t('warning')],
75
      LogLevel::ERROR     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('error'), 'title' => t('error')],
76
      LogLevel::CRITICAL  => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('critical'), 'title' => t('critical')],
77
      LogLevel::ALERT     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('alert'), 'title' => t('alert')],
78
      LogLevel::EMERGENCY => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('emergency'), 'title' => t('emergency')],
79
    );
0 ignored issues
show
introduced by
Unused variable $icons.
Loading history...
80
81
    $collection = $this->database->selectCollection(Logger::TEMPLATE_COLLECTION);
82
    $cursor = $collection->find();
83
84
    $header = array(
85
      // Icon column.
86
      '',
87
      t('#'),
88
      array('data' => t('Type')),
89
      array('data' => t('Date')),
90
      t('Source'),
91
      t('Message'),
92
    );
93
94
    $rows = array();
95
    foreach ($cursor as $id => $value) {
96
      // dsm($value, $id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
      /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
      if ($value['type'] == 'php' && $value['message'] == '%type: %message in %function (line %line of %file).') {
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 114 characters
Loading history...
99
        $collection = $this->logger->eventCollection($value['_id']);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 8
Loading history...
100
        $result = $collection->find()
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 8
Loading history...
101
                             ->sort(array('$natural' => -1))
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 29
Loading history...
102
                             ->limit(1)
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 29
Loading history...
103
                             ->getNext();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 29
Loading history...
104
        if ($value) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 8
Loading history...
105
          $value['file'] = basename($result['variables']['%file']);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 10
Loading history...
106
          $value['line'] = $result['variables']['%line'];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 10
Loading history...
107
          $value['message'] = '%type in %function';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 10
Loading history...
108
          $value['variables'] = $result['variables'];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 10
Loading history...
109
        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 8
Loading history...
110
      }
111
      */
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 7 spaces, found 6
Loading history...
112
      $message = ''; //Unicode::truncate(strip_tags(SafeMarkup::format($value)), 56, TRUE, TRUE);
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 97 characters
Loading history...
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
introduced by
No space before comment text; expected "// Unicode::truncate(strip_tags(SafeMarkup::format($value)), 56, TRUE, TRUE);" but found "//Unicode::truncate(strip_tags(SafeMarkup::format($value)), 56, TRUE, TRUE);"
Loading history...
Coding Style introduced by
Comments may not appear after statements
Loading history...
113
      //$value['count'] = $this->logger->eventCollection($value['_id'])->count();
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 81 characters
Loading history...
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
introduced by
No space before comment text; expected "// $value['count'] = $this->logger->eventCollection($value['_id'])->count();" but found "//$value['count'] = $this->logger->eventCollection($value['_id'])->count();"
Loading history...
114
      $rows[$id] = array(
115
        //$icons[$value['severity']],
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
introduced by
No space before comment text; expected "// $icons[$value['severity']]," but found "//$icons[$value['severity']],"
Loading history...
116
        isset($value['count']) && $value['count'] > 1 ? intval($value['count']) : 0,
117
        t($value['type']),
0 ignored issues
show
introduced by
Only string literals should be passed to t() where possible
Loading history...
118
        empty($value['timestamp']) ? '' : format_date($value['timestamp'], 'short'),
119
        empty($value['file']) ? '' : Unicode::truncate(basename($value['file']), 30) . (empty($value['line']) ? '' : ('+' . $value['line'])),
120
        \Drupal::l($message, Url::fromRoute('mongodb_watchdog.reports.detail', ['event_template' => $id])),
121
      );
122
    }
123
124
    $build['mongodb_watchdog_table'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$build was never initialized. Although not strictly required by PHP, it is generally a good practice to add $build = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
125
      '#theme' => 'table',
126
      '#header' => $header,
127
      '#rows' => $rows,
128
      '#attributes' => ['id' => 'admin-mongodb_watchdog'],
129
    );
130
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
131
132
    return $build;
133
  }
134
135
  /**
136
   * The controller factory.
137
   *
138
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
139
   *   The DIC.
140
   *
141
   * @return static
142
   *   The database instance.
143
   */
144 View Code Duplication
  public static function create(ContainerInterface $container) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    /** @var \MongoDB $database */
146
    $database = $container->get('mongodb.watchdog_storage');
147
148
    /** @var \Psr\Log\LoggerInterface $logger */
149
    $logger = $container->get('logger.channel.mongodb_watchdog');
150
151
    /** @var \Drupal\mongodb_watchdog\Logger $logger */
152
    $watchdog = $container->get('mongodb.logger');
153
154
    return new static($database, $logger, $watchdog);
155
  }
156
}
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...
157