diff --git a/src/Client.php b/src/Client.php index 2bdc531..9292856 100644 --- a/src/Client.php +++ b/src/Client.php @@ -31,14 +31,19 @@ class Client /** * @var string */ - const version = '2.1.2'; + const version = '2.2.2'; + + /** + * @var string + */ + private $logPath = ''; /** * Client constructor. */ - public function __construct($token) + public function __construct($token, $logPath = '') { - if (empty($token) or !is_string($token)) { + if (empty($token) or !is_string($token)) { throw new InvalidTokenException('Token string is required'); } @@ -46,6 +51,7 @@ public function __construct($token) $this->schools = new Schools($token); $this->meta = new Meta($token); $this->attendanceCodes = new AttendanceCodes($token); + $this->logPath = $logPath; } /** @@ -56,7 +62,10 @@ public function __construct($token) */ public function school($id) { - return new Schools($this->token, $id); + if(!empty($this->logPath)) { + $this->logPath .= DIRECTORY_SEPARATOR . $id; + } + return new Schools($this->token, $id, $this->logPath); } /** diff --git a/src/Endpoints/BootstrapEndpoint.php b/src/Endpoints/BootstrapEndpoint.php index ab04b5e..2b0690f 100644 --- a/src/Endpoints/BootstrapEndpoint.php +++ b/src/Endpoints/BootstrapEndpoint.php @@ -35,12 +35,18 @@ class BootstrapEndpoint */ public $token; + /** + * @var string + */ + private $logPath; + /** * BootstrapEndpoint constructor. */ - public function __construct($token, $uri = false) + public function __construct($token, $uri = false, $logPath = '') { $this->token = $token; + $this->logPath = $logPath; if ($uri) { $this->uri = $uri . $this->uri; @@ -111,7 +117,9 @@ public function all($includes = [], $parameters = []) $response = $this->getRequest($uri)->getBody()->getContents(); $decoded = json_decode($response); - return new ResultIterator($decoded, $this->token); + $this->logResponse($this->logPath, $uri, $response); + + return new ResultIterator($decoded, $this->token, $this->logPath); } /** @@ -166,6 +174,8 @@ public function getWithMeta($id, $includes = [], $parameters = []) $response = $this->getRequest($uri)->getBody()->getContents(); $decoded = json_decode($response); + $this->logResponse($this->logPath, $uri, $response); + return $decoded; } @@ -265,4 +275,22 @@ public function getEndpoint() { return "https://{$this->domain}/{$this->version}/"; } -} \ No newline at end of file + + + /** + * Log response to filesystem + * + * @param string $logPath + * @param string $uri + * @param string $response + */ + protected function logResponse(string $logPath, string $uri, string $response) { + if(!empty($logPath)) { + if (!is_dir($logPath)) { + mkdir($logPath, 0777, true); + } + $filename = sha1($uri) . '.json'; + file_put_contents($logPath . DIRECTORY_SEPARATOR . $filename, "URI: $uri\nRESPONSE:\n$response"); + } + } +} diff --git a/src/Endpoints/Schools.php b/src/Endpoints/Schools.php index cc87247..19d1c0e 100644 --- a/src/Endpoints/Schools.php +++ b/src/Endpoints/Schools.php @@ -142,46 +142,52 @@ class Schools extends BootstrapEndpoint */ public $exclusions; + /** + * @var string + */ + private $logPath; + /** * Schools constructor. * * @param string $uri */ - public function __construct($token, $id = false) + public function __construct($token, $id = false, $logPath = '') { $this->token = $token; + $this->logPath = $logPath; if ($id) { $this->uri = $this->uri . $id . '/'; } - $this->achievements = new Achievements($token, $this->uri); - $this->achievementsAttributes = new AchievementsAttributes($token, $this->uri); - $this->assessment = new Assessment($token, $this->uri); - $this->attendance = new Attendance($token, $this->uri); - $this->attendanceSummaries = new AttendanceSummaries($token, $this->uri); - $this->behaviours = new Behaviours($token, $this->uri); - $this->behavioursAttributes = new BehavioursAttributes($token, $this->uri); - $this->classes = new Classes($token, $this->uri); - $this->contacts = new Contacts($token, $this->uri); - $this->counts = new Counts($token, $this->uri); - $this->deletions = new Deletions($token, $this->uri); - $this->doctors = new Doctors($token, $this->uri); - $this->employees = new Employees($token, $this->uri); - $this->employeeAbsences = new EmployeeAbsences($token, $this->uri); - $this->events = new Events($token, $this->uri); - $this->exclusions = new Exclusions($token, $this->uri); - $this->groups = new Groups($token, $this->uri); - $this->lessons = new Lessons($token, $this->uri); - $this->lessonAttendance = new LessonAttendance($token, $this->uri); - $this->medicalConditions = new MedicalConditions($token, $this->uri); - $this->medicalEvents = new MedicalEvents($token, $this->uri); - $this->periods = new Periods($token, $this->uri); - $this->photos = new Photos($token, $this->uri); - $this->rooms = new Rooms($token, $this->uri); - $this->students = new Students($token, $this->uri); - $this->studentsPreAdmission = new StudentsPreAdmission($token, $this->uri); - $this->subjects = new Subjects($token, $this->uri); + $this->achievements = new Achievements($token, $this->uri, $this->logPath); + $this->achievementsAttributes = new AchievementsAttributes($token, $this->uri, $this->logPath); + $this->assessment = new Assessment($token, $this->uri, $this->logPath); + $this->attendance = new Attendance($token, $this->uri, $this->logPath); + $this->attendanceSummaries = new AttendanceSummaries($token, $this->uri, $this->logPath); + $this->behaviours = new Behaviours($token, $this->uri, $this->logPath); + $this->behavioursAttributes = new BehavioursAttributes($token, $this->uri, $this->logPath); + $this->classes = new Classes($token, $this->uri, $this->logPath); + $this->contacts = new Contacts($token, $this->uri, $this->logPath); + $this->counts = new Counts($token, $this->uri, $this->logPath); + $this->deletions = new Deletions($token, $this->uri, $this->logPath); + $this->doctors = new Doctors($token, $this->uri, $this->logPath); + $this->employees = new Employees($token, $this->uri, $this->logPath); + $this->employeeAbsences = new EmployeeAbsences($token, $this->uri, $this->logPath); + $this->events = new Events($token, $this->uri, $this->logPath); + $this->exclusions = new Exclusions($token, $this->uri, $this->logPath); + $this->groups = new Groups($token, $this->uri, $this->logPath); + $this->lessons = new Lessons($token, $this->uri, $this->logPath); + $this->lessonAttendance = new LessonAttendance($token, $this->uri, $this->logPath); + $this->medicalConditions = new MedicalConditions($token, $this->uri, $this->logPath); + $this->medicalEvents = new MedicalEvents($token, $this->uri, $this->logPath); + $this->periods = new Periods($token, $this->uri, $this->logPath); + $this->photos = new Photos($token, $this->uri, $this->logPath); + $this->rooms = new Rooms($token, $this->uri, $this->logPath); + $this->students = new Students($token, $this->uri, $this->logPath); + $this->studentsPreAdmission = new StudentsPreAdmission($token, $this->uri, $this->logPath); + $this->subjects = new Subjects($token, $this->uri, $this->logPath); } public function updateDomain($domain) diff --git a/src/ResultIterator.php b/src/ResultIterator.php index 3320dd3..bd7c198 100644 --- a/src/ResultIterator.php +++ b/src/ResultIterator.php @@ -19,11 +19,17 @@ class ResultIterator extends BootstrapEndpoint implements \Iterator */ private $meta; - public function __construct($givenArray, $token) + /** + * @var string + */ + private $logPath; + + public function __construct($givenArray, $token, $logPath = '') { $this->array = $givenArray->data; $this->token = $token; $this->meta = ! empty($givenArray->meta) ? $givenArray->meta : new \stdClass(); + $this->logPath = $logPath; } function rewind() @@ -57,6 +63,8 @@ function valid() $nextResponse = $this->getUrl($this->meta->pagination->next)->getBody()->getContents(); $decoded = json_decode($nextResponse); + $this->logResponse($this->logPath, $this->meta->pagination->next, $nextResponse); + $this->meta = ! empty($decoded->meta) ? $decoded->meta : new \stdClass(); $this->array = $decoded->data;