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

SqlLiteDBStore   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 122
Duplicated Lines 26.23 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 23
c 1
b 0
f 1
lcom 1
cbo 2
dl 32
loc 122
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D getEventData() 15 44 9
C storeEvent() 17 51 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    try {
90
      // Itemize an event so we can save it
91
      $itemized = $event->itemizeEvent($granularity);
92
93
      // Write days
94
      foreach ($itemized[Event::BAT_DAY] as $year => $months) {
95
        foreach ($months as $month => $days) {
96
          $values = array_values($days);
97
          $keys = array_keys($days);
98
          $this->pdo->exec("INSERT OR REPLACE INTO $this->day_table (unit_id, year, month, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . implode(', ', $values) . ")");
99
        }
100
      }
101
102
      if ($granularity == Event::BAT_HOURLY) {
103
        // Write Hours
104
        foreach ($itemized[Event::BAT_HOUR] as $year => $months) {
105 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...
106
            foreach ($days as $day => $hours) {
107
              // Count required as we may receive empty hours for granular events that start and end on midnight
108
              if (count($hours) > 0) {
109
                $values = array_values($hours);
110
                $keys = array_keys($hours);
111
                $this->pdo->exec("INSERT OR REPLACE INTO $this->hour_table (unit_id, year, month, day, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . implode(', ', $values) . ")");
112
              }
113
            }
114
          }
115
        }
116
117
        // If we have minutes write minutes
118
        foreach ($itemized[Event::BAT_MINUTE] as $year => $months) {
119
          foreach ($months as $month => $days) {
120 View Code Duplication
            foreach ($days as $day => $hours) {
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...
121
              foreach ($hours as $hour => $minutes) {
122
                $values = array_values($minutes);
123
                $keys = array_keys($minutes);
124
                $this->pdo->exec("INSERT OR REPLACE 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) . ")");
125
              }
126
            }
127
          }
128
        }
129
      }
130
    }
131
    catch (\Exception $e) {
132
      $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...
133
    }
134
135
    return $stored;
136
  }
137
138
}
139