Completed
Push — 8.x-2.x ( 8af97d...4c7eae )
by Frédéric G.
03:11
created

AdminController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * @file
4
 * Contains AdminController.
5
 */
6
7
namespace Drupal\mongodb_watchdog\Controller;
8
9
use Drupal\Component\Utility\SafeMarkup;
10
use Drupal\Component\Utility\Unicode;
11
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12
use Drupal\Core\Url;
13
use Drupal\mongodb\Connection;
14
use Drupal\mongodb_watchdog\Logger;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
/**
19
 * Class AdminController.
20
 *
21
 * @package Drupal\mongodb_watchdog
22
 */
23
class AdminController implements ContainerInjectionInterface {
24
25
  /**
0 ignored issues
show
introduced by
Missing short description in doc comment
Loading history...
26
   * @var \MongoDB
27
   */
28
  protected $database;
29
30
  /**
0 ignored issues
show
introduced by
Missing short description in doc comment
Loading history...
31
   * @var \Drupal\mongodb_watchdog\Logger
32
   */
33
  protected $logger;
34
35
  /**
36
   * Constructor.
37
   *
38
   * @param \MongoDB $database
39
   *   The watchdog database.
40
   * @param \Drupal\mongodb_watchdog\Logger $logger
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
41
   */
42
  public function __construct(\MongoDB $database, Logger $logger) {
43
    $this->database = $database;
44
    $this->logger = $logger;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
45
  }
46
47
  /**
48
   * Controller for mongodb_watchdog.detail.
49
   *
50
   * @return array
51
   *   A render array.
52
   */
53
  public function detail() {
54
    return [];
55
  }
56
57
  /**
58
   * Controller for mongodb_watchdog.overview.
59
   *
60
   * @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...
61
   *   A render array.
62
   */
63
  public function overview() {
64
    $icons = array(
65
      WATCHDOG_DEBUG     => '',
66
      WATCHDOG_INFO      => '',
67
      WATCHDOG_NOTICE    => '',
68
      WATCHDOG_WARNING   => ['#theme' => 'image', 'path' => 'misc/watchdog-warning.png', 'alt' => t('warning'), 'title' => t('warning')],
69
      WATCHDOG_ERROR     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('error'), 'title' => t('error')],
70
      WATCHDOG_CRITICAL  => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('critical'), 'title' => t('critical')],
71
      WATCHDOG_ALERT     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('alert'), 'title' => t('alert')],
72
      WATCHDOG_EMERGENCY => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('emergency'), 'title' => t('emergency')],
73
    );
74
75
    $collection = $this->database->selectCollection(Logger::TEMPLATE_COLLECTION);
76
    $cursor = $collection->find();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
77
78
    $header = array(
79
      // Icon column.
80
      '',
81
      t('#'),
82
      array('data' => t('Type')),
83
      array('data' => t('Date')),
84
      t('Source'),
85
      t('Message'),
86
    );
87
88
    $rows = array();
89
    foreach ($cursor as $id => $value) {
90
      dsm($value, $id);
0 ignored issues
show
introduced by
The use of function dsm() is discouraged
Loading history...
91
//      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 116 characters
Loading history...
Unused Code Comprehensibility introduced by
60% 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...
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
introduced by
6 spaces found before inline comment; expected "// if ($value['type'] == 'php' && $value['message'] == 'type: message in function (line line of file).') {" but found "// if ($value['type'] == 'php' && $value['message'] == 'type: message in function (line line of file).') {"
Loading history...
introduced by
Inline comments must start with a capital letter
Loading history...
92
//        $collection = $this->logger->eventCollection($value['_id']);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 6 spaces
Loading history...
93
//        $result = $collection->find()
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
94
//                             ->sort(array('$natural' => -1))
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 8 spaces
Loading history...
95
//                             ->limit(1)
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
96
//                             ->getNext();
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
97
//        if ($value) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
98
//          $value['file'] = basename($result['variables']['%file']);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 8 spaces
Loading history...
99
//          $value['line'] = $result['variables']['%line'];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
100
//          $value['message'] = '%type in %function';
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
101
//          $value['variables'] = $result['variables'];
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
102
//        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
103
//      }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 6 spaces, found 0
Loading history...
104
      $message = Unicode::truncate(strip_tags(SafeMarkup::format($value)), 56, TRUE, TRUE);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105
      $value['count'] = $this->logger->eventCollection($value['_id'])->count();
106
      $rows[$id] = array(
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
107
        $icons[$value['severity']],
108
        isset($value['count']) && $value['count'] > 1 ? intval($value['count']) : 0,
109
        t($value['type']),
0 ignored issues
show
introduced by
Only string literals should be passed to t() where possible
Loading history...
110
        empty($value['timestamp']) ? '' : format_date($value['timestamp'], 'short'),
111
        empty($value['file']) ? '' : Unicode::truncate(basename($value['file']), 30) . (empty($value['line']) ? '' : ('+' . $value['line'])),
112
        \Drupal::l($message, Url::fromRoute('mongodb_watchdog.detail', ['id' => $id])),
113
      );
114
    }
115
116
    $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...
117
      '#theme' => 'table',
118
      '#header' => $header,
119
      '#rows' => $rows,
120
      '#attributes' => ['id' => 'admin-mongodb_watchdog'],
121
    );
122
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
123
124
    return $build;
125
  }
126
127
  /**
128
   * The controller factory.
129
   *
130
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
131
   *   The DIC.
132
   *
133
   * @return static
134
   *   The database instance.
135
   */
136
  public static function create(ContainerInterface $container) {
137
    /** @var \MongoDB $database */
138
    $database = $container->get('mongodb.watchdog_storage');
139
    /** @var \Drupal\mongodb_watchdog\Logger $logger */
140
    $logger = $container->get('mongodb.logger');
141
142
    return new static($database, $logger);
143
  }
144
}
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...
145