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 ( a65850...aa6a9b )
by
unknown
02:35
created

MinMaxDaysConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 1
eloc 7
nc 1
nop 6
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 ($this->start_date->getTimestamp() <= $calendar_response->getStartDate()->getTimestamp() &&
66
        $this->end_date->getTimestamp() >= $calendar_response->getEndDate()->getTimestamp() &&
67
        ($this->checkin_day === NULL || $this->checkin_day == $calendar_response->getStartDate()->format('N'))) {
68
69
      $units = $this->getUnits();
70
71
      $included_set = $calendar_response->getIncluded();
72
73
      foreach ($included_set as $unit_id => $set) {
74
        if (isset($units[$unit_id]) || empty($units)) {
75
          $start_date = $calendar_response->getStartDate();
76
          $end_date = $calendar_response->getEndDate();
77
78
          $diff = $end_date->diff($start_date)->days;
79
          if (is_numeric($this->min_days) && $diff < $this->min_days) {
80
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
81
82
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
83
          }
84
          elseif (is_numeric($this->max_days) && $diff > $this->max_days) {
85
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
86
87
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
88
          }
89
        }
90
      }
91
    }
92
  }
93
94
  /**
95
   * Generates a text describing an availability_constraint.
96
   *
97
   * @return string
98
   *   The formatted message.
99
   */
100
  public function toString() {
101
    $text = '';
102
103
    // Min/max stay length constraint variables.
104
    $minimum_stay = empty($this->min_days) ? '' : (($this->min_days == 1) ? '@count day' : '@count days');
105
    $maximum_stay = empty($this->max_days) ? '' : (($this->max_days == 1) ? '@count day' : '@count days');
106
107
    // Day of the week constraint variable.
108
    $day_of_the_week = $this->getWeekDay($this->checkin_day);
109
110
    $start_date = FALSE;
111
    $end_date = FALSE;
112
113
    // Date range constraint variables.
114
    if ($this->start_date !== NULL) {
115
      $start_date = $this->start_date->format('Y-m-d');
116
    }
117
    if ($this->start_date !== NULL) {
118
      $end_date = $this->end_date->format('Y-m-d');
119
    }
120
121
    // Next create replacement placeholders to be used in t() below.
122
    $args = array(
123
      '@minimum_stay' => $minimum_stay,
124
      '@maximum_stay' => $maximum_stay,
125
      '@day_of_the_week' => $day_of_the_week,
126
    );
127
128
    // Finally, build out the constraint text string adding components
129
    // as necessary.
130
131
    // Specify a date range constraint.
132
    if ($start_date && $end_date) {
133
      $text = 'From @start_date to @end_date';
134
      $args['@start_date'] = $start_date;
135
      $args['@end_date'] = $end_date;
136
    }
137
138
    // Specify the day of the week constraint.
139
    if ($day_of_the_week) {
140
      if ($start_date && $end_date) {
141
        $text = 'From @start_date to @end_date, if booking starts on @day_of_the_week';
142
      }
143
      else {
144
        $text = 'If booking starts on @day_of_the_week';
145
      }
146
    }
147
148
    // Specify the min/max stay length constraint.
149
    if ($minimum_stay || $maximum_stay) {
150
      if (empty($text)) {
151
        $text = 'The stay ';
152
      }
153
      else {
154
        $text .= ' the stay ';
155
      }
156
    }
157
    if ($minimum_stay && $maximum_stay) {
158
      // Special case when min stay and max stay are the same.
159
      if ($minimum_stay == $maximum_stay) {
160
        $text .= 'must be for @minimum_stay';
161
      }
162
      else {
163
        $text .= 'must be at least @minimum_stay and at most @maximum_stay';
164
      }
165
    }
166
    elseif ($minimum_stay) {
167
      $text .= 'must be for at least @minimum_stay';
168
    }
169
    elseif ($maximum_stay) {
170
      $text .= 'cannot be more than @maximum_stay';
171
    }
172
173
    return array('text' => $text, 'args' => $args);
174
  }
175
176
  /**
177
   * @param $day
178
   * @return string
179
   */
180 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...
181
    $weekdays = array(
182
      1 => 'Monday',
183
      2 => 'Tuesday',
184
      3 => 'Wednesday',
185
      4 => 'Thursday',
186
      5 => 'Friday',
187
      6 => 'Saturday',
188
      7 => 'Sunday',
189
    );
190
191
    return isset($weekdays[$day]) ? $weekdays[$day] : '';
192
  }
193
194
}
195