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

CheckInDayConstraint::applyConstraint()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 29
Code Lines 17

Duplication

Lines 7
Ratio 24.14 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 7
loc 29
rs 5.2653
cc 11
eloc 17
nc 16
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Class CheckInDayConstraint
6
 */
7
8
namespace Roomify\Bat\Constraint;
9
10
use Roomify\Bat\Calendar\CalendarResponse;
11
use Roomify\Bat\Constraint\Constraint;
12
13
/**
14
 *
15
 */
16
class CheckInDayConstraint extends Constraint {
17
18
  /**
19
   * @var
20
   */
21
  protected $checkin_day;
22
23
  /**
24
   * @param $units
25
   * @param $checkin_day
26
   */
27
  public function __construct($units, $checkin_day, $start_date = NULL, $end_date = NULL) {
28
    parent::__construct($units);
29
30
    $this->checkin_day = $checkin_day;
31
    $this->start_date = $start_date;
32
    $this->end_date = $end_date;
33
  }
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function applyConstraint(CalendarResponse &$calendar_response) {
39
    parent::applyConstraint($calendar_response);
40
41
    if ($this->start_date === NULL) {
42
      $this->start_date = new \DateTime('1970-01-01');
43
    }
44
    if ($this->end_date === NULL) {
45
      $this->end_date = new \DateTime('2999-12-31');
46
    }
47
48
    if ( (($calendar_response->getStartDate()->getTimestamp() >= $this->start_date->getTimestamp() &&
49
           $calendar_response->getStartDate()->getTimestamp() <= $this->end_date->getTimestamp()) ||
50
          ($calendar_response->getStartDate()->getTimestamp() <= $this->start_date->getTimestamp() &&
51
           $calendar_response->getEndDate()->getTimestamp() >= $this->end_date->getTimestamp())) &&
52
         $this->checkin_day != $calendar_response->getStartDate()->format('N') ) {
53
54
      $units = $this->getUnits();
55
56
      $included_set = $calendar_response->getIncluded();
57
58 View Code Duplication
      foreach ($included_set as $unit_id => $set) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
59
        if (isset($units[$unit_id]) || empty($units)) {
60
          $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::INVALID_STATE);
61
62
          $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
63
        }
64
      }
65
    }
66
  }
67
68
  /**
69
   * Generates a text describing an availability_constraint.
70
   *
71
   * @return string
72
   *   The formatted message.
73
   */
74
  public function toString() {
75
    $text = '';
76
    $args = array();
77
78
    $start_date = FALSE;
79
    $end_date = FALSE;
80
81
    // Day of the week constraint variable.
82
    $day_of_the_week = $this->getWeekDay($this->checkin_day);
83
84
    // Date range constraint variables.
85
    if ($this->start_date !== NULL) {
86
      $start_date = $this->start_date->format('Y-m-d');
87
    }
88
    if ($this->start_date !== NULL) {
89
      $end_date = $this->end_date->format('Y-m-d');
90
    }
91
92
    // Finally, build out the constraint text string adding components
93
    // as necessary.
94
95
    // Specify a date range constraint.
96 View Code Duplication
    if ($start_date && $end_date) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
      $text = 'From @start_date to @end_date';
98
99
      $args['@start_date'] = $start_date;
100
      $args['@end_date'] = $end_date;
101
    }
102
103
    // Specify the day of the week constraint.
104 View Code Duplication
    if ($day_of_the_week) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105
      if ($start_date && $end_date) {
106
        $text = 'From @start_date to @end_date, if booking starts on @day_of_the_week';
107
      } else {
108
        $text = 'If booking starts on @day_of_the_week';
109
      }
110
111
      $args['@day_of_the_week'] = $day_of_the_week;
112
    }
113
114
    return array('text' => $text, 'args' => $args);
115
  }
116
117
  /**
118
   * @param $day
119
   * @return string
120
   */
121 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...
122
    $weekdays = array(
123
      1 => 'Monday',
124
      2 => 'Tuesday',
125
      3 => 'Wednesday',
126
      4 => 'Thursday',
127
      5 => 'Friday',
128
      6 => 'Saturday',
129
      7 => 'Sunday',
130
    );
131
132
    return isset($weekdays[$day]) ? $weekdays[$day] : '';
133
  }
134
135
}
136