Completed
Pull Request — 8.x-2.x (#5)
by Frédéric G.
02:46
created

src/Controller/AdminController.php (32 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Controller\ControllerBase;
12
use Drupal\Core\Logger\RfcLogLevel;
13
use Drupal\Core\Url;
14
use Drupal\mongodb_watchdog\Logger;
15
use MongoDB\Database;
16
use Psr\Log\LogLevel;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Drupal\Core\Extension\ModuleHandlerInterface;
20
use Drupal\Core\Form\FormBuilderInterface;
21
22
23
/**
24
 * Class AdminController.
25
 *
26
 * @package Drupal\mongodb_watchdog
27
 */
28
class AdminController extends ControllerBase {
29
30
  /**
31
   * The MongoDB database for the logger alias.
32
   *
33
   * @var \MongoDB
34
   */
35
  protected $database;
36
37
  /**
38
   * The core logger channel, to log intervening events.
39
   *
40
   * @var \Psr\Log\LoggerInterface
41
   */
42
  protected $logger;
43
44
  /**
45
   * The MongoDB logger, to load events.
46
   *
47
   * @var \Drupal\mongodb_watchdog\Logger
48
   */
49
  protected $watchdog;
50
51
   * The module handler service.
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '*', expecting T_FUNCTION
Loading history...
Line indented incorrectly; expected 2 spaces, found 3
Loading history...
Expected 1 space before "*"; newline found
Loading history...
Concat operator must be surrounded by spaces
Loading history...
52
   *
0 ignored issues
show
Expected 1 space before "*"; newline found
Loading history...
53
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
0 ignored issues
show
Expected 1 space before "*"; newline found
Loading history...
54
   */
0 ignored issues
show
Expected 1 space before "*"; newline found
Loading history...
Expected 1 space after "*"; 0 found
Loading history...
Expected 1 space before "/"; 0 found
Loading history...
55
  protected $moduleHandler;
56
57
  /**
58
   * The form builder service.
59
   *
60
   * @var \Drupal\Core\Form\FormBuilderInterface
61
   */
62
  protected $formBuilder;
63
64
  /**
65
   * Constructor.
66
   *
67
   * @param \MongoDB $database
68
   *   The watchdog database.
69
   * @param \Psr\Log\LoggerInterface $logger
70
   *   The logger service, to log intervening events.
71
   * @param \Drupal\mongodb_watchdog\Logger $watchdog
72
   *   The MongoDB logger, to load stored events.
73
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
74
   *   A module handler.
75
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
76
   *   The form builder service.
77
   */
78
  public function __construct(Database $database, LoggerInterface $logger, Logger $watchdog, ModuleHandlerInterface $module_handler, FormBuilderInterface $form_builder) {
0 ignored issues
show
Expected type hint "MongoDB"; found "Database" for $database
Loading history...
79
    $this->database = $database;
80
    $this->logger = $logger;
81
    $this->moduleHandler = $module_handler;
82
    $this->formBuilder = $form_builder;
83
    $this->watchdog = $watchdog;
84
  }
85
86
  /**
87
   * Controller for mongodb_watchdog.overview.
88
   *
89
   * @return array
90
   *   A render array.
91
   */
92
  public function overview() {
93
    $icons = array(
94
      RfcLogLevel::DEBUG     => '',
95
      RfcLogLevel::INFO      => '',
96
      RfcLogLevel::NOTICE    => '',
97
      RfcLogLevel::WARNING   => ['#theme' => 'image', 'path' => 'misc/watchdog-warning.png', 'alt' => t('warning'), 'title' => t('warning')],
98
      RfcLogLevel::ERROR     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('error'), 'title' => t('error')],
99
      RfcLogLevel::CRITICAL  => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('critical'), 'title' => t('critical')],
100
      RfcLogLevel::ALERT     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('alert'), 'title' => t('alert')],
101
      RfcLogLevel::EMERGENCY => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('emergency'), 'title' => t('emergency')],
102
    );
103
104
    $collection = $this->watchdog->templateCollection();
