From 8d98947271f7b41787b1e0c036ae1cdbb7cddb03 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 6 Apr 2014 21:34:23 +0100 Subject: [PATCH] Issue #1957152 by brantwynn: Replace calls to the flood service with Drupal::flood(). --- .../Drupal/contact/MessageFormController.php | 49 ++++++++++++++++++- .../Drupal/system/Tests/System/FloodTest.php | 2 +- core/modules/system/system.module | 2 +- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/core/modules/contact/lib/Drupal/contact/MessageFormController.php b/core/modules/contact/lib/Drupal/contact/MessageFormController.php index 122665f2741..20588871e9c 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageFormController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageFormController.php @@ -8,9 +8,13 @@ namespace Drupal\contact; use Drupal\Component\Utility\String; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Language\Language; use Drupal\user\UserInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for contact message forms. @@ -24,6 +28,48 @@ class MessageFormController extends ContentEntityFormController { */ protected $entity; + /** + * The config factory service. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * The flood control mechanism. + * + * @var \Drupal\Core\Flood\FloodInterface + */ + protected $flood; + + /** + * Constructs a MessageFormController object. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The factory for configuration objects. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. + * @param \Drupal\Core\Flood\FloodInterface $flood + * The flood control mechanism. + */ + public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, FloodInterface $flood) { + parent::__construct($entity_manager); + + $this->configFactory = $config_factory; + $this->flood = $flood; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('entity.manager'), + $container->get('flood') + ); + } + /** * Overrides Drupal\Core\Entity\EntityFormController::form(). */ @@ -195,7 +241,8 @@ class MessageFormController extends ContentEntityFormController { drupal_mail('contact', 'page_autoreply', $sender->getEmail(), $language_interface->id, $params); } - \Drupal::service('flood')->register('contact', \Drupal::config('contact.settings')->get('flood.interval')); + $config = $this->configFactory->get('contact.settings'); + $this->flood->register('contact', $config->get('flood.interval')); if (!$message->isPersonal()) { watchdog('contact', '%sender-name (@sender-from) sent an e-mail regarding %category.', array( '%sender-name' => $sender->name, diff --git a/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php b/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php index e7cbdb2e830..76c00b41828 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php @@ -48,7 +48,7 @@ class FloodTest extends WebTestBase { $name = 'flood_test_cleanup'; // Register expired event. - $flood = \Drupal::service('flood'); + $flood = \Drupal::flood(); $flood->register($name, $window_expired); // Verify event is not allowed. $this->assertFalse($flood->isAllowed($name, $threshold)); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 4b6a1654751..9ab9bd586aa 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1558,7 +1558,7 @@ function system_get_module_admin_tasks($module, array $info) { */ function system_cron() { // Cleanup the flood. - \Drupal::service('flood')->garbageCollection(); + \Drupal::flood()->garbageCollection(); foreach (Cache::getBins() as $cache_backend) { $cache_backend->garbageCollection();