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 ( c9db31...f3227f )
by
unknown
03:02
created

MinMaxDaysConstraint::setCheckinDay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @file
5
 * Class MinMaxConstraint
6
 */
7
8
namespace Roomify\Bat\Constraint;
9
10
use Roomify\Bat\Calendar\CalendarResponse;
11
use Roomify\Bat\Constraint\Constraint;
12
13
/**
14
 * Checks that a request is at least a set number of days and does not exceed a
15
 * set number of days.
16
 *
17
 */
18
class MinMaxDaysConstraint extends Constraint {
19
20
  /**
21
   * @var int
22
   */
23
  protected $min_days = 0;
24
25
  /**
26
   * @var int
27
   */
28
  protected $max_days = 0;
29
30
  /**
31
   * @var int
32
   */
33
  protected $checkin_day = NULL;
34
35
  /**
36
   * @param $min_days
37
   * @param $max_days
38
   * @param $start_date
39
   * @param $end_date
40
   * @param $checkin_day
41
   */
42
  public function __construct($units, $min_days = 0, $max_days = 0, $start_date = NULL, $end_date = NULL, $checkin_day = NULL) {
43
    parent::__construct($units);
44
45
    $this->min_days = $min_days;
46
    $this->max_days = $max_days;
47
    $this->start_date = $start_date;
48
    $this->end_date = $end_date;
49
    $this->checkin_day = $checkin_day;
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function applyConstraint(CalendarResponse &$calendar_response) {
56
    parent::applyConstraint($calendar_response);
57
58
    if ($this->start_date === NULL) {
59
      $this->start_date = new \DateTime('1970-01-01');
60
    }
61
    if ($this->end_date === NULL) {
62
      $this->end_date = new \DateTime('2999-12-31');
63
    }
64
65
    if ( (($calendar_response->getStartDate()->getTimestamp() >= $this->start_date->getTimestamp() &&
66
           $calendar_response->getStartDate()->getTimestamp() <= $this->end_date->getTimestamp()) ||
67
          ($calendar_response->getEndDate()->getTimestamp() >= $this->start_date->getTimestamp() &&
68
           $calendar_response->getEndDate()->getTimestamp() <= $this->end_date->getTimestamp()) ||
69
          ($calendar_response->getStartDate()->getTimestamp() <= $this->start_date->getTimestamp() &&
70
           $calendar_response->getEndDate()->getTimestamp() >= $this->end_date->getTimestamp())) &&
71
         ($this->checkin_day === NULL || $this->checkin_day == $calendar_response->getStartDate()->format('N')) ) {
72
73
      $units = $this->getUnits();
74
75
      $included_set = $calendar_response->getIncluded();
76
77
      foreach ($included_set as $unit_id => $set) {
78
        if (isset($units[$unit_id]) || empty($units)) {
79
          $start_date = $calendar_response->getStartDate();
80
          $end_date = $calendar_response->getEndDate();
81
82
          $diff = $end_date->diff($start_date)->days;
83
          if (is_numeric($this->min_days) && $diff < $this->min_days) {
84
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
85
86
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
87
          } elseif (is_numeric($this->max_days) && $diff > $this->max_days) {
88
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
89
90
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
91
          }
92
        }
93
      }
94
    }
95
  }
96
97
  /**
98
   * Generates a text describing an availability_constraint.
99
   *
100
   * @return string
101
   *   The formatted message.
102
   */
103
  public function toString() {
104
    $text = '';
105
106
    // Min/max stay length constraint variables.
107
    $minimum_stay = empty($this->min_days) ? '' : (($this->min_days == 1) ? '@count day' : '@count days');
108
    $maximum_stay = empty($this->max_days) ? '' : (($this->max_days == 1) ? '@count day' : '@count days');
109
110
    // Day of the week constraint variable.
111
    $day_of_the_week = $this->getWeekDay($this->checkin_day);
112
113
    $start_date = FALSE;
114
    $end_date = FALSE;
115
116
    // Date range constraint variables.
117
    if ($this->start_date !== NULL) {
118
      $start_date = $this->start_date->format('Y-m-d');
119
    }
120
    if ($this->start_date !== NULL) {
121
      $end_date = $this->end_date->format('Y-m-d');
122
    }
123
124
    // Next create replacement placeholders to be used in t() below.
125
    $args = array(
126
      '@minimum_stay' => $minimum_stay,
127
      '@maximum_stay' => $maximum_stay,
128
      '@day_of_the_week' => $day_of_the_week,
129
    );
130
131
    // Finally, build out the constraint text string adding components
132
    // as necessary.
133
134
    // Specify a date range constraint.
135
    if ($start_date && $end_date) {
136
      $text = 'From @start_date to @end_date';
137
      $args['@start_date'] = $start_date;
138
      $args['@end_date'] = $end_date;
139
    }
140
141
    // Specify the day of the week constraint.
142
    if ($day_of_the_week) {
143
      if ($start_date && $end_date) {
144
        $text = 'From @start_date to @end_date, if booking starts on @day_of_the_week';
145
      } else {
146
        $text = 'If booking starts on @day_of_the_week';
147
      }
148
    }
149
150
    // Specify the min/max stay length constraint.
151
    if ($minimum_stay || $maximum_stay) {
152
      if (empty($text)) {
153
        $text = 'The stay ';
154
      } else {
155
        $text .= ' the stay ';
156
      }
157
    }
158
    if ($minimum_stay && $maximum_stay) {
159
      // Special case when min stay and max stay are the same.
160
      if ($minimum_stay == $maximum_stay) {
161
        $text .= 'must be for @minimum_stay';
162
      } else {
163
        $text .= 'must be at least @minimum_stay and at most @maximum_stay';
164
      }
165
    } elseif ($minimum_stay) {
166
      $text .= 'must be for at least @minimum_stay';
167
    } elseif ($maximum_stay) {
168
      $text .= 'cannot be more than @maximum_stay';
169
    }
170
171
    return array('text' => $text, 'args' => $args);
172
  }
173
174
  /**
175
   * @param $day
176
   * @return string
177
   */
178 View Code Duplication
  private function getWeekDay($day) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    $weekdays = array(
180
      1 => 'Monday',
181
      2 => 'Tuesday',
182
      3 => 'Wednesday',
183
      4 => 'Thursday',
184
      5 => 'Friday',
185
      6 => 'Saturday',
186
      7 => 'Sunday',
187
    );
188
189
    return isset($weekdays[$day]) ? $weekdays[$day] : '';
190
  }
191
192
  /**
193
   * @return int
194
   */
195
  public function getMinDays() {
196
    return $min_days;
0 ignored issues
show
Bug introduced by
The variable $min_days does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
197
  }
198
199
  /**
200
   * @param $min_days
201
   */
202
  public function setMinDays($min_days) {
203
    $this->min_days = $min_days;
204
  }
205
206
  /**
207
   * @return int
208
   */
209
  public function getMaxDays() {
210
    return $max_days;
0 ignored issues
show
Bug introduced by
The variable $max_days does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
211
  }
212
213
  /**
214
   * @param $max_days
215
   */
216
  public function setMaxDays($max_days) {
217
    $this->max_days = $max_days;
218
  }
219
220
  /**
221
   * @return int
222
   */
223
  public function getCheckinDay() {
224
    return $checkin_day;
0 ignored issues
show
Bug introduced by
The variable $checkin_day does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
225
  }
226
227
  /**
228
   * @param $checkin_day
229
   */
230
  public function setCheckinDay($checkin_day) {
231
    $this->checkin_day = $checkin_day;
232
  }
233
234
}
235