Commit 0217d868 authored by gbyte.co's avatar gbyte.co

Change private members to protected, improve type hinting, clean up

parent 2dcacab1
......@@ -25,8 +25,11 @@ function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
*/
function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/**
* @var Drupal\simple_sitemap\Form\FormHelper $f
*/
$f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
if (!$f->alteringForm) {
if (!$f->alteringForm()) {
return;
}
......@@ -34,12 +37,12 @@ function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form
'#type' => 'details',
'#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
'#title' => t('Simple XML sitemap'),
'#description' => $f->entityCategory == 'instance' ? t('Settings for this entity can be overridden here.') : '',
'#description' => $f->getEntityCategory() == 'instance' ? t('Settings for this entity can be overridden here.') : '',
];
// Attach some js magic to forms.
// todo: JS not working on comment entity form, hence disabling.
if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') {
if ($f->getEntityTypeId() != 'comment' || $f->getEntityCategory() != 'instance') {
$form['#attached']['library'][] = 'simple_sitemap/form';
}
......@@ -73,6 +76,9 @@ function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form
*/
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
/**
* @var Drupal\simple_sitemap\Form\FormHelper $f
*/
$f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
$values = $form_state->getValues();
......@@ -82,24 +88,34 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
// Only make changes in DB if sitemap settings actually changed.
if ($f->valuesChanged($form, $values)) {
/**
* @var \Drupal\simple_sitemap\Simplesitemap $generator
*/
$generator = \Drupal::service('simple_sitemap.generator');
switch ($f->entityCategory) {
switch ($f->getEntityCategory()) {
case 'bundle':
$f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId();
$generator->setBundleSettings($f->entityTypeId, $f->bundleName, [
'index' => $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority']
]);
$generator->setBundleSettings(
$f->getEntityTypeId(),
!empty($f->getBundleName()) ? $f->getBundleName() : $f->getFormEntityId(),
[
'index' => $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority']
]
);
break;
case 'instance':
$f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId();
$generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, [
'index' => $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority']
]);
$generator->setEntityInstanceSettings(
$f->getEntityTypeId(),
!empty($f->getInstanceId()) ? $f->getInstanceId() : $f->getFormEntityId(),
[
'index' => $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority']
]
);
break;
}
......@@ -114,6 +130,10 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
* Implements hook_cron.
*/
function simple_sitemap_cron() {
/**
* @var \Drupal\simple_sitemap\Simplesitemap $generator
*/
$generator = \Drupal::service('simple_sitemap.generator');
if ($generator->getSetting('cron_generate')) {
$generator->generateSitemap('backend');
......@@ -128,8 +148,14 @@ function simple_sitemap_cron() {
* @param \Drupal\Core\Entity\EntityInterface $entity
*/
function simple_sitemap_entity_delete(EntityInterface $entity) {
\Drupal::service('simple_sitemap.generator')
->removeEntityInstanceSettings($entity->getEntityTypeId(), $entity->id());
/**
* @var \Drupal\simple_sitemap\Simplesitemap $generator
*/
$generator = \Drupal::service('simple_sitemap.generator');
$generator->removeEntityInstanceSettings(
$entity->getEntityTypeId(), $entity->id()
);
}
/**
......@@ -161,6 +187,10 @@ function simple_sitemap_menu_delete(MenuInterface $menu) {
* @param string $bundle
*/
function simple_sitemap_delete_bundle_config($entity_type_id, $bundle) {
/**
* @var \Drupal\simple_sitemap\Simplesitemap $generator
*/
$generator = \Drupal::service('simple_sitemap.generator');
$deleted_bundle_settings = $generator->getBundleSettings($entity_type_id, $bundle);
if ($deleted_bundle_settings !== FALSE) {
......
......@@ -14,12 +14,12 @@ class Batch {
/**
* @var array
*/
private $batch;
protected $batch;
/**
* @var array
*/
private $batchInfo;
protected $batchInfo;
const BATCH_TITLE = 'Generating XML sitemap';
const BATCH_INIT_MESSAGE = 'Initializing batch...';
......
......@@ -233,7 +233,7 @@ class BatchUrlGenerator {
'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($custom_path['priority']) ? $custom_path['priority'] : NULL,
];
if (!is_null($entity)) {
if (NULL !== $entity) {
$path_data['entity_info'] = ['entity_type' => $entity->getEntityTypeId(), 'id' => $entity->id()];
}
$this->addUrlVariants($url_object, $path_data, $entity);
......@@ -265,7 +265,7 @@ class BatchUrlGenerator {
* @param $entity_info
* @return mixed
*/
private function getBatchIterationEntities($entity_info) {
protected function getBatchIterationEntities($entity_info) {
$query = $this->entityQuery->get($entity_info['entity_type_name']);
if (!empty($entity_info['keys']['id'])) {
......@@ -294,9 +294,9 @@ class BatchUrlGenerator {
/**
* @param array $custom_paths
* @return mixed
* @return array
*/
private function getBatchIterationCustomPaths(array $custom_paths) {
protected function getBatchIterationCustomPaths(array $custom_paths) {
if ($this->needsInitialization()) {
$this->initializeBatch(count($custom_paths));
......@@ -314,14 +314,14 @@ class BatchUrlGenerator {
* @param $path_data
* @param $entity
*/
private function addUrlVariants($url_object, $path_data, $entity) {
protected function addUrlVariants($url_object, $path_data, $entity) {
$alternate_urls = [];
$translation_languages = !is_null($entity) && $this->batchInfo['skip_untranslated']
$translation_languages = NULL !== $entity && $this->batchInfo['skip_untranslated']
? $entity->getTranslationLanguages() : $this->languages;
// Entity is not translated.
if (!is_null($entity) && isset($translation_languages['und'])) {
if (NULL !== $entity && isset($translation_languages['und'])) {
if ($url_object->access($this->anonUser)) {
$url_object->setOption('language', $this->languages[$this->defaultLanguageId]);
$alternate_urls[$this->defaultLanguageId] = $this->replaceBaseUrlWithCustom($url_object->toString());
......@@ -329,7 +329,7 @@ class BatchUrlGenerator {
}
else {
// Including only translated variants of entity.
if (!is_null($entity) && $this->batchInfo['skip_untranslated']) {
if (NULL !== $entity && $this->batchInfo['skip_untranslated']) {
foreach ($translation_languages as $language) {
$translation = $entity->getTranslation($language->getId());
if ($translation->access('view', $this->anonUser)) {
......@@ -429,7 +429,7 @@ class BatchUrlGenerator {
* @param $url_object
* @return object|null
*/
private function getEntityFromUrlObject($url_object) {
protected function getEntityFromUrlObject($url_object) {
$route_parameters = $url_object->getRouteParameters();
return !empty($route_parameters) && $this->entityTypeManager
->getDefinition($entity_type_id = key($route_parameters), FALSE)
......@@ -438,7 +438,11 @@ class BatchUrlGenerator {
: NULL;
}
private function replaceBaseUrlWithCustom($url) {
/**
* @param string $url
* @return string
*/
protected function replaceBaseUrlWithCustom($url) {
return !empty($this->batchInfo['base_url'])
? str_replace($GLOBALS['base_url'], $this->batchInfo['base_url'], $url)
: $url;
......
......@@ -15,7 +15,7 @@ class EntityHelper {
/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
protected $entityTypeManager;
/**
* EntityHelper constructor.
......
This diff is collapsed.
......@@ -97,7 +97,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
* @param $custom_links_string
* @return array
*/
private function getCustomLinksFromString($custom_links_string) {
protected function getCustomLinksFromString($custom_links_string) {
// Unify newline characters and explode into array.
$custom_links_string_lines = explode("\n", str_replace("\r\n", "\n", $custom_links_string));
// Remove empty values and whitespaces from array.
......
......@@ -14,7 +14,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
/**
* @var array
*/
private $formSettings = [
protected $formSettings = [
'max_links',
'cron_generate',
'remove_duplicates',
......
......@@ -19,42 +19,42 @@ class Simplesitemap {
/**
* @var \Drupal\simple_sitemap\SitemapGenerator
*/
private $sitemapGenerator;
protected $sitemapGenerator;
/**
* @var \Drupal\simple_sitemap\EntityHelper
*/
private $entityHelper;
protected $entityHelper;
/**
* @var \Drupal\Core\Config\ConfigFactory
*/
private $configFactory;
protected $configFactory;
/**
* @var \Drupal\Core\Database\Connection
*/
private $db;
protected $db;
/**
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
private $entityQuery;
protected $entityQuery;
/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
protected $entityTypeManager;
/**
* @var \Drupal\Core\Path\PathValidator
*/
private $pathValidator;
protected $pathValidator;
/**
* @var array
*/
private static $allowed_link_settings = [
protected static $allowed_link_settings = [
'entity' => ['index', 'priority'],
'custom' => ['priority'],
];
......@@ -95,7 +95,7 @@ class Simplesitemap {
*
* @return string
*/
private function fetchSitemapChunks() {
protected function fetchSitemapChunks() {
return $this->db
->query("SELECT * FROM {simple_sitemap}")
->fetchAllAssoc('id');
......@@ -185,6 +185,8 @@ class Simplesitemap {
if (isset($sitemap_entity_types[$entity_type_id])) {
$entity_type = $sitemap_entity_types[$entity_type_id];
$keys = $entity_type->getKeys();
// Menu fix.
$keys['bundle'] = $entity_type_id == 'menu_link_content' ? 'menu_name' : $keys['bundle'];
$query = $this->entityQuery->get($entity_type_id);
......@@ -240,7 +242,7 @@ class Simplesitemap {
* False if entity type does not exist.
*/
public function getBundleSettings($entity_type_id = NULL, $bundle_name = NULL) {
if (null !== $entity_type_id) {
if (NULL !== $entity_type_id) {
$bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
$settings = $this->configFactory
->get("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
......@@ -352,7 +354,7 @@ class Simplesitemap {
public function removeEntityInstanceSettings($entity_type_id, $entity_ids = NULL) {
$query = $this->db->delete('simple_sitemap_entity_overrides')
->condition('entity_type', $entity_type_id);
if (!is_null($entity_ids)) {
if (NULL !== $entity_ids) {
$entity_ids = !is_array($entity_ids) ? [$entity_ids] : $entity_ids;
$query->condition('entity_id', $entity_ids, 'IN');
}
......@@ -421,7 +423,7 @@ class Simplesitemap {
/**
*
*/
private function addLinkSettings($type, $settings, &$target) {
protected function addLinkSettings($type, $settings, &$target) {
foreach ($settings as $setting_key => $setting) {
if (in_array($setting_key, self::$allowed_link_settings[$type])) {
switch ($setting_key) {
......@@ -513,7 +515,7 @@ class Simplesitemap {
*/
public function getSitemap($chunk_id = NULL) {
$chunks = $this->fetchSitemapChunks();
if (is_null($chunk_id) || !isset($chunks[$chunk_id])) {
if (NULL === $chunk_id || !isset($chunks[$chunk_id])) {
// Return sitemap index, if there are multiple sitemap chunks.
if (count($chunks) > 1) {
......@@ -556,7 +558,7 @@ class Simplesitemap {
* @return string
* The sitemap index.
*/
private function getSitemapIndex($chunks) {
protected function getSitemapIndex($chunks) {
return $this->sitemapGenerator
->setGenerator($this)
->generateSitemapIndex($chunks);
......@@ -579,7 +581,7 @@ class Simplesitemap {
$setting = $this->configFactory
->get('simple_sitemap.settings')
->get($name);
return !is_null($setting) ? $setting : $default;
return NULL !== $setting ? $setting : $default;
}
/**
......
......@@ -23,42 +23,42 @@ class SitemapGenerator {
/**
* @var \Drupal\simple_sitemap\Batch\Batch
*/
private $batch;
protected $batch;
/**
* @var \Drupal\simple_sitemap\EntityHelper
*/
private $entityHelper;
protected $entityHelper;
/**
* @var \Drupal\Core\Database\Connection
*/
private $db;
protected $db;
/**
* @var \Drupal\Core\Extension\ModuleHandler
*/
private $moduleHandler;
protected $moduleHandler;
/**
* @var string
*/
private $defaultLanguageId;
protected $defaultLanguageId;
/**
* @var string
*/
private $generateFrom = 'form';
protected $generateFrom = 'form';
/**
* @var bool
*/
private $isHreflangSitemap;
protected $isHreflangSitemap;
/**
* @var \Drupal\simple_sitemap\Simplesitemap
*/
private $generator;
protected $generator;
/**
* SitemapGenerator constructor.
......@@ -131,7 +131,7 @@ class SitemapGenerator {
* @return array
* Data to be processed.
*/
private function getCustomUrlsData() {
protected function getCustomUrlsData() {
$paths = [];
foreach ($this->generator->getCustomLinks() as $i => $custom_path) {
$paths[$i]['path'] = $custom_path['path'];
......@@ -148,7 +148,7 @@ class SitemapGenerator {
*
* @return array
*/
private function getEntityTypeData() {
protected function getEntityTypeData() {
$data_sets = [];
$sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
$entity_types = $this->generator->getBundleSettings();
......@@ -240,7 +240,7 @@ class SitemapGenerator {
* @return string
* Sitemap chunk
*/
private function generateSitemapChunk(array $links) {
protected function generateSitemapChunk(array $links) {
$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(TRUE);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment