Commit 3c1d5959 authored by gbyte.co's avatar gbyte.co

Clean up code and docblocks

parent bf842b80
......@@ -13,8 +13,7 @@ class Batch {
private $batch;
private $batchInfo;
const PATH_DOES_NOT_EXIST = "The path @faulty_path has been omitted from the XML sitemap, as it does not exist.";
const PATH_DOES_NOT_EXIST_OR_NO_ACCESS = "The path @faulty_path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users.";
const PATH_DOES_NOT_EXIST_OR_NO_ACCESS = "The path @path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users.";
const BATCH_INIT_MESSAGE = 'Initializing batch...';
const BATCH_ERROR_MESSAGE = 'An error has occurred. This may result in an incomplete XML sitemap.';
const BATCH_PROGRESS_MESSAGE = 'Processing @current out of @total link types.';
......@@ -148,7 +147,7 @@ class Batch {
foreach ($entities as $entity_id => $entity) {
if (self::isBatch($batch_info)) {
self::setCurrentId($entity_id, $context); //todo: move outside of this loop
self::setCurrentId($entity_id, $context);
}
// Overriding entity settings if it has been overridden on entity edit page...
......@@ -244,8 +243,9 @@ class Batch {
if (self::isBatch($batch_info)) {
self::setCurrentId($i, $context);
}
if (!\Drupal::service('path.validator')->isValid($custom_path['path'])) { //todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush.
self::registerError(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS, ['@faulty_path' => $custom_path['path']], 'warning');
self::registerError(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS, ['@path' => $custom_path['path']], 'warning');
continue;
}
$url_object = Url::fromUserInput($custom_path['path'], ['absolute' => TRUE]);
......@@ -308,14 +308,14 @@ class Batch {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['sandbox']['max'] = $max;
$context['results']['processed_paths'] = !empty($context['results']['processed_paths']) ? $context['results']['processed_paths'] : [];
$context['results']['processed_paths'] = !empty($context['results']['processed_paths'])
? $context['results']['processed_paths'] : [];
}
}
private static function setCurrentId($id, &$context) {
$context['sandbox']['progress']++;
$context['sandbox']['current_id'] = $id;
$context['results']['link_count'] = !isset($context['results']['link_count']) ? 1 : $context['results']['link_count'] + 1; //Not used ATM.
}
private static function setProgressInfo(&$context) {
......@@ -342,7 +342,8 @@ class Batch {
if (count($chunk_links) == $batch_info['max_links']) {
$remove_sitemap = empty($context['results']['chunk_count']);
\Drupal::service('simple_sitemap.sitemap_generator')->generateSitemap($chunk_links, $remove_sitemap);
$context['results']['chunk_count'] = !isset($context['results']['chunk_count']) ? 1 : $context['results']['chunk_count'] + 1;
$context['results']['chunk_count'] = !isset($context['results']['chunk_count'])
? 1 : $context['results']['chunk_count'] + 1;
$context['results']['generate'] = array_slice($context['results']['generate'], count($chunk_links));
}
}
......
......@@ -39,7 +39,7 @@ class SimplesitemapController extends ControllerBase {
*
* @throws NotFoundHttpException
*
* @return object Response
* @return object
* Returns an XML response.
*/
public function getSitemap($chunk_id = NULL) {
......
......@@ -201,7 +201,8 @@ class Form {
/**
* Gets the object entity of the form if available.
*
* @return object $entity or FALSE if non-existent or if form operation is
* @return object|false
* Entity or FALSE if non-existent or if form operation is
* 'delete'.
*/
private function getFormEntity() {
......@@ -218,7 +219,8 @@ class Form {
* Gets new entity Id after entity creation.
* To be used in an entity form submit.
*
* @return int entity ID.
* @return int
* Entity ID.
*/
public function getFormEntityId() {
return $this->formState->getFormObject()->getEntity()->id();
......
......@@ -81,7 +81,7 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
$form['simple_sitemap_settings']['advanced']['skip_untranslated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip non-existent translations'),
'#description' => $this->t('If unchecked, the sitemap will include links to all content translation variants, even when the content has not been translated yet. If checked, only links to the translated content are included.'),
'#description' => $this->t('If checked, only links to the translated content will be included, otherwise the sitemap will include links to all content translation variants, even when the content has not been translated yet.'),
'#default_value' => $generator->getSetting('skip_untranslated', FALSE),
];
......@@ -101,7 +101,7 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
'#maxlength' => 5,
'#size' => 5,
'#default_value' => $generator->getSetting('batch_process_limit', 1500),
'#required' => TRUE, //TODO: test
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
......@@ -114,13 +114,13 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
$max_links = $form_state->getValue('max_links');
if ($max_links != '') {
if (!is_numeric($max_links) || $max_links < 1 || $max_links != round($max_links)) {
$form_state->setErrorByName('', $this->t("The value of the <em>Maximum links in a sitemap</em> field must be empty, or a positive integer greater than 0."));
$form_state->setErrorByName('max_links', $this->t("The value of the <em>Maximum links in a sitemap</em> field must be empty, or a positive integer greater than 0."));
}
}
$batch_process_limit = $form_state->getValue('batch_process_limit');
if (!is_numeric($batch_process_limit) || $batch_process_limit < 1 || $batch_process_limit != round($batch_process_limit)) {
$form_state->setErrorByName('', $this->t("The value of the <em>Refresh batch every n links</em> field must be a positive integer greater than 0."));
$form_state->setErrorByName('batch_process_limit', $this->t("The value of the <em>Refresh batch every n links</em> field must be a positive integer greater than 0."));
}
}
......@@ -136,7 +136,6 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
}
public function generateSitemap(array &$form, FormStateInterface $form_state) {
$generator = \Drupal::service('simple_sitemap.generator');
$generator->generateSitemap();
\Drupal::service('simple_sitemap.generator')->generateSitemap();
}
}
......@@ -83,8 +83,7 @@ class Simplesitemap {
* @param string $entity_type_id
* Entity type id like 'node'.
*
* @return bool
* TRUE if entity type has been enabled, FALSE if it was not.
* @return $this
*/
public function enableEntityType($entity_type_id) {
$entity_types = $this->getConfig('entity_types');
......@@ -103,8 +102,7 @@ class Simplesitemap {
* @param string $entity_type_id
* Entity type id like 'node'.
*
* @return bool
* TRUE if entity type has been disabled, FALSE if it was not.
* @return $this
*/
public function disableEntityType($entity_type_id) {
$entity_types = $this->getConfig('entity_types');
......@@ -140,9 +138,9 @@ class Simplesitemap {
/**
* Overrides entity bundle/entity type sitemap settings for a single entity.
*
* @param $entity_type_id
* @param $id
* @param $settings
* @param string $entity_type_id
* @param int $id
* @param array $settings
* @return $this
*/
public function setEntityInstanceSettings($entity_type_id, $id, $settings) {
......@@ -173,9 +171,9 @@ class Simplesitemap {
/**
* Gets sitemap settings for an entity bundle or a non-bundle entity type.
*
* @param $entity_type_id
* @param null $bundle_name
* @return bool
* @param string $entity_type_id
* @param string|null $bundle_name
* @return array|false
*/
public function getBundleSettings($entity_type_id, $bundle_name = NULL) {
$bundle_name = is_null($bundle_name) ? $entity_type_id : $bundle_name;
......@@ -216,9 +214,9 @@ class Simplesitemap {
* Gets sitemap settings for an entity instance which overrides bundle
* settings.
*
* @param $entity_type_id
* @param $id
* @return bool
* @param string $entity_type_id
* @param int $id
* @return array
*/
public function getEntityInstanceSettings($entity_type_id, $id) {
$entity_types = $this->getConfig('entity_types');
......@@ -235,15 +233,16 @@ class Simplesitemap {
/**
* Adds a custom path to the sitemap settings.
*
* @param $path
* @param $settings
* @return $this|bool
* @param string $path
* @param array $settings
* @return $this
*/
public function addCustomLink($path, $settings) {
if (!\Drupal::service('path.validator')->isValid($path))
return FALSE; // todo: log error
return $this; // todo: log error
if ($path[0] != '/')
return FALSE; // todo: log error
return $this; // todo: log error
$custom_links = $this->getConfig('custom');
foreach($custom_links as $key => $link) {
if ($link['path'] == $path) {
......@@ -261,8 +260,8 @@ class Simplesitemap {
/**
* Returns settings for a custom path added to the sitemap settings.
*
* @param $path
* @return bool
* @param string $path
* @return array|false
*/
public function getCustomLink($path) {
$custom_links = $this->getConfig('custom');
......@@ -277,7 +276,7 @@ class Simplesitemap {
/**
* Removes a custom path from the sitemap settings.
*
* @param $path
* @param string $path
* @return $this
*/
public function removeCustomLink($path) {
......@@ -294,6 +293,8 @@ class Simplesitemap {
/**
* Removes all custom paths from the sitemap settings.
*
* @return $this
*/
public function removeCustomLinks() {
$this->saveConfig('custom', []);
......@@ -333,7 +334,7 @@ class Simplesitemap {
*
* @param int $chunk_id
*
* @return string $sitemap
* @return string|false
* If no sitemap id provided, either a sitemap index is returned, or the
* whole sitemap, if the amount of links does not exceed the max links setting.
* If a sitemap id is provided, a sitemap chunk is returned.
......@@ -392,7 +393,7 @@ class Simplesitemap {
* Name of the setting, like 'max_links'.
*
* @param mixed $default
* Value to be returned if the setting does not exist in the conifuration.
* Value to be returned if the setting does not exist in the configuration.
*
* @return mixed
* The current setting from db or a default value.
......@@ -405,9 +406,9 @@ class Simplesitemap {
/**
* Saves a specific sitemap setting to db.
*
* @param $name
* @param string $name
* Setting name, like 'max_links'.
* @param $setting
* @param mixed $setting
* The setting to be saved.
*
* @return $this
......@@ -422,7 +423,7 @@ class Simplesitemap {
/**
* Returns a 'time ago' string of last timestamp generation.
*
* @return mixed
* @return string|false
* Formatted timestamp of last sitemap generation, otherwise FALSE.
*/
public function getGeneratedAgo() {
......
......@@ -59,7 +59,7 @@ class SitemapGenerator {
/**
* Returns a batch-ready data array for custom link generation.
*
* @return array $paths
* @return array
* Data to be processed.
*/
private function getCustomUrlsData() {
......@@ -76,7 +76,7 @@ class SitemapGenerator {
* Collects entity metadata for entities that are set to be indexed
* and returns an array of batch-ready data sets for entity link generation.
*
* @return array $data_sets.
* @return array
*/
private function getEntityTypeData() {
$data_sets = [];
......@@ -158,7 +158,8 @@ class SitemapGenerator {
* @param array $links
* All links with their multilingual versions and settings.
*
* @return string sitemap chunk
* @return string
* Sitemap chunk
*/
private function generateSitemapChunk($links) {
$writer = new XMLWriter();
......
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