From fd42aa857bc621e60a52131618c7698d3afae77c Mon Sep 17 00:00:00 2001 From: Doug Green Date: Thu, 8 Feb 2018 09:57:24 -0500 Subject: [PATCH 1/2] #2913690: PHPCS and refactor cleanup --- config/install/cloudflare.settings.yml | 6 + config/schema/cloudflare.settings.schema.yml | 2 +- .../src/Mocks/ZoneMock.php | 12 +- .../Purge/DiagnosticCheck/CredentialCheck.php | 6 +- .../Plugin/Purge/Purger/CloudFlarePurger.php | 6 +- .../DiagnosticCheck/ApiRateLimitCheckTest.php | 3 +- .../DailyTagPurgeLimitCheckTest.php | 3 +- src/EventSubscriber/ClientIpRestore.php | 6 +- src/Form/SettingsForm.php | 158 +++++++++--------- src/Form/ZoneSelectionForm.php | 40 ++++- src/Timestamp.php | 4 +- src/Zone.php | 18 +- 12 files changed, 146 insertions(+), 118 deletions(-) create mode 100644 config/install/cloudflare.settings.yml diff --git a/config/install/cloudflare.settings.yml b/config/install/cloudflare.settings.yml new file mode 100644 index 0000000..ab22eeb --- /dev/null +++ b/config/install/cloudflare.settings.yml @@ -0,0 +1,6 @@ +client_ip_restore_enabled: false +bypass_host: '' +valid_credentials: false +zone_id: '' +apikey: '' +email: '' diff --git a/config/schema/cloudflare.settings.schema.yml b/config/schema/cloudflare.settings.schema.yml index 7f60ad4..ad5cff8 100644 --- a/config/schema/cloudflare.settings.schema.yml +++ b/config/schema/cloudflare.settings.schema.yml @@ -1,4 +1,4 @@ - # Schema for the configuration files of the CloudFlare module. +# Schema for the configuration files of the CloudFlare module. cloudflare.settings: type: config_object label: 'CloudFlare Config' diff --git a/modules/cloudflare_form_tester/src/Mocks/ZoneMock.php b/modules/cloudflare_form_tester/src/Mocks/ZoneMock.php index 6559108..5b76471 100644 --- a/modules/cloudflare_form_tester/src/Mocks/ZoneMock.php +++ b/modules/cloudflare_form_tester/src/Mocks/ZoneMock.php @@ -71,10 +71,10 @@ class ZoneMock implements CloudFlareZoneInterface { /** * {@inheritdoc} */ - public static function create(ConfigFactoryInterface $config, LoggerInterface $logger, CloudFlareStateInterface $state, CloudFlareComposerDependenciesCheckInterface $check_interface) { + public static function create(ConfigFactoryInterface $config_factory, LoggerInterface $logger, CloudFlareStateInterface $state, CloudFlareComposerDependenciesCheckInterface $check_interface) { return new static( - $config, + $config_factory, $logger, $state, $check_interface @@ -84,8 +84,8 @@ public static function create(ConfigFactoryInterface $config, LoggerInterface $l /** * Zone constructor. * - * @param \Drupal\Core\Config\ConfigFactoryInterface $config - * CloudFlare config object. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. * @param \Psr\Log\LoggerInterface $logger * A logger instance. * @param \Drupal\cloudflare\CloudFlareStateInterface $state @@ -93,8 +93,8 @@ public static function create(ConfigFactoryInterface $config, LoggerInterface $l * @param \Drupal\cloudflare\CloudFlareComposerDependenciesCheckInterface $check_interface * Checks that composer dependencies are met. */ - public function __construct(ConfigFactoryInterface $config, LoggerInterface $logger, CloudFlareStateInterface $state, CloudFlareComposerDependenciesCheckInterface $check_interface) { - $this->config = $config->get('cloudflare.settings'); + public function __construct(ConfigFactoryInterface $config_factory, LoggerInterface $logger, CloudFlareStateInterface $state, CloudFlareComposerDependenciesCheckInterface $check_interface) { + $this->config = $config_factory->get('cloudflare.settings'); $this->logger = $logger; $this->state = $state; $this->zone = $this->config->get('zone'); diff --git a/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/CredentialCheck.php b/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/CredentialCheck.php index 580c92e..3785c59 100644 --- a/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/CredentialCheck.php +++ b/modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/CredentialCheck.php @@ -35,12 +35,12 @@ class CredentialCheck extends DiagnosticCheckBase implements DiagnosticCheckInte * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The factory for configuration objects. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->config = $config->get('cloudflare.settings'); + $this->config = $config_factory->get('cloudflare.settings'); } /** diff --git a/modules/cloudflarepurger/src/Plugin/Purge/Purger/CloudFlarePurger.php b/modules/cloudflarepurger/src/Plugin/Purge/Purger/CloudFlarePurger.php index 99bf819..4fed2ef 100644 --- a/modules/cloudflarepurger/src/Plugin/Purge/Purger/CloudFlarePurger.php +++ b/modules/cloudflarepurger/src/Plugin/Purge/Purger/CloudFlarePurger.php @@ -93,7 +93,7 @@ public static function create(ContainerInterface $container, array $configuratio * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The factory for configuration objects. * @param \Drupal\cloudflare\CloudFlareStateInterface $state * Tracks limits associated with CloudFlare Api. @@ -105,10 +105,10 @@ public static function create(ContainerInterface $container, array $configuratio * @throws \LogicException * Thrown if $configuration['id'] is missing, see Purger\Service::createId. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config, CloudFlareStateInterface $state, LoggerInterface $logger, CloudFlareComposerDependenciesCheckInterface $checker) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, CloudFlareStateInterface $state, LoggerInterface $logger, CloudFlareComposerDependenciesCheckInterface $checker) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->config = $config->get('cloudflare.settings'); + $this->config = $config_factory->get('cloudflare.settings'); $this->state = $state; $this->logger = $logger; $this->areCloudflareComposerDepenciesMet = $checker->check(); diff --git a/modules/cloudflarepurger/src/Tests/DiagnosticCheck/ApiRateLimitCheckTest.php b/modules/cloudflarepurger/src/Tests/DiagnosticCheck/ApiRateLimitCheckTest.php index 45a862a..848a525 100644 --- a/modules/cloudflarepurger/src/Tests/DiagnosticCheck/ApiRateLimitCheckTest.php +++ b/modules/cloudflarepurger/src/Tests/DiagnosticCheck/ApiRateLimitCheckTest.php @@ -2,7 +2,6 @@ namespace Drupal\cloudflarepurger\Tests\DiagnosticCheck; -use DateTime; use Drupal\purge\Plugin\Purge\DiagnosticCheck\DiagnosticCheckInterface; use Drupal\cloudflare\State; use Drupal\cloudflarepurger\Plugin\Purge\DiagnosticCheck\ApiRateLimitCheck; @@ -29,7 +28,7 @@ class ApiRateLimitCheckTest extends DiagnosticCheckTestBase { */ public function testApiRateLimitCheck($api_rate, $expected_severity) { $this->drupalState->set(State::API_RATE_COUNT, $api_rate); - $this->drupalState->set(State::API_RATE_COUNT_START, new DateTime()); + $this->drupalState->set(State::API_RATE_COUNT_START, new \DateTime()); $api_rate_limit_check = new ApiRateLimitCheck([], '23123', 'this is a definition', $this->cloudflareState, $this->composerDependencyStub); $actual_severity = $api_rate_limit_check->run(); diff --git a/modules/cloudflarepurger/src/Tests/DiagnosticCheck/DailyTagPurgeLimitCheckTest.php b/modules/cloudflarepurger/src/Tests/DiagnosticCheck/DailyTagPurgeLimitCheckTest.php index b057005..6c135a6 100644 --- a/modules/cloudflarepurger/src/Tests/DiagnosticCheck/DailyTagPurgeLimitCheckTest.php +++ b/modules/cloudflarepurger/src/Tests/DiagnosticCheck/DailyTagPurgeLimitCheckTest.php @@ -2,7 +2,6 @@ namespace Drupal\cloudflarepurger\Tests\DiagnosticCheck; -use DateTime; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\purge\Plugin\Purge\DiagnosticCheck\DiagnosticCheckInterface; use Drupal\cloudflare\State; @@ -39,7 +38,7 @@ public function setUp() { */ public function testDailyTagPurgeLimitCheck($api_rate, $expected_severity) { $this->drupalState->set(State::TAG_PURGE_DAILY_COUNT, $api_rate); - $this->drupalState->set(State::TAG_PURGE_DAILY_COUNT_START, new DateTime()); + $this->drupalState->set(State::TAG_PURGE_DAILY_COUNT_START, new \DateTime()); $api_rate_limit_check = new DailyTagPurgeLimitCheck([], '23123', 'this is a definition', $this->cloudflareState, $this->composerDependencyStub); $actual_severity = $api_rate_limit_check->run(); diff --git a/src/EventSubscriber/ClientIpRestore.php b/src/EventSubscriber/ClientIpRestore.php index 71d43d3..e7d68f7 100644 --- a/src/EventSubscriber/ClientIpRestore.php +++ b/src/EventSubscriber/ClientIpRestore.php @@ -67,7 +67,7 @@ class ClientIpRestore implements EventSubscriberInterface { /** * Constructs a ClientIpRestore. * - * @param \Drupal\Core\Config\ConfigFactoryInterface $config + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The factory for configuration objects. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * Cache backend. @@ -76,10 +76,10 @@ class ClientIpRestore implements EventSubscriberInterface { * @param \Psr\Log\LoggerInterface $logger * A logger instance. */ - public function __construct(ConfigFactoryInterface $config, CacheBackendInterface $cache, ClientInterface $http_client, LoggerInterface $logger) { + public function __construct(ConfigFactoryInterface $config_factory, CacheBackendInterface $cache, ClientInterface $http_client, LoggerInterface $logger) { $this->httpClient = $http_client; $this->cache = $cache; - $this->config = $config->get('cloudflare.settings'); + $this->config = $config_factory->get('cloudflare.settings'); $this->logger = $logger; $this->isClientIpRestoreEnabled = $this->config->get(SELF::CLOUDFLARE_CLIENT_IP_RESTORE_ENABLED); $this->bypassHost = $this->config->get(SELF::CLOUDFLARE_BYPASS_HOST); diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 702c767..48f6427 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -3,6 +3,7 @@ namespace Drupal\cloudflare\Form; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\Config; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; @@ -16,7 +17,6 @@ use CloudFlarePhpSdk\Exceptions\CloudFlareException; use CloudFlarePhpSdk\Exceptions\CloudFlareTimeoutException; use CloudFlarePhpSdk\Exceptions\CloudFlareInvalidCredentialException; -use InvalidArgumentException; /** * Class SettingsForm. @@ -40,21 +40,21 @@ class SettingsForm extends FormBase implements ContainerInjectionInterface { protected $zoneApi; /** - * The cloudflare settings configuration. + * The configuration factory. * - * @var \Drupal\Core\Config\Config + * @var \Drupal\Core\Config\ConfigFactoryInterface */ - protected $config; + protected $configFactory; /** - * A logger instance for cloudflare. + * A logger instance for CloudFlare. * * @var \Psr\Log\LoggerInterface */ protected $logger; /** - * Tracks rate limits associated with CloudFlare Api. + * Tracks rate limits associated with CloudFlare API. * * @var \Drupal\cloudflare\CloudFlareStateInterface */ @@ -68,18 +68,11 @@ class SettingsForm extends FormBase implements ContainerInjectionInterface { protected $cloudFlareComposerDependenciesCheck; /** - * Tracks if a zoneId has been set. + * Boolean indicates if CloudFlare dependencies have been met. * * @var bool */ - protected $hasZoneId; - - /** - * Tracks if valid credentials have been entered. - * - * @var bool - */ - protected $hasValidCredentials; + protected $cloudFlareComposerDependenciesMet; /** * {@inheritdoc} @@ -104,10 +97,10 @@ public static function create(ContainerInterface $container) { /** * Constructs a new CloudFlareAdminSettingsForm. * - * @param \Drupal\Core\Config\ConfigFactoryInterface $config + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The factory for configuration objects. * @param \Drupal\cloudflare\CloudFlareStateInterface $state - * Tracks rate limits associated with CloudFlare Api. + * Tracks rate limits associated with CloudFlare API. * @param \Drupal\cloudflare\CloudFlareZoneInterface $zone_api * ZoneApi instance for accessing api. * @param \Psr\Log\LoggerInterface $logger @@ -117,32 +110,14 @@ public static function create(ContainerInterface $container) { * @param \Drupal\cloudflare\CloudFlareComposerDependenciesCheckInterface $check_interface * Checks if composer dependencies are met. */ - public function __construct(ConfigFactoryInterface $config, CloudFlareStateInterface $state, CloudFlareZoneInterface $zone_api, LoggerInterface $logger, EmailValidator $email_validator, CloudFlareComposerDependenciesCheckInterface $check_interface) { - $this->configFactory = $config; - $this->config = $config->getEditable('cloudflare.settings'); + public function __construct(ConfigFactoryInterface $config_factory, CloudFlareStateInterface $state, CloudFlareZoneInterface $zone_api, LoggerInterface $logger, EmailValidator $email_validator, CloudFlareComposerDependenciesCheckInterface $check_interface) { + $this->configFactory = $config_factory; $this->state = $state; $this->zoneApi = $zone_api; $this->logger = $logger; $this->emailValidator = $email_validator; - $this->cloudFlareComposerDependenciesMet = $check_interface->check(); $this->cloudFlareComposerDependenciesCheck = $check_interface; - $this->hasZoneId = !empty($this->config->get('zone_id')); - $this->hasValidCredentials = $this->config->get('valid_credentials') === TRUE; - - if ($this->hasValidCredentials && $this->cloudFlareComposerDependenciesMet) { - try { - $this->zones = $this->zoneApi->listZones(); - $this->hasMultipleZones = count($this->zones) > 1; - } - catch (CloudFlareTimeoutException $e) { - drupal_set_message("Unable to connect to CloudFlare in order to validate credentials. Connection timed out. Please try again later.", 'error'); - } - } - else { - $this->zones = []; - $this->hasMultipleZones = FALSE; - } - + $this->cloudFlareComposerDependenciesMet = $check_interface->check(); } /** @@ -165,9 +140,10 @@ public function getFormId() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $form = array_merge($form, $this->buildApiCredentialsSection()); - $form = array_merge($form, $this->buildZoneSelectSection()); - $form = array_merge($form, $this->buildGeneralConfig()); + $config = $this->configFactory->get('cloudflare.settings'); + $form = array_merge($form, $this->buildApiCredentialsSection($config)); + $form = array_merge($form, $this->buildZoneSelectSection($config)); + $form = array_merge($form, $this->buildGeneralConfig($config)); // Form elements are being disabled after parent::buildForm because: // 1: parent::buildForm creates the submit button @@ -179,7 +155,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['api_credentials_fieldset']['email']['#disabled'] = TRUE; $form['cloudflare_config']['client_ip_restore_enabled']['#disabled'] = TRUE; $form['cloudflare_config']['bypass_host']['#disabled'] = TRUE; - $form['cloudflare_config']['bypass_host']['#disabled'] = TRUE; $form['actions']['submit']['#disabled'] = TRUE; } @@ -189,10 +164,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { /** * Builds credentials section for inclusion in the settings form. * + * @param \Drupal\Core\Config\Config $config + * The readonly configuration. + * * @return array * Form Api render array with credentials section. */ - protected function buildApiCredentialsSection() { + protected function buildApiCredentialsSection(Config $config) { $section = []; $section['api_credentials_fieldset'] = [ @@ -203,13 +181,13 @@ protected function buildApiCredentialsSection() { '#type' => 'textfield', '#title' => $this->t('CloudFlare API Key'), '#description' => $this->t('Your API key. Get it at cloudflare.com/a/account/my-account.'), - '#default_value' => $this->config->get('apikey'), + '#default_value' => $config->get('apikey'), '#required' => TRUE, ]; $section['api_credentials_fieldset']['email'] = [ '#type' => 'textfield', '#title' => $this->t('Account e-mail address'), - '#default_value' => $this->config->get('email'), + '#default_value' => $config->get('email'), '#required' => TRUE, ]; @@ -219,10 +197,13 @@ protected function buildApiCredentialsSection() { /** * Builds zone selection section for inclusion in the settings form. * + * @param \Drupal\Core\Config\Config $config + * The readonly configuration. + * * @return array * Form Api render array with selection section. */ - protected function buildZoneSelectSection() { + protected function buildZoneSelectSection(Config $config) { $section = []; $section['zone_selection_fieldset'] = [ @@ -231,18 +212,31 @@ protected function buildZoneSelectSection() { '#weight' => 0, ]; - if ($this->hasZoneId) { - $zone_id = $this->config->get('zone_id'); - $description = $this->t('To change the current zone click the "Next" button below.'); - foreach ($this->zones as $zone) { + $zone_id = $config->get('zone_id'); + if (!empty($zone_id)) { + // Get the zones. + $zones = []; + if ($config->get('valid_credentials') === TRUE && $this->cloudFlareComposerDependenciesMet) { + try { + $zones = $this->zoneApi->listZones(); + } + catch (CloudFlareTimeoutException $e) { + drupal_set_message($this->t('Unable to connect to CloudFlare in order to validate credentials. Connection timed out. Please try again later.'), 'error'); + } + } + + // Find this zone_id. + foreach ($zones as $zone) { if ($zone->getZoneId() == $zone_id) { $zone_text = $zone->getName(); + break; } } - } + $description = $this->t('To change the current zone click the "Next" button below.'); + } else { - $zone_text = "No Zone Selected"; + $zone_text = $this->t('No Zone Selected'); $description = $this->t('No zone has been selected. Enter valid Api credentials then click next.'); } @@ -260,10 +254,13 @@ protected function buildZoneSelectSection() { /** * Builds general config section for inclusion in the settings form. * + * @param \Drupal\Core\Config\Config $config + * The readonly configuration. + * * @return array - * Form Api render array with selection section. + * Form API render array with selection section. */ - protected function buildGeneralConfig() { + protected function buildGeneralConfig(Config $config) { $section = []; $section['cloudflare_config'] = [ @@ -275,14 +272,14 @@ protected function buildGeneralConfig() { '#type' => 'checkbox', '#title' => $this->t('Restore Client Ip Address'), '#description' => $this->t('CloudFlare operates as a reverse proxy and replaces the client IP address. This setting will restore it.
Read more here.'), - '#default_value' => $this->config->get('client_ip_restore_enabled'), + '#default_value' => $config->get('client_ip_restore_enabled'), ]; $section['cloudflare_config']['bypass_host'] = [ '#type' => 'textfield', '#title' => $this->t('Host to Bypass CloudFlare'), - '#description' => $this->t('Optional: You can specify a host(no http/https) used for authenticated users to edit the site that bypasses CloudFlare.
This will help suppress log warnings regarding requests bypassing CloudFlare.'), - '#default_value' => $this->config->get('bypass_host'), + '#description' => $this->t('Optional: Specify a host (no http/https) used for authenticated users to edit the site that bypasses CloudFlare.
This will help suppress log warnings regarding requests bypassing CloudFlare.'), + '#default_value' => $config->get('bypass_host'), ]; return $section; @@ -292,24 +289,22 @@ protected function buildGeneralConfig() { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { + // Get the email address and apikey. $email = trim($form_state->getValue('email')); $apikey = trim($form_state->getValue('apikey')); - $bypass_host = trim($form_state->getValue('bypass_host')); - $is_email_valid = $this->emailValidator->isValid($email); - if (!$is_email_valid) { + // Validate the email address. + if (!$this->emailValidator->isValid($email)) { $form_state->setErrorByName('email', $this->t('Please enter a valid e-mail address.')); return; } try { - // Simply using this call to confirm that the credentials can authenticate - // against the CloudFlareApi. An exception here tell us the credentials - // are invalid. + // Confirm that the credentials can authenticate with the CloudFlareApi. $this->zoneApi->assertValidCredentials($apikey, $email, $this->cloudFlareComposerDependenciesCheck, $this->state); } catch (CloudFlareTimeoutException $e) { - $message = $this->t('Unable to connect to CloudFlare in order to validate credentials. Connection timed out. Please try again later.'); + $message = $this->t('Unable to connect to CloudFlare in order to validate credentials. Connection timed out. Please try again later.'); $form_state->setErrorByName('apikey', $message); $this->logger->error($message); return; @@ -323,24 +318,24 @@ public function validateForm(array &$form, FormStateInterface $form_state) { return; } - try { - $has_http_or_https = strpos($bypass_host, 'http') > -1; - if ($has_http_or_https) { + // Validate the bypass host. + $bypass_host = trim($form_state->getValue('bypass_host')); + if (!empty($bypass_host)) { + // Validate the bypass host does not begin with http. + if (strpos($bypass_host, 'http') > -1) { $form_state->setErrorByName('$bypass_host', $this->t('Please enter a host without http/https')); + return; } - // Quick and easy way to validate the domain. - if (!empty($bypass_host)) { - $bypass_uri = 'http://' . $bypass_host; - Url::fromUri($bypass_uri); + // Validate the host domain. + try { + Url::fromUri("http://$bypass_host"); + } + catch (\InvalidArgumentException $e) { + $form_state->setErrorByName('bypass_host', $this->t('You have entered an invalid host.')); + return; } } - catch (InvalidArgumentException $e) { - $form_state->setErrorByName('bypass_host', $this->t('You have entered an invalid host.')); - return; - } - - parent::validateForm($form, $form_state); } /** @@ -350,17 +345,18 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $api_key = trim($form_state->getValue('apikey')); $email = trim($form_state->getValue('email')); - // Deslash the host url. + // Deslash the host URL. $bypass_host = trim(rtrim($form_state->getValue('bypass_host'), "/")); $client_ip_restore_enabled = $form_state->getValue('client_ip_restore_enabled'); - $this->config->set('apikey', $api_key) + $config = $this->configFactory->getEditable('cloudflare.settings'); + $config + ->set('apikey', $api_key) ->set('email', $email) ->set('valid_credentials', TRUE) ->set('bypass_host', $bypass_host) ->set('client_ip_restore_enabled', $client_ip_restore_enabled); - - $this->config->save(); + $config->save(); } } diff --git a/src/Form/ZoneSelectionForm.php b/src/Form/ZoneSelectionForm.php index fd54c5a..3f126a0 100644 --- a/src/Form/ZoneSelectionForm.php +++ b/src/Form/ZoneSelectionForm.php @@ -21,6 +21,28 @@ * @package Drupal\cloudflare\Form */ class ZoneSelectionForm extends FormBase implements ContainerInjectionInterface { + + /** + * The configuration factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * Wrapper to access the CloudFlare zone api. + * + * @var \Drupal\cloudflare\CloudFlareZoneInterface + */ + protected $zoneApi; + + /** + * A logger instance for CloudFlare. + * + * @var \Psr\Log\LoggerInterface + */ + protected $logger; + /** * List of the zones for the current Api credentials. * @@ -28,6 +50,13 @@ class ZoneSelectionForm extends FormBase implements ContainerInjectionInterface */ protected $zones; + /** + * Boolean indicates if CloudFlare dependencies have been met. + * + * @var bool + */ + protected $cloudFlareComposerDependenciesMet; + /** * Tracks if the current CloudFlare account has multiple zones. * @@ -54,7 +83,7 @@ public static function create(ContainerInterface $container) { /** * Constructs a new ZoneSelectionForm. * - * @param \Drupal\Core\Config\ConfigFactoryInterface $config + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The factory for configuration objects. * @param \Drupal\cloudflare\CloudFlareZoneInterface $zone_api * ZoneApi instance for accessing api. @@ -63,9 +92,9 @@ public static function create(ContainerInterface $container) { * @param bool $composer_dependencies_met * Checks that the composer dependencies for CloudFlare are met. */ - public function __construct(ConfigFactoryInterface $config, CloudFlareZoneInterface $zone_api, LoggerInterface $logger, $composer_dependencies_met) { - $this->configFactory = $config; - $this->config = $config->getEditable('cloudflare.settings'); + public function __construct(ConfigFactoryInterface $config_factory, CloudFlareZoneInterface $zone_api, LoggerInterface $logger, $composer_dependencies_met) { + $this->configFactory = $config_factory; + $this->config = $config_factory->getEditable('cloudflare.settings'); $this->zoneApi = $zone_api; $this->logger = $logger; $this->cloudFlareComposerDependenciesMet = $composer_dependencies_met; @@ -81,7 +110,7 @@ public function __construct(ConfigFactoryInterface $config, CloudFlareZoneInterf $this->hasMultipleZones = count($this->zones) > 1; } catch (CloudFlareTimeoutException $e) { - drupal_set_message("Unable to connect to CloudFlare. You will not be able to change the selected Zone.", 'error'); + drupal_set_message($this->t('Unable to connect to CloudFlare. You will not be able to change the selected Zone.'), 'error'); } } } @@ -209,6 +238,7 @@ public function autocompleteZone(Request $request) { $matches = []; // Tracks if the current CloudFlare account has multiple zones. + /** @var \CloudFlarePhpSdk\ApiTypes\Zone\Zone $zone */ foreach ($this->zoneApi->listZones() as $zone) { if (stripos($zone->getName(), $zone_autocomplete_text) === 0) { $matches[] = ['value' => $zone->getZoneId(), 'label' => $zone->getName()]; diff --git a/src/Timestamp.php b/src/Timestamp.php index c5121f4..6ba7ba0 100644 --- a/src/Timestamp.php +++ b/src/Timestamp.php @@ -2,8 +2,6 @@ namespace Drupal\cloudflare; -use DateTime; - /** * Timestamp class to get datetime. * @@ -15,7 +13,7 @@ class Timestamp implements CloudFlareTimestampInterface { * {@inheritdoc} */ public function now() { - return new DateTime(); + return new \DateTime(); } } diff --git a/src/Zone.php b/src/Zone.php index 58ff896..74e7768 100644 --- a/src/Zone.php +++ b/src/Zone.php @@ -77,10 +77,10 @@ class Zone implements CloudFlareZoneInterface { /** * {@inheritdoc} */ - public static function create(ConfigFactoryInterface $config, LoggerInterface $logger, CacheBackendInterface $cache, CloudFlareStateInterface $state, CloudFlareComposerDependenciesCheckInterface $check_interface) { - $cf_config = $config->get('cloudflare.settings'); - $api_key = $cf_config->get('apikey'); - $email = $cf_config->get('email'); + public static function create(ConfigFactoryInterface $config_factory, LoggerInterface $logger, CacheBackendInterface $cache, CloudFlareStateInterface $state, CloudFlareComposerDependenciesCheckInterface $check_interface) { + $config = $config_factory->get('cloudflare.settings'); + $api_key = $config->get('apikey'); + $email = $config->get('email'); // If someone has not correctly installed composer here is where we need to // handle it to prevent PHP error. @@ -93,7 +93,7 @@ public static function create(ConfigFactoryInterface $config, LoggerInterface $l } return new static( - $config, + $config_factory, $logger, $cache, $state, @@ -105,8 +105,8 @@ public static function create(ConfigFactoryInterface $config, LoggerInterface $l /** * Zone constructor. * - * @param \Drupal\Core\Config\ConfigFactoryInterface $config - * CloudFlare config object. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The configuration factory. * @param \Psr\Log\LoggerInterface $logger * A logger instance. * @param \Drupal\Core\Cache\CacheBackendInterface $cache @@ -118,8 +118,8 @@ public static function create(ConfigFactoryInterface $config, LoggerInterface $l * @param \Drupal\cloudflare\CloudFlareComposerDependenciesCheckInterface $check_interface * Checks that composer dependencies are met. */ - public function __construct(ConfigFactoryInterface $config, LoggerInterface $logger, CacheBackendInterface $cache, CloudFlareStateInterface $state, $zone_api, CloudFlareComposerDependenciesCheckInterface $check_interface) { - $this->config = $config->get('cloudflare.settings'); + public function __construct(ConfigFactoryInterface $config_factory, LoggerInterface $logger, CacheBackendInterface $cache, CloudFlareStateInterface $state, $zone_api, CloudFlareComposerDependenciesCheckInterface $check_interface) { + $this->config = $config_factory->get('cloudflare.settings'); $this->logger = $logger; $this->cache = $cache; $this->state = $state; From 5e8ced055e624d709f29834294d113e22fd439aa Mon Sep 17 00:00:00 2001 From: Doug Green Date: Thu, 8 Feb 2018 10:15:44 -0500 Subject: [PATCH 2/2] Remove PHP5.6 from tests --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2fa4bca..97d35e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ language: php sudo: false php: - - 5.6 - 7.0 - 7.1 @@ -57,4 +56,4 @@ install: script: - ./run-tests.sh - - composer phpcs \ No newline at end of file + - composer phpcs