105
    $templates = $collection->find([], TopController::LEGACY_TYPE_MAP)->toArray();
106
ksm($templates);
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
107
    $this->moduleHandler->loadInclude('mongodb_watchdog', 'admin.inc');
108
109
    $build['dblog_filter_form'] = $this->formBuilder->getForm('Drupal\mongodb_watchdog\Form\MongodbWatchdogFilterForm');
110
111
    $header = array(
112
      // Icon column.
113
      '',
114
      t('#'),
115
      array('data' => t('Type')),
116
      array('data' => t('Date')),
117
      t('Source'),
118
      t('Message'),
119
    );
120
121
    $rows = array();
122
    foreach ($templates as $id => $value) {
123
      if ($id < 5) {
124
//        if ($value['type'] == 'php' && $value['message'] == '%type: %message in %function (line %line of %file).') {
0 ignored issues
show
Line exceeds 80 characters; contains 118 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...
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
8 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...
Inline comments must start with a capital letter
Loading history...
125
//          $collection = $this->logger->eventCollection($value['_id']);
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
Comment indentation error, expected only 8 spaces
Loading history...
126
//          $result = $collection->find()
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
127
//            ->sort(['$natural' => -1])
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
Comment indentation error, expected only 10 spaces
Loading history...
128
//            ->limit(1)
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
129
//            ->getNext();
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
130
//          if ($value) {
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
131
//            $value['file'] = basename($result['variables']['%file']);
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
Comment indentation error, expected only 10 spaces
Loading history...
132
//            $value['line'] = $result['variables']['%line'];
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
133
//            $value['message'] = '%type in %function';
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
134
//            $value['variables'] = $result['variables'];
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
135
//          }
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
136
//        }
0 ignored issues
show
Line indented incorrectly; expected 8 spaces, found 0
Loading history...
137
        $message = Unicode::truncate(strip_tags(SafeMarkup::format($value['message'], [])), 56, TRUE, TRUE);
138
        $value['count'] = $this->watchdog->eventCollection($value['_id'])->count();
139
        $rows[$id] = [
140
          $icons[$value['severity']],
141
          isset($value['count']) && $value['count'] > 1 ? intval($value['count']) : 0,
142
          t($value['type']),
143
          empty($value['timestamp']) ? '' : format_date($value['timestamp'], 'short'),
144
          empty($value['file']) ? '' : Unicode::truncate(basename($value['file']), 30) . (empty($value['line']) ? '' : ('+' . $value['line'])),
145
          \Drupal::l($message, Url::fromRoute('mongodb_watchdog.reports.detail', ['id' => $id])),
146
        ];
147
      }
148
149
    }
150
kint($rows);
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
151
    $build['mongodb_watchdog_table'] = array(
152
      '#theme' => 'table',
153
      '#header' => $header,
154
      '#rows' => $rows,
155
      '#attributes' => ['id' => 'admin-mongodb_watchdog'],
156
      '#attached' => array(
157
        'library' => array('mongodb_watchdog/drupal.mongodb_watchdog'),
158
      ),
159
    );
160
161
    $build['mongodb_watchdog_pager'] = array('#type' => 'pager');
162
163
    return $build;
164
  }
165
166
  /**
167
   * The controller factory.
168
   *
169
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
170
   *   The DIC.
171
   *
172
   * @return static
173
   *   The database instance.
174
   */
175
  public static function create(ContainerInterface $container) {
176
    /** @var \MongoDB $database */
177
    $database = $container->get('mongodb.watchdog_storage');
178
179
    $form_builder = $container->get('form_builder');
180
181
    $module_handler = $container->get('module_handler');
182
183
    /** @var \Psr\Log\LoggerInterface $logger */
184
    $logger = $container->get('logger.channel.mongodb_watchdog');
185
186
    /** @var \Drupal\mongodb_watchdog\Logger $logger */
187
    $watchdog = $container->get('mongodb.logger');
188
189
    return new static($database, $logger, $watchdog, $module_handler, $form_builder);
190
  }
191
}
192