-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
LitCalHealth.php
272 lines (247 loc) · 12.4 KB
/
LitCalHealth.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include_once( 'vendor/autoload.php' );
include_once( 'includes/enums/LitSchema.php' );
$testsFolder = dirname(__FILE__) . '/tests';
$it = new DirectoryIterator("glob://$testsFolder/*Test.php");
foreach($it as $f) {
$fileName = $f->getFilename();
include_once( 'tests/' . $fileName );
}
use Swaggest\JsonSchema\InvalidValue;
use Swaggest\JsonSchema\Schema;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class LitCalHealth implements MessageComponentInterface {
protected $clients;
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
echo sprintf('Receiving message "%s" from connection %d', $msg, $from->resourceId);
$messageReceived = json_decode( $msg );
if( json_last_error() === JSON_ERROR_NONE ) {
if( property_exists( $messageReceived, 'action' ) ) {
switch( $messageReceived->action ) {
case 'executeValidation':
if(
property_exists( $messageReceived, 'validate' ) &&
property_exists( $messageReceived, 'sourceFile' ) &&
property_exists( $messageReceived, 'category' )
) {
$this->executeValidation( $messageReceived, $from );
}
break;
case 'validateCalendar':
if(
property_exists( $messageReceived, 'calendar' ) &&
property_exists( $messageReceived, 'year' ) &&
property_exists( $messageReceived, 'category' )
) {
$this->validateCalendar( $messageReceived->calendar, $messageReceived->year, $messageReceived->category, $from );
}
break;
case 'executeUnitTest':
if(
property_exists( $messageReceived, 'calendar' ) &&
property_exists( $messageReceived, 'year' ) &&
property_exists( $messageReceived, 'category' ) &&
property_exists( $messageReceived, 'test' )
) {
$this->executeUnitTest( $messageReceived->test, $messageReceived->calendar, $messageReceived->year, $messageReceived->category, $from );
}
break;
default:
$message = new stdClass();
$message->type = "echobot";
$message->text = $msg;
$this->sendMessage( $from, $message );
}
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
private function sendMessage( ConnectionInterface $from, string|stdClass $msg ) {
if( gettype( $msg ) !== 'string' ) {
$msg = json_encode( $msg );
}
foreach ($this->clients as $client) {
if ($from === $client) {
// The message from sender will be echoed back only to the sender, not to other clients
$client->send($msg);
}
}
}
const DataPathToSchema = [
"https://litcal.johnromanodorazio.com/api/dev/LitCalMetadata.php" => LitSchema::METADATA,
"data/propriumdetempore.json" => LitSchema::PROPRIUMDETEMPORE,
"data/propriumdesanctis_1970/propriumdesanctis_1970.json" => LitSchema::PROPRIUMDESANCTIS,
"data/propriumdesanctis_2002/propriumdesanctis_2002.json" => LitSchema::PROPRIUMDESANCTIS,
"data/propriumdesanctis_2008/propriumdesanctis_2008.json" => LitSchema::PROPRIUMDESANCTIS,
"data/memorialsFromDecrees/memorialsFromDecrees.json" => LitSchema::DECREEMEMORIALS,
"nations/index.json" => LitSchema::INDEX
];
const LitCalBaseUrl = "https://litcal.johnromanodorazio.com/api/dev/LitCalEngine.php";
public function __construct() {
$this->clients = new \SplObjectStorage;
}
private function executeValidation( object $validation, ConnectionInterface $to ) {
$dataPath = $validation->sourceFile;
switch( $validation->category ) {
case 'universalcalendar':
$schema = LitCalHealth::DataPathToSchema[ $dataPath ];
break;
case 'nationalcalendar':
$schema = LitSchema::NATIONAL;
break;
case 'diocesancalendar':
$schema = LitSchema::DIOCESAN;
break;
case 'widerregioncalendar':
$schema = LitSchema::WIDERREGION;
break;
case 'propriumdesanctis':
$schema = LitSchema::PROPRIUMDESANCTIS;
break;
}
$data = file_get_contents( $dataPath );
if( $data !== false ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The Data file $dataPath exists";
$message->classes = ".$validation->validate.file-exists";
$this->sendMessage( $to, $message );
$jsonData = json_decode( $data );
if( json_last_error() === JSON_ERROR_NONE ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The Data file $dataPath was successfully decoded as JSON";
$message->classes = ".$validation->validate.json-valid";
$this->sendMessage( $to, $message );
$validationResult = $this->validateDataAgainstSchema( $jsonData, $schema );
if( gettype( $validationResult ) === 'boolean' && $validationResult === true ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The Data file $dataPath was successfully validated against the Schema $schema";
$message->classes = ".$validation->validate.schema-valid";
$this->sendMessage( $to, $message );
}
else if( gettype( $validationResult === 'object' ) ) {
$validationResult->classes = ".$validation->validate.schema-valid";
$this->sendMessage( $to, $validationResult );
}
} else {
$message = new stdClass();
$message->type = "error";
$message->text = "There was an error decoding the Data file $dataPath as JSON: " . json_last_error_msg();
$message->classes = ".$validation->validate.json-valid";
$this->sendMessage( $to, $message );
}
} else {
$message = new stdClass();
$message->type = "error";
$message->text = "Data file $dataPath does not exist";
$message->classes = ".$validation->validate.file-exists";
$this->sendMessage( $to, $message );
}
}
private function validateCalendar( string $Calendar, int $Year, string $category, ConnectionInterface $to ) : void {
if( $Calendar === 'VATICAN' ) {
$req = "?nationalcalendar=VATICAN&year=$Year&calendartype=CIVIL";
} else {
$req = "?$category=$Calendar&year=$Year&calendartype=CIVIL";
}
$data = file_get_contents( self::LitCalBaseUrl . $req );
if( $data !== false ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The $category of $Calendar for the year $Year exists";
$message->classes = ".calendar-$Calendar.file-exists.year-$Year";
$this->sendMessage( $to, $message );
$jsonData = json_decode( $data );
if( json_last_error() === JSON_ERROR_NONE ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The $category of $Calendar for the year $Year was successfully decoded as JSON";
$message->classes = ".calendar-$Calendar.json-valid.year-$Year";
$this->sendMessage( $to, $message );
$validationResult = $this->validateDataAgainstSchema( $jsonData, LitSchema::LITCAL );
if( gettype( $validationResult ) === 'boolean' && $validationResult === true ) {
$message = new stdClass();
$message->type = "success";
$message->text = "The $category of $Calendar for the year $Year was successfully validated against the Schema " . LitSchema::LITCAL;
$message->classes = ".calendar-$Calendar.schema-valid.year-$Year";
$this->sendMessage( $to, $message );
}
else if( gettype( $validationResult === 'object' ) ) {
$validationResult->classes = ".calendar-$Calendar.schema-valid.year-$Year";
$this->sendMessage( $to, $validationResult );
}
} else {
$message = new stdClass();
$message->type = "error";
$message->text = "There was an error decoding the $category of $Calendar for the year $Year from the URL " . self::LitCalBaseUrl . $req . " as JSON: " . json_last_error_msg();
$message->classes = ".calendar-$Calendar.json-valid.year-$Year";
$this->sendMessage( $to, $message );
}
} else {
$message = new stdClass();
$message->type = "error";
$message->text = "The $category of $Calendar for the year $Year does not exist at the URL " . self::LitCalBaseUrl . $req;
$message->classes = ".calendar-$Calendar.file-exists.year-$Year";
$this->sendMessage( $to, $message );
}
}
private function executeUnitTest( string $Test, string $Calendar, int $Year, string $category, ConnectionInterface $to ) : void {
if( $Calendar === 'VATICAN' ) {
$req = "?nationalcalendar=VATICAN&year=$Year&calendartype=CIVIL";
} else {
$req = "?$category=$Calendar&year=$Year&calendartype=CIVIL";
}
$jsonData = json_decode( file_get_contents( self::LitCalBaseUrl . $req ) );
if( json_last_error() === JSON_ERROR_NONE ) {
$TestClass = new $Test;
$TestClass::$testObject = $jsonData;
$testResult = $TestClass->test();
if( gettype( $testResult ) === 'boolean' && $testResult === true ) {
$message = new stdClass();
$message->type = "success";
$message->text = "$Test passed for the Calendar $Calendar for the year $Year";
$message->classes = ".$Test.year-{$Year}.test-valid";
$message->test = $Test;
$this->sendMessage( $to, $message );
}
else if( gettype( $testResult ) === 'object' ) {
$testResult->classes = ".$Test.year-{$Year}.test-valid";
$testResult->test = $Test;
$testResult->jsonData = $jsonData;
$this->sendMessage( $to, $testResult );
}
}
}
private function validateDataAgainstSchema( object|array $data, string $schemaUrl ) : bool|object {
$res = false;
try {
$schema = Schema::import( $schemaUrl );
$schema->in($data);
$res = true;
} catch (InvalidValue|Exception $e) {
$message = new stdClass();
$message->type = "error";
$message->text = LitSchema::ERROR_MESSAGES[ $schemaUrl ] . PHP_EOL . $e->getMessage();
return $message;
}
return $res;
}
}