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 ( 5d93fe...e96ad3 )
by
unknown
08:21
created

DrupalDBStore::buildQueries()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 49
rs 8.7972
c 1
b 0
f 0
cc 4
eloc 28
nc 5
nop 3
1
<?php
2
3
/**
4
 * @file
5
 * Class DrupalDBStore
6
 */
7
8
namespace Roomify\Bat\Store;
9
10
use Roomify\Bat\Event\Event;
11
use Roomify\Bat\Store\SqlDBStore;
12
13
/**
14
 * This is a Drupal-specific implementation of the Store.
15
 *
16
 */
17
class DrupalDBStore extends SqlDBStore {
18
19
  /**
20
   *
21
   * @param \DateTime $start_date
22
   * @param \DateTime $end_date
23
   * @param $unit_ids
24
   *
25
   * @return array
26
   */
27
  public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) {
28
29
    $queries  = $this->buildQueries($start_date, $end_date, $unit_ids);
30
31
    $results = array();
32
    // Run each query and store results
33
    foreach ($queries as $type => $query) {
34
      $results[$type] = db_query($query);
35
    }
36
37
    $db_events = array();
38
39
    // Cycle through day results and setup an event array
40
    while( $data = $results[Event::BAT_DAY]->fetchAssoc()) {
41
      // Figure out how many days the current month has
42
      $temp_date = new \DateTime($data['year'] . "-" . $data['month']);
43
      $days_in_month = (int)$temp_date->format('t');
44
      for ($i = 1; $i<=$days_in_month; $i++) {
45
        $db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d'.$i];
46
      }
47
    }
48
49
    // With the day events taken care off let's cycle through hours
50
    while( $data = $results[Event::BAT_HOUR]->fetchAssoc()) {
51 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...
52
        $db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']][$data['day']]['h'. $i] = $data['h'.$i];
53
      }
54
    }
55
56
    // With the hour events taken care off let's cycle through minutes
57
    while( $data = $results[Event::BAT_MINUTE]->fetchAssoc()) {
58 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...
59
        if ($i <= 9) {
60
          $index = 'm0'.$i;
61
        }
62
        else {
63
          $index = 'm'.$i;
64
        }
65
        $db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']][$data['day']][$data['hour']][$index] = $data[$index];
66
      }
67
    }
68
69
    return $db_events;
70
  }
71
72
  /**
73
   * @param \Roomify\Bat\Event\Event $event
74
   * @param $granularity
75
   *
76
   * @return bool
77
   */
78
  public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) {
79
    $stored = TRUE;
80
    $transaction = db_transaction();
81
82
    try {
83
      // Itemize an event so we can save it
84
      $itemized = $event->itemizeEvent($granularity);
85
86
      //Write days
87
      foreach ($itemized[Event::BAT_DAY] as $year => $months) {
88
        foreach ($months as $month => $days) {
89
          db_merge($this->day_table)
90
            ->key(array(
91
              'unit_id' => $event->unit_id,
0 ignored issues
show
Bug introduced by
The property unit_id cannot be accessed from this context as it is declared protected in class Roomify\Bat\Event\AbstractEvent.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
92
              'year' => $year,
93
              'month' => $month
94
            ))
95
            ->fields($days)
96
            ->execute();
97
        }
98
      }
99
100
      if ($granularity == Event::BAT_HOURLY) {
101
        // Write Hours
102 View Code Duplication
        foreach ($itemized[Event::BAT_HOUR] as $year => $months) {
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...
103
          foreach ($months as $month => $days) {
104
            foreach ($days as $day => $hours) {
105
              // Count required as we may receive empty hours for granular events that start and end on midnight
106
              if (count($hours) > 0) {
107
                db_merge($this->hour_table)
108
                  ->key(array(
109
                    'unit_id' => $event->unit_id,
0 ignored issues
show
Bug introduced by
The property unit_id cannot be accessed from this context as it is declared protected in class Roomify\Bat\Event\AbstractEvent.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
110
                    'year' => $year,
111
                    'month' => $month,
112
                    'day' => substr($day, 1)
113
                  ))
114
                  ->fields($hours)
115
                  ->execute();
116
              }
117
            }
118
          }
119
        }
120
121
        //If we have minutes write minutes
122
        foreach ($itemized[Event::BAT_MINUTE] as $year => $months) {
123 View Code Duplication
          foreach ($months as $month => $days) {
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...
124
            foreach ($days as $day => $hours) {
125
              foreach ($hours as $hour => $minutes) {
126
                db_merge($this->minute_table)
127
                  ->key(array(
128
                    'unit_id' => $event->unit_id,
0 ignored issues
show
Bug introduced by
The property unit_id cannot be accessed from this context as it is declared protected in class Roomify\Bat\Event\AbstractEvent.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
129
                    'year' => $year,
130
                    'month' => $month,
131
                    'day' => substr($day, 1),
132
                    'hour' => substr($hour, 1)
133
                  ))
134
                  ->fields($minutes)
135
                  ->execute();
136
              }
137
            }
138
          }
139
        }
140
      }
141
    }
142
    catch (\Exception $e) {
143
      $saved = FALSE;
0 ignored issues
show
Unused Code introduced by
$saved is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144
      $transaction->rollback();
145
      watchdog_exception('BAT Event Save Exception', $e);
146
    }
147
148
    return $stored;
149
  }
150
151
}
152