GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ba15e1...75ffe1 )
by Ronald
12:34
created

MinMaxDaysConstraint   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 145
wmc 29
lcom 1
cbo 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C applyConstraint() 0 31 12
D toString() 0 68 16
1
<?php
2
3
/**
4
 * @file
5
 * Class MinMaxConstraint
6
 */
7
8
namespace Roomify\Bat\Constraint;
9
10
use Roomify\Bat\Constraint;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Roomify\Bat\Constraint\Constraint.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
12
/**
13
 * Checks that a request is at least a set number of days and does not exceed a
14
 * set number of days.
15
 *
16
 */
17
class MinMaxDaysConstraint extends Constraint {
18
19
  /**
20
   * @var int
21
   */
22
  public $min_days = 0;
23
24
  /**
25
   * @var int
26
   */
27
  public $max_days = 0;
28
29
  /**
30
   * @var int
31
   */
32
  public $checkin_day = NULL;
33
34
  /**
35
   * @param $min_days
36
   * @param $max_days
37
   * @param $start_date
38
   * @param $end_date
39
   * @param $checkin_day
40
   */
41
  public function __construct($units, $min_days = 0, $max_days = 0, $start_date = NULL, $end_date = NULL, $checkin_day = NULL) {
42
    parent::__construct($units);
43
44
    $this->min_days = $min_days;
45
    $this->max_days = $max_days;
46
    $this->start_date = $start_date;
47
    $this->end_date = $end_date;
48
    $this->checkin_day = $checkin_day;
49
  }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54
  public function applyConstraint(&$calendar_response) {
55
    parent::applyConstraint($calendar_response);
56
57
    if ($this->start_date->getTimestamp() <= $calendar_response->getStartDate()->getTimestamp() &&
58
        $this->end_date->getTimestamp() >= $calendar_response->getEndDate()->getTimestamp() &&
59
        ($this->checkin_day === NULL || $this->checkin_day == $calendar_response->getStartDate()->format('N'))) {
60
61
      $units = $this->getUnits();
62
63
      $included_set = $calendar_response->getIncluded();
64
65
      foreach ($included_set as $unit_id => $set) {
66
        if (isset($units[$unit_id]) || empty($units)) {
67
          $start_date = $calendar_response->getStartDate();
68
          $end_date = $calendar_response->getEndDate();
69
70
          $diff = $end_date->diff($start_date)->days;
71
          if (is_numeric($this->min_days) && $diff < $this->min_days) {
72
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
73
74
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
75
          }
76
          elseif (is_numeric($this->max_days) && $diff > $this->max_days) {
77
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
78
79
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
80
          }
81
        }
82
      }
83
    }
84
  }
85
86
  /**
87
   * Generates a text describing an availability_constraint.
88
   *
89
   * @return string
90
   *   The formatted message.
91
   */
92
  public function toString() {
93
    $text = '';
94
95
    // Min/max stay length constraint variables.
96
    $minimum_stay = empty($this->min_days) ? '' : format_plural($this->min_days, '@count day', '@count days', array('@count' => $this->min_days));
97
    $maximum_stay = empty($this->max_days) ? '' : format_plural($this->max_days, '@count day', '@count days', array('@count' => $this->max_days));
98
99
    // Day of the week constraint variable.
100
    $day_of_the_week = rooms_availability_constraints_get_weekday($this->checkin_day);
101
102
    // Date range constraint variables.
103
    $start_date = $this->start_date->format('Y-m-d');
104
    $end_date = $this->end_date->format('Y-m-d');
105
106
    // Next create replacement placeholders to be used in t() below.
107
    $args = array(
108
      '@minimum_stay' => $minimum_stay,
109
      '@maximum_stay' => $maximum_stay,
110
      '@start_date' => $start_date,
111
      '@end_date' => $end_date,
112
      '@day_of_the_week' => $day_of_the_week,
113
    );
114
115
    // Finally, build out the constraint text string adding components
116
    // as necessary.
117
118
    // Specify a date range constraint.
119
    if ($start_date && $end_date) {
120
      $text = t('From @start_date to @end_date', $args);
121
    }
122
123
    // Specify the day of the week constraint.
124
    if ($day_of_the_week) {
125
      if ($start_date && $end_date) {
126
        $text = t('From @start_date to @end_date, if booking starts on @day_of_the_week', $args);
127
      }
128
      else {
129
        $text = t('If booking starts on @day_of_the_week', $args);
130
      }
131
    }
132
133
    // Specify the min/max stay length constraint.
134
    if ($minimum_stay || $maximum_stay) {
135
      if (empty($text)) {
136
        $text = t('The stay') . ' ';
137
      }
138
      else {
139
        $text .=   ' ' . t('the stay') . ' ';
140
      }
141
    }
142
    if ($minimum_stay && $maximum_stay) {
143
      // Special case when min stay and max stay are the same.
144
      if ($minimum_stay == $maximum_stay) {
145
        $text .= t('must be for @minimum_stay', $args);
146
      }
147
      else {
148
        $text .= t('must be at least @minimum_stay and at most @maximum_stay', $args);
149
      }
150
    }
151
    elseif ($minimum_stay) {
152
      $text .= t('must be for at least @minimum_stay', $args);
153
    }
154
    elseif ($maximum_stay) {
155
      $text .= t('cannot be more than @maximum_stay', $args);
156
    }
157
158
    return $text;
159
  }
160
161
}
162