1 | <?php |
||
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() |
||
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) |
||
69 | |||
70 | /** |
||
71 | * Get the MySQL connection |
||
72 | * |
||
73 | * @return mysqli |
||
74 | */ |
||
75 | public static function getDatabase() |
||
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) |
||
96 | |||
97 | /** |
||
98 | * Get the API connection |
||
99 | * |
||
100 | * @return CanvasPest|null |
||
101 | */ |
||
102 | public static function getApi() |
||
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: