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.

SqlDBStore   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 135
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 18
loc 135
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 18 25 3
A buildQueries() 0 44 4

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 SqlDBStore
6
 */
7
8
namespace Roomify\Bat\Store;
9
10
use Roomify\Bat\Event\Event;
11
use Roomify\Bat\Event\EventItemizer;
12
use Roomify\Bat\Unit\Unit;
13
use Roomify\Bat\Store\SqlDBStore;
14
15
/**
16
 * This is a generic Sql implementation of the Store.
17
 *
18
 */
19
abstract class SqlDBStore extends Store {
20
21
  // There are two types of stores - for event ids and status
22
  const BAT_EVENT = 'event';
23
  const BAT_STATE = 'state';
24
25
  /**
26
   * The table that holds day data.
27
   * @var
28
   */
29
  public $day_table;
30
31
  /**
32
   * The table that holds hour data.
33
   * @var
34
   */
35
  public $hour_table;
36
37
  /**
38
   * The table that holds minute data.
39
   * @var
40
   */
41
  public $minute_table;
42
43
  /**
44
   * The table that holds day data without prefix.
45
   * @var
46
   */
47
  public $day_table_no_prefix;
48
49
  /**
50
   * The table that holds hour data without prefix.
51
   * @var
52
   */
53
  public $hour_table_no_prefix;
54
55
  /**
56
   * The table that holds minute data without prefix.
57
   * @var
58
   */
59
  public $minute_table_no_prefix;
60
61
  /**
62
   * The event type we are dealing with.
63
   * @var
64
   */
65
  public $event_type;
66
67
  /**
68
   * SqlDBStore constructor.
69
   *
70
   * Provided with the event type it will determine the appropriate table names to
71
   * store data in. This assumes standard behaviour from Bat_Event
72
   * @param $event_type
73
   * @param string $event_data
74
   */
75
  public function __construct($event_type, $event_data = 'state', $prefix = '') {
76
77
    $this->event_type = $event_type;
78
79 View Code Duplication
    if ($event_data == SqlDBStore::BAT_STATE) {
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...
80
      $this->day_table = $prefix . 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_STATE;
81
      $this->hour_table = $prefix . 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_STATE;
82
      $this->minute_table = $prefix . 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_STATE;
83
84
      $this->day_table_no_prefix = 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_STATE;
85
      $this->hour_table_no_prefix = 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_STATE;
86
      $this->minute_table_no_prefix = 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_STATE;
87
    }
88
89 View Code Duplication
    if ($event_data == SqlDBStore::BAT_EVENT) {
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...
90
      $this->day_table = $prefix . 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_EVENT;
91
      $this->hour_table = $prefix . 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_EVENT;
92
      $this->minute_table = $prefix . 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_EVENT;
93
94
      $this->day_table_no_prefix = 'bat_event_' . $event_type . '_day_' . SqlDBStore::BAT_EVENT;
95
      $this->hour_table_no_prefix = 'bat_event_' . $event_type . '_hour_' . SqlDBStore::BAT_EVENT;
96
      $this->minute_table_no_prefix = 'bat_event_' . $event_type . '_minute_' . SqlDBStore::BAT_EVENT;
97
    }
98
99
  }
100
101
  /**
102
   * @param \DateTime $start_date
103
   * @param \DateTime $end_date
104
   * @param $unit_ids
105
   *
106
   * @return array
107
   */
108
  public function buildQueries(\DateTime $start_date, \DateTime $end_date, $unit_ids) {
109
    $queries = array();
110
111
    $queries[Event::BAT_DAY] = 'SELECT * FROM ' . $this->day_table . ' WHERE ';
112
    $queries[Event::BAT_HOUR] = 'SELECT * FROM ' . $this->hour_table . ' WHERE ';
113
    $queries[Event::BAT_MINUTE] = 'SELECT * FROM ' . $this->minute_table . ' WHERE ';
114
115
    // Create a mock event which we will use to determine how to query the database
116
    $mock_event = new Event($start_date, $end_date, new Unit(0, 0, array()), -10);
117
    // We don't need a granular event even if we are retrieving granular data - since we don't
118
    // know what the event break-down is going to be we need to get the full range of data from
119
    // days, hours and minutes.
120
    $itemized = $mock_event->itemize(new EventItemizer($mock_event, Event::BAT_DAILY));
121
122
    $year_count = 0;
123
124
    $query_parameters = '';
125
126
    foreach ($itemized[Event::BAT_DAY] as $year => $months) {
127
      if ($year_count > 0) {
128
        // We are dealing with multiple years so add an OR
129
        $query_parameters .= ' OR ';
130
      }
131
      $query_parameters .= 'year IN (' . $year . ') ';
132
      $query_parameters .= 'AND month IN (' . implode(",", array_keys($months)) . ') ';
133
      if (count($unit_ids) > 0) {
134
        // Unit ids are defined so add this as a filter
135
        $query_parameters .= 'AND unit_id in (' . implode("," , $unit_ids) . ') ';
136
      }
137
      $year_count++;
138
    }
139
140
    // Add parameters to each query
141
    $queries[Event::BAT_DAY] .= $query_parameters;
142
    $queries[Event::BAT_HOUR] .= $query_parameters;
143
    $queries[Event::BAT_MINUTE] .= $query_parameters;
144
145
    // Clean up and add ordering information
146
    $queries[Event::BAT_DAY] .= ' ORDER BY unit_id, year, month';
147
    $queries[Event::BAT_HOUR] .= ' ORDER BY unit_id, year, month, day';
148
    $queries[Event::BAT_MINUTE] .= ' ORDER BY unit_id, year, month, day, hour';
149
150
    return $queries;
151
  }
152
153
}
154