diff --git a/code/checks/FileAccessibilityAndValidationCheck.php b/code/checks/FileAccessibilityAndValidationCheck.php new file mode 100644 index 0000000..c7257a0 --- /dev/null +++ b/code/checks/FileAccessibilityAndValidationCheck.php @@ -0,0 +1,145 @@ +path = $path; + $this->fileTypeValidateFunc = ($fileTypeValidateFunc)? $fileTypeValidateFunc:'noVidation'; + $this->checkType = ($checkType) ? $checkType : self::CHECK_SINGLE; + } + + function check() { + $origStage = Versioned::get_reading_mode(); + Versioned::set_reading_mode('Live'); + + $files = $this->getFiles(); + if($files){ + $fileTypeValidateFunc = $this->fileTypeValidateFunc; + if(method_exists ($this, $fileTypeValidateFunc)){ + $invalidFiles = array(); + $validFiles = array(); + + foreach($files as $file){ + if($this->$fileTypeValidateFunc($file)){ + $validFiles[] = $file; + }else{ + $invalidFiles[] = $file; + } + } + + // If at least one file was valid, count as passed + if($this->checkType == self::CHECK_SINGLE && count($invalidFiles) < count($files)) { + $validFileList = "\n"; + foreach($validFiles as $vf){ + $validFileList .= $vf."\n"; + } + if($fileTypeValidateFunc == 'noVidation') { + $checkReturn = array( + EnvironmentCheck::OK, + sprintf('At least these file(s) accessible: %s', $validFileList) + ); + }else{ + $checkReturn = array( + EnvironmentCheck::OK, + sprintf('At least these file(s) passed file type validate function "%s": %s', $fileTypeValidateFunc, $validFileList) + ); + } + } else { + if (count($invalidFiles) == 0) $checkReturn = array(EnvironmentCheck::OK, 'All files valideted'); + else { + $invalidFileList = "\n"; + foreach($invalidFiles as $vf){ + $invalidFileList .= $vf."\n"; + } + + if($fileTypeValidateFunc == 'noVidation'){ + $checkReturn = array( + EnvironmentCheck::ERROR, + sprintf('File(s) not accessible: %s', $invalidFileList) + ); + }else{ + $checkReturn = array( + EnvironmentCheck::ERROR, + sprintf('File(s) not passing the file type validate function "%s": %s', $fileTypeValidateFunc, $invalidFileList) + ); + } + + } + } + }else{ + $checkReturn = array( + EnvironmentCheck::ERROR, + sprintf("Invalid file type validation method name passed: %s ", $fileTypeValidateFunc) + ); + } + + }else{ + $checkReturn = array( + EnvironmentCheck::ERROR, + sprintf("No files accessible at path %s", $this->path) + ); + } + + Versioned::set_reading_mode($origStage); + + return $checkReturn; + } + + private function jsonValidate($file){ + $json = json_decode(file_get_contents($file)); + if(!$json) { + return false; + }else{ + return true; + } + } + + protected function noVidation($file) { + return true; + } + + /** + * @return Array Of absolute file paths + */ + protected function getFiles() { + return glob($this->path); + } + +} \ No newline at end of file