This repository was archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeeklyDelta.php
More file actions
60 lines (46 loc) · 1.86 KB
/
WeeklyDelta.php
File metadata and controls
60 lines (46 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
use Carbon\Carbon;
use Carbon\CarbonImmutable;
require_once 'vendor/autoload.php';
require_once 'config.php';
class WeeklyDelta {
private $data = [];
public $requiredBy = '';
private $caseStr = "COUNT(CASE WHEN dateClosed <= CAST('%s' AS DATE) THEN defID ELSE NULL END) AS %s";
private $queryString = "SELECT sy.systemName AS system, %s, %s FROM CDL c JOIN system sy ON c.systemAffected = sy.systemID WHERE dateClosed IS NOT NULL and c.requiredBy < %u GROUP BY c.systemAffected";
private function connect() {
return new MysqliDb(DB_HOST, DB_USER, DB_PWD, DB_NAME);
}
private function fetchData() {
$queryStr = $this->buildQueryString();
$link = $this->connect();
$result = $link->rawQuery($queryStr);
$link->disconnect();
return $result;
}
private function buildQueryString() {
$today = new CarbonImmutable();
$caseThisWeek = sprintf($this->caseStr, $today, 'openThisWeek');
$caseLastWeek = sprintf($this->caseStr, $today->subWeek(), 'openLastWeek');
return sprintf($this->queryString, $caseThisWeek, $caseLastWeek, $this->requiredBy);
}
public function fetchRequiredByID($reqBy) {
$link = $this->connect();
$whereField = is_int($reqBy) ? 'reqByID' : 'requiredBy';
$link->where($whereField, $reqBy);
$result = $link->getOne('requiredBy', 'reqByID');
$link->disconnect();
return $result['reqByID'];
}
public function __construct($reqBy) {
$this->requiredBy = $this->fetchRequiredByID($reqBy);
if ($this->requiredBy) $this->data = $this->fetchData();
else throw new Exception("No requiredBy data could be found for value $reqBy");
}
public function getData() {
return $this->data;
}
public function __toString() {
print_r($this->getData());
}
}