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.

DrupalDBStore::storeEvent()   F
last analyzed

Complexity

Conditions 25
Paths 54

Size

Total Lines 122

Duplication

Lines 23
Ratio 18.85 %

Importance

Changes 0
Metric Value
dl 23
loc 122
rs 3.3333
c 0
b 0
f 0
cc 25
nc 54
nop 2

How to fix   Long Method    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 DrupalDBStore
6
 */
7
8
namespace Roomify\Bat\Store;
9
10
use Roomify\Bat\Event\EventInterface;
11
use Roomify\Bat\Event\Event;
12
use Roomify\Bat\Event\EventItemizer;
13
use Roomify\Bat\Store\SqlDBStore;
14
15
/**
16
 * This is a Drupal-specific implementation of the Store.
17
 *
18
 */
19
class DrupalDBStore extends SqlDBStore {
20
21
  /**
22
   *
23
   * @param \DateTime $start_date
24
   * @param \DateTime $end_date
25
   * @param $unit_ids
26
   *
27
   * @return array
28
   */
29
  public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) {
30
31
    $queries  = $this->buildQueries($start_date, $end_date, $unit_ids);
32
33
    $results = array();
34
    // Run each query and store results
35
    foreach ($queries as $type => $query) {
36
      if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) {
37
        $results[$type] = \Drupal\Core\Database\Database::getConnection()->query($query);
38
      } else {
39
        $results[$type] = db_query($query);
40
      }
41
    }
42
43
    $db_events = array();
44
45
    // Cycle through day results and setup an event array
46
    while( $data = $results[Event::BAT_DAY]->fetchAssoc()) {
47
      // Figure out how many days the current month has
48
      $temp_date = new \DateTime($data['year'] . "-" . $data['month']);
49
      $days_in_month = (int)$temp_date->format('t');
50 View Code Duplication
      for ($i = 1; $i<=$days_in_month; $i++) {
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...
51
        $db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d'.$i];
52
      }
53
    }
54
55
    // With the day events taken care off let's cycle through hours
56
    while( $data = $results[Event::BAT_HOUR]->fetchAssoc()) {
57 View Code Duplication
      for ($i = 0; $i<=23; $i++) {
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...
58
        $db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']]['d' . $data['day']]['h'. $i] = $data['h'.$i];
59
      }
60
    }
61
62
    // With the hour events taken care off let's cycle through minutes
63
    while( $data = $results[Event::BAT_MINUTE]->fetchAssoc()) {
64 View Code Duplication
      for ($i = 0; $i<=59; $i++) {
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...
65
        if ($i <= 9) {
66
          $index = 'm0'.$i;
67
        } else {
68
          $index = 'm'.$i;
69
        }
70
        $db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']]['d' . $data['day']]['h' . $data['hour']][$index] = $data[$index];
71
      }
72
    }
73
74
    return $db_events;
75
  }
76
77
  /**
78
   * @param \Roomify\Bat\Event\EventInterface $event
79
   * @param $granularity
80
   *
81
   * @return bool
82
   */
83
  public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOURLY) {
84
    $stored = TRUE;
85
    if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) {
86
      $transaction = \Drupal\Core\Database\Database::getConnection()->startTransaction();
87
    } else {
88
      $transaction = db_transaction();
89
    }
90
91
    // Get existing event data from db
92
    $existing_events = $this->getEventData($event->getStartDate(), $event->getEndDate(), array($event->getUnitId()));
93
94
    try {
95
      // Itemize an event so we can save it
96
      $itemized = $event->itemize(new EventItemizer($event, $granularity));
97
98
      //Write days
99
      foreach ($itemized[Event::BAT_DAY] as $year => $months) {
100
        foreach ($months as $month => $days) {
101
          if ($granularity === Event::BAT_HOURLY) {
102
            foreach ($days as $day => $value) {
103
              $this->itemizeSplitDay($existing_events, $itemized, $value, $event->getUnitId(), $year, $month, $day);
104
            }
105
          }
106
          if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) {
107
            \Drupal\Core\Database\Database::getConnection()->merge($this->day_table_no_prefix)
108
              ->key(array(
109
                'unit_id' => $event->getUnitId(),
110
                'year' => $year,
111
                'month' => $month
112
              ))
113
              ->fields($days)
114
              ->execute();
115
          } else {
116
            db_merge($this->day_table_no_prefix)
117
              ->key(array(
118
                'unit_id' => $event->getUnitId(),
119
                'year' => $year,
120
                'month' => $month
121
              ))
122
              ->fields($days)
123
              ->execute();
124
          }
125
        }
126
      }
127
128
      if (($granularity == Event::BAT_HOURLY) && isset($itemized[Event::BAT_HOUR])) {
129
        // Write Hours
130
        foreach ($itemized[Event::BAT_HOUR] as $year => $months) {
131
          foreach ($months as $month => $days) {
132
            foreach ($days as $day => $hours) {
133
              // Count required as we may receive empty hours for granular events that start and end on midnight
134
              if (count($hours) > 0) {
135
                foreach ($hours as $hour => $value){
136
                  $this->itemizeSplitHour($existing_events, $itemized, $value, $event->getUnitId(), $year, $month, $day, $hour);
137
                }
138
                if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) {
139
                  \Drupal\Core\Database\Database::getConnection()->merge($this->hour_table_no_prefix)
140
                    ->key(array(
141
                      'unit_id' => $event->getUnitId(),
142
                      'year' => $year,
143
                      'month' => $month,
144
                      'day' => substr($day, 1)
145
                    ))
146
                    ->fields($hours)
147
                    ->execute();
148 View Code Duplication
                } else {
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...
149
                  db_merge($this->hour_table_no_prefix)
150
                    ->key(array(
151
                      'unit_id' => $event->getUnitId(),
152
                      'year' => $year,
153
                      'month' => $month,
154
                      'day' => substr($day, 1)
155
                    ))
156
                    ->fields($hours)
157
                    ->execute();
158
                }
159
              }
160
            }
161
          }
162
        }
163
164
        // If we have minutes write minutes
165
        foreach ($itemized[Event::BAT_MINUTE] as $year => $months) {
166
          foreach ($months as $month => $days) {
167
            foreach ($days as $day => $hours) {
168
              foreach ($hours as $hour => $minutes) {
169
                if (class_exists('Drupal') && floatval(\Drupal::VERSION) >= 9) {
170
                  \Drupal\Core\Database\Database::getConnection()->merge($this->minute_table_no_prefix)
171
                    ->key(array(
172
                      'unit_id' => $event->getUnitId(),
173
                      'year' => $year,
174
                      'month' => $month,
175
                      'day' => substr($day, 1),
176
                      'hour' => substr($hour, 1)
177
                    ))
178
                    ->fields($minutes)
179
                    ->execute();
180 View Code Duplication
                } else {
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...
181
                  db_merge($this->minute_table_no_prefix)
182
                    ->key(array(
183
                      'unit_id' => $event->getUnitId(),
184
                      'year' => $year,
185
                      'month' => $month,
186
                      'day' => substr($day, 1),
187
                      'hour' => substr($hour, 1)
188
                    ))
189
                    ->fields($minutes)
190
                    ->execute();
191
                }
192
              }
193
            }
194
          }
195
        }
196
      }
197
    } catch (\Exception $e) {
198
      $stored = FALSE;
199
      $transaction->rollback();
200
      watchdog_exception('BAT Event Save Exception', $e);
201
    }
202
203
    return $stored;
204
  }
205
206
}
207