HideNotificationController.php in OptimizeDB 8
File
src/Controller/HideNotificationController.php
View source
<?php
namespace Drupal\optimizedb\Controller;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HideNotificationController extends ControllerBase {
protected $time;
protected $configFactory;
protected $dateFormatter;
public function __construct(TimeInterface $time, ConfigFactoryInterface $configFactory, DateFormatterInterface $dateFormatter) {
$this->time = $time;
$this->configFactory = $configFactory;
$this->dateFormatter = $dateFormatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('datetime.time'), $container
->get('config.factory'), $container
->get('date.formatter'));
}
public function hide() {
$time = $this->time
->getRequestTime();
$config = $this->configFactory
->getEditable('optimizedb.settings');
$notify_optimize = $config
->get('notify_optimize');
if ($notify_optimize) {
$config
->set('notify_optimize', FALSE)
->set('last_optimization', $time)
->save();
$optimization_period = (int) $config
->get('optimization_period');
$time_next_optimization = strtotime('+ ' . $optimization_period . ' day', $time);
$output = $this
->t('The following message on the need to perform optimization, you get - @date.', [
'@date' => $this->dateFormatter
->format($time_next_optimization),
]);
}
else {
$output = $this
->t('Alerts are not available.');
}
return [
'#type' => 'markup',
'#markup' => $output,
];
}
}