Completed
Push — master ( e090e4...18d79e )
by Damian
02:26
created

checks/FileAccessibilityAndValidationCheck.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Checks for the accessibility and file type validation of one or more files or folders.
5
 *
6
 * Examples:
7
 * // Checks /assets/calculator_files has .json files and all files are valid json files.
8
 * EnvironmentCheckSuite::register('check', 'FileAccessibilityAndValidationCheck("' . BASE_PATH . '/assets/calculator_files/*.json",
9
 *  "jsonValidate", '.FileAccessibilityAndValidationCheck::CHECK_ALL.')', 'Check a json file exist and are all valid json files'
10
 * );
11
 * 
12
 * // Checks /assets/calculator_files/calculator.json exists and is valid json file.
13
 * EnvironmentCheckSuite::register('check', 'FileAccessibilityAndValidationCheck("' . BASE_PATH . '/assets/calculator_files/calculator.json",
14
 *  "jsonValidate", '.FileAccessibilityAndValidationCheck::CHECK_SINGLE.')', 'Check a calculator.json exists and is valid json file'
15
 * );
16
 *
17
 * // Check only existence 
18
 * EnvironmentCheckSuite::register('check', 'FileAccessibilityAndValidationCheck("' . BASE_PATH . '/assets/calculator_files/calculator.json")',
19
 * 'Check a calculator.json exists only'
20
 * );
21
 */
22
class FileAccessibilityAndValidationCheck implements EnvironmentCheck {
0 ignored issues
show
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
23
	/**
24
	 * @var int
25
	 */
26
	const CHECK_SINGLE = 1;
27
28
	/**
29
	 * @var int
30
	 */
31
	const CHECK_ALL = 2;
32
33
	/**
34
	 * Absolute path to a file or folder, compatible with glob().
35
	 *
36
	 * @var string
37
	 */
38
	protected $path;
39
40
	/**
41
	 * Constant, check for a single file to match age criteria, or all of them.
42
	 *
43
	 * @var int
44
	 */
45
	protected $fileTypeValidateFunc;
46
47
	/**
48
	 * Constant, check for a single file to match age criteria, or all of them.
49
	 *
50
	 * @var int
51
	 */
52
	protected $checkType;
53
54
	/**
55
	 * @param string $path
56
	 * @param string $fileTypeValidateFunc
57
	 * @param null|int $checkType
58
	 */
59
	function __construct($path, $fileTypeValidateFunc = 'noVidation', $checkType = null) {
60
		$this->path = $path;
61
		$this->fileTypeValidateFunc = ($fileTypeValidateFunc)? $fileTypeValidateFunc:'noVidation';
62
		$this->checkType = ($checkType) ? $checkType : self::CHECK_SINGLE;
63
	}
64
65
	/**
66
	 * @inheritdoc
67
	 *
68
	 * @return array
69
	 */
70
	function check() {
71
		$origStage = Versioned::get_reading_mode();
72
		Versioned::set_reading_mode('Live');
73
74
		$files = $this->getFiles();
75
		if($files){
76
			$fileTypeValidateFunc = $this->fileTypeValidateFunc;
77
			if(method_exists ($this, $fileTypeValidateFunc)){
78
				$invalidFiles = array();
79
				$validFiles = array();
80
81
				foreach($files as $file){
82
					if($this->$fileTypeValidateFunc($file)){
83
						$validFiles[] = $file;
84
					}else{
85
						$invalidFiles[] = $file;
86
					}
87
				}
88
89
				// If at least one file was valid, count as passed
90
				if($this->checkType == self::CHECK_SINGLE && count($invalidFiles) < count($files)) {
91
					$validFileList = "\n";
92
					foreach($validFiles as $vf){
93
						$validFileList .= $vf."\n";
94
					}
95 View Code Duplication
					if($fileTypeValidateFunc == 'noVidation') {
96
						$checkReturn = array(
97
							EnvironmentCheck::OK,
98
							sprintf('At least these file(s) accessible: %s', $validFileList)
99
						);
100
					}else{
101
						$checkReturn = array(
102
							EnvironmentCheck::OK,
103
							sprintf('At least these file(s) passed file type validate function "%s": %s', $fileTypeValidateFunc, $validFileList)
104
						);
105
					}
106
				} else {
107
					if (count($invalidFiles) == 0) $checkReturn = array(EnvironmentCheck::OK, 'All files valideted');
108
					else {
109
						$invalidFileList = "\n";
110
						foreach($invalidFiles as $vf){
111
							$invalidFileList .= $vf."\n";
112
						}
113
114 View Code Duplication
						if($fileTypeValidateFunc == 'noVidation'){
115
							$checkReturn = array(
116
								EnvironmentCheck::ERROR,
117
								sprintf('File(s) not accessible: %s', $invalidFileList)
118
							);
119
						}else{
120
							$checkReturn = array(
121
								EnvironmentCheck::ERROR,
122
								sprintf('File(s) not passing the file type validate function "%s": %s', $fileTypeValidateFunc, $invalidFileList)
123
							);
124
						}
125
	
126
					}
127
				}
128
			}else{
129
				$checkReturn =  array(
130
					EnvironmentCheck::ERROR,
131
					sprintf("Invalid file type validation method name passed: %s ", $fileTypeValidateFunc)
132
				);
133
			}
134
135
		}else{
136
			$checkReturn = array(
137
				EnvironmentCheck::ERROR,
138
				sprintf("No files accessible at path %s", $this->path)
139
			);
140
		}
141
142
		Versioned::set_reading_mode($origStage);
143
144
		return $checkReturn;
145
	}
146
147
	/**
148
	 * @param string $file
149
	 *
150
	 * @return bool
151
	 */
152
	private function jsonValidate($file){
153
		$json = json_decode(file_get_contents($file));
154
		if(!$json) {
155
			return false;
156
		}else{
157
			return true;
158
		}
159
	}
160
161
	/**
162
	 * @param string $file
163
	 *
164
	 * @return bool
165
	 */
166
	protected function noVidation($file) {
167
		return true;
168
	}
169
170
	/**
171
	 * Gets a list of absolute file paths.
172
	 *
173
	 * @return array
174
	 */
175
	protected function getFiles() {
176
		return glob($this->path);
177
	}
178
}
179