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 ( 84136f...fd9624 )
by Ronald
02:33
created

SqlLiteDBStore::storeEvent()   D

Complexity

Conditions 19
Paths 25

Size

Total Lines 86
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 86
rs 4.7641
cc 19
eloc 52
nc 25
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 SqlLiteDBStore
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 SqlLite implementation of the Store.
15
 *
16
 */
17
class SqlLiteDBStore extends SqlDBStore {
18
19
  protected $pdo;
20
21
  public function __construct(\PDO $pdo, $event_type, $event_data = 'state') {
22
    parent::__construct($event_type, $event_data);
23
24
    $this->pdo = $pdo;
25
  }
26
27
  /**
28
   *
29
   * @param \DateTime $start_date
30
   * @param \DateTime $end_date
31
   * @param $unit_ids
32
   *
33
   * @return array
34
   */
35
  public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) {
36
37
    $queries  = $this->buildQueries($start_date, $end_date, $unit_ids);
38
39
    $results = array();
40
    // Run each query and store results
41
    foreach ($queries as $type => $query) {
42
      $results[$type] = $this->pdo->query($query);
43
    }
44
45
    $db_events = array();
46
47
    // Cycle through day results and setup an event array
48
    foreach ($results[Event::BAT_DAY]->fetchAll() as $data) {
49
      // Figure out how many days the current month has
50
      $temp_date = new \DateTime($data['year'] . "-" . $data['month']);
51
      $days_in_month = (int)$temp_date->format('t');
52 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...
53
        $db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d'.$i];
54
      }
55
    }
56
57
    // With the day events taken care off let's cycle through hours
58
    foreach ($results[Event::BAT_HOUR]->fetchAll() as $data) {
59 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...
60
        $db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']][$data['day']]['h'. $i] = $data['h'.$i];
61
      }
62
    }
63
64
    // With the hour events taken care off let's cycle through minutes
65
    foreach ($results[Event::BAT_MINUTE]->fetchAll() as $data) {
66 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...
67
        if ($i <= 9) {
68
          $index = 'm0'.$i;
69
        }
70
        else {
71
          $index = 'm'.$i;
72
        }
73
        $db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']][$data['day']][$data['hour']][$index] = $data[$index];
74
      }
75
    }
76
77
    return $db_events;
78
  }
79
80
  /**
81
   * @param \Roomify\Bat\Event\Event $event
82
   * @param $granularity
83
   *
84
   * @return bool
85
   */
86
  public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) {
87
    $stored = TRUE;
88
89
    // Get existing event data from db
90
    $existing_events = $this->getEventData($event->getStartDate(), $event->getEndDate(), array($event->getUnitId()));
91
92
93
    try {
94
      // Itemize an event so we can save it
95
      $itemized = $event->itemizeEvent($granularity);
96
97
      // Write days
98
      foreach ($itemized[Event::BAT_DAY] as $year => $months) {
99
        foreach ($months as $month => $days) {
100
          $values = array_values($days);
101
          $keys = array_keys($days);
102
          // Because SQLite does not have a nice merge first we have to check if a row exists to determine whether to do an insert or an update
103
          if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month])) {
104
            $command = "UPDATE $this->day_table SET ";
105
            foreach ($days as $day => $value){
106
              $command .=   "$day = $value,";
107
            }
108
            $command = rtrim($command, ',');
109
            $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month";
110
            $this->pdo->exec($command);
111
          } else {
112
            $this->pdo->exec("INSERT INTO $this->day_table (unit_id, year, month, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . implode(', ', $values) . ")");
113
          }
114
        }
115
      }
116
117
      if ($granularity == Event::BAT_HOURLY) {
118
        // Write Hours
119
        foreach ($itemized[Event::BAT_HOUR] as $year => $months) {
120
          foreach ($months as $month => $days) {
121
            foreach ($days as $day => $hours) {
122
              // Count required as we may receive empty hours for granular events that start and end on midnight
123
              if (count($hours) > 0) {
124
                $values = array_values($hours);
125
                $keys = array_keys($hours);
126
                if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month][$day])) {
127
                  $command = "UPDATE $this->hour_table SET ";
128
                  foreach ($hours as $hour => $value){
129
                    $command .=   "$hour = $value,";
130
                  }
131
                  $command = rtrim($command, ',');
132
                  $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month AND day = ". substr($day,1);
133
                  $this->pdo->exec($command);
134
                } else {
135
                  $this->pdo->exec("INSERT INTO $this->hour_table (unit_id, year, month, day, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . implode(', ', $values) . ")");
136
                }
137
              }
138
            }
139
          }
140
        }
141
142
        // If we have minutes write minutes
143
        foreach ($itemized[Event::BAT_MINUTE] as $year => $months) {
144
          foreach ($months as $month => $days) {
145
            foreach ($days as $day => $hours) {
146
              foreach ($hours as $hour => $minutes) {
147
                $values = array_values($minutes);
148
                $keys = array_keys($minutes);
149
                if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month][$day][$hour])) {
150
                  $command = "UPDATE $this->minute_table SET ";
151
                  foreach ($minutes as $minute => $value){
152
                    $command .=   "$minute = $value,";
153
                  }
154
                  $command = rtrim($command, ',');
155
                  $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month AND day = ". substr($day,1) . " AND hour = " . substr($hour,1);
156
                  $this->pdo->exec($command);
157
                } else {
158
                  $this->pdo->exec("INSERT INTO $this->minute_table (unit_id, year, month, day, hour, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . substr($hour, 1) . ", " . implode(', ', $values) . ")");
159
                }
160
              }
161
            }
162
          }
163
        }
164
      }
165
    }
166
    catch (\Exception $e) {
167
      $stored = FALSE;
168
    }
169
170
    return $stored;
171
  }
172
173
}
174