Commit a5fa2594 authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #3219111: Clean code - Introduce constant OfficeHoursDateHelper::daysPerWeek

parent 80838288
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -12,6 +12,12 @@ use Drupal\Core\Datetime\DrupalDateTime;
 */
class OfficeHoursDateHelper extends DateHelper {

  /**
   * @var int
   */
  const daysPerWeek = 7;


  /**
   * Gets the day number of first day of the week.
   *
@@ -285,8 +291,12 @@ class OfficeHoursDateHelper extends DateHelper {
    $first_day = ($first_day == '') ? OfficeHoursDateHelper::getFirstDay() : $first_day;
    // Sort Weekdays; leave Exception days at bottom of list.
    foreach ($office_hours as $key => $item) {
      $new_office_hours[$key - (7 * (int) ($key >= $first_day))] = $item;
      $new_office_hours[$key - (OfficeHoursDateHelper::daysPerWeek * (int) ($key >= $first_day))] = $item;
    }
    // @todo Use pattern from extractFormValues(), avoiding key-change.
    //usort($values, function ($a, $b) {
    //  return SortArray::sortByKeyInt($a, $b, '_weight');
    //}
    ksort($new_office_hours);
    return $new_office_hours;
  }
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ trait OfficeHoursFormatterTrait {
    // Initialize days and times, using date_api as key (0=Sun, 6=Sat).
    // Empty days are not yet present in $items, and are now added in $days.
    $office_hours = [];
    for ($day = 0; $day < 7; $day++) {
    for ($day = 0; $day < OfficeHoursDateHelper::daysPerWeek; $day++) {
      $office_hours[$day] = ['startday' => $day] + $default_office_hours;
    }

+3 −2
Original line number Diff line number Diff line
@@ -381,7 +381,8 @@ abstract class OfficeHoursFormatterBase extends FormatterBase {
          if ($next['startday'] != $today) {
            // We will open tomorrow or later.
            $next_time = $start;
            $add_days = ($next['startday'] - $today + 7) % 7;
            $add_days = ($next['startday'] - $today + OfficeHoursDateHelper::daysPerWeek)
              % OfficeHoursDateHelper::daysPerWeek;
            break;
          }
          elseif ($start > $now) {
@@ -404,7 +405,7 @@ abstract class OfficeHoursFormatterBase extends FormatterBase {
              $first_time_slot_found = TRUE;

              $next_time = $start;
              $add_days = 7;
              $add_days = OfficeHoursDateHelper::daysPerWeek;
            }
            continue; // A new slot might come along.
          }
+5 −2
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ class OfficeHoursDefaultWidget extends OfficeHoursWidgetBase {
      ->getCardinality();
    if ($field_cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      $this->fieldDefinition->getFieldStorageDefinition()
        ->setCardinality($this->getFieldSetting('cardinality_per_day') * 7);
        ->setCardinality($this->getFieldSetting('cardinality_per_day') * OfficeHoursDateHelper::daysPerWeek);
    }

    $elements = parent::formMultipleElements($items, $form, $form_state);
@@ -83,7 +83,10 @@ class OfficeHoursDefaultWidget extends OfficeHoursWidgetBase {
    foreach ($days as $day) {
      // @todo The theme_function clears values above cardinality. Move it here.
      for ($day_delta = 0; $day_delta < $cardinality; $day_delta++) {
        $default_value = ['day' => $day, 'day_delta' => $day_delta];
        $default_value = [
          'day' => $day,
          'day_delta' => $day_delta,
        ];
        if (isset($indexed_items[$day][$day_delta])) {
          $default_value = $indexed_items[$day][$day_delta]->getValue() + $default_value;
        }
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\office_hours\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\office_hours\OfficeHoursDateHelper;

/**
 * Processes a input array of office hours to the correct format for the field.
@@ -107,7 +108,7 @@ class CSVOfficeHoursField extends OfficeHoursField {
    $delimiter = isset($this->configuration['delimiter']) ? $this->configuration['delimiter'] : '-';
    $has_comment = isset($this->configuration['comment']) ? $this->configuration['comment'] : FALSE;

    if (count($value) !== ($slots_per_day * ($has_comment ? 2 : 1) * 7)) {
    if (count($value) !== ($slots_per_day * ($has_comment ? 2 : 1) * OfficeHoursDateHelper::daysPerWeek)) {
      throw new MigrateException(sprintf('%s does not have the correct size', var_export($value, TRUE)));
    }