|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace smtech\CanvasICSSync\SyncIntoCanvas; |
|
4
|
|
|
|
|
5
|
|
|
use PDO; |
|
6
|
|
|
use smtech\CanvasPest\CanvasPest; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A syncable object |
|
10
|
|
|
* |
|
11
|
|
|
* @author Seth Battis <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Syncable |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Database connection |
|
17
|
|
|
* |
|
18
|
|
|
* @var PDO|null |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $database; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Canvas API connection |
|
24
|
|
|
* |
|
25
|
|
|
* @var CanvasPest|null |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $api; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Unique timestamp for the current sync |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $syncTimestamp; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Generate a unique identifier for this synchronization pass |
|
37
|
|
|
**/ |
|
38
|
|
|
public static function getSyncTimestamp() |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
if (empty(static::$syncTimestamp)) { |
|
41
|
|
|
$timestamp = new DateTime(); |
|
42
|
|
|
static::$syncTimestamp = |
|
43
|
|
|
$timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT) . |
|
44
|
|
|
Constants::SEPARATOR . md5( |
|
45
|
|
|
(php_sapi_name() == 'cli' ? |
|
46
|
|
|
'cli' : $_SERVER['REMOTE_ADDR'] |
|
47
|
|
|
) . time() |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
return static::$syncTimestamp; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Update the MySQL connection |
|
55
|
|
|
* |
|
56
|
|
|
* @param PDO $db |
|
|
|
|
|
|
57
|
|
|
* @throws Exception If `$db` is null |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function setDatabase(PDO $database) |
|
60
|
|
|
{ |
|
61
|
|
|
if (empty($database)) { |
|
62
|
|
|
throw new Exception( |
|
63
|
|
|
'A non-null database connection is required' |
|
64
|
|
|
); |
|
65
|
|
|
} else { |
|
66
|
|
|
static::$database = $database; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the MySQL connection |
|
72
|
|
|
* |
|
73
|
|
|
* @return mysqli |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function getDatabase() |
|
76
|
|
|
{ |
|
77
|
|
|
return static::$db; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Update the API connection |
|
82
|
|
|
* |
|
83
|
|
|
* @param CanvasPest $api |
|
84
|
|
|
* @throws Exception if `$api` is null |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function setApi(CanvasPest $api) |
|
87
|
|
|
{ |
|
88
|
|
|
if (empty($api)) { |
|
89
|
|
|
throw new Exception( |
|
90
|
|
|
'A non-null API connection is required' |
|
91
|
|
|
); |
|
92
|
|
|
} else { |
|
93
|
|
|
static::$api = $api; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the API connection |
|
99
|
|
|
* |
|
100
|
|
|
* @return CanvasPest|null |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function getApi() |
|
103
|
|
|
{ |
|
104
|
|
|
return static::$api; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Save the syncable object to the MySQL database |
|
109
|
|
|
* |
|
110
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
abstract public function save(); |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Load a syncable object from the MySQL database |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $id |
|
118
|
|
|
* @return Syncable|null |
|
119
|
|
|
*/ |
|
120
|
|
|
abstract public static function load($id); |
|
121
|
|
|
} |
|
122
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: