Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 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. |
||
| 52 | * |
||
| 53 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
||
| 54 | */ |
||
| 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) { |
||
| 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); |
||
| 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).') { |
||
| 125 | // $collection = $this->logger->eventCollection($value['_id']); |
||
| 126 | // $result = $collection->find() |
||
| 127 | // ->sort(['$natural' => -1]) |
||
| 128 | // ->limit(1) |
||
| 129 | // ->getNext(); |
||
| 130 | // if ($value) { |
||
| 131 | // $value['file'] = basename($result['variables']['%file']); |
||
| 132 | // $value['line'] = $result['variables']['%line']; |
||
| 133 | // $value['message'] = '%type in %function'; |
||
| 134 | // $value['variables'] = $result['variables']; |
||
| 135 | // } |
||
| 136 | // } |
||
| 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); |
||
| 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'), |
||
| 192 |