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

checks/FileAccessibilityAndValidationCheck.php (2 issues)

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
23
{
24
    /**
25
     * @var int
26
     */
27
    const CHECK_SINGLE = 1;
28
29
    /**
30
     * @var int
31
     */
32
    const CHECK_ALL = 2;
33
34
    /**
35
     * Absolute path to a file or folder, compatible with glob().
36
     *
37
     * @var string
38
     */
39
    protected $path;
40
41
    /**
42
     * Constant, check for a single file to match age criteria, or all of them.
43
     *
44
     * @var int
45
     */
46
    protected $fileTypeValidateFunc;
47
48
    /**
49
     * Constant, check for a single file to match age criteria, or all of them.
50
     *
51
     * @var int
52
     */
53
    protected $checkType;
54
55
    /**
56
     * @param string $path
57
     * @param string $fileTypeValidateFunc
58
     * @param null|int $checkType
59
     */
60
    public function __construct($path, $fileTypeValidateFunc = 'noVidation', $checkType = null)
61
    {
62
        $this->path = $path;
63
        $this->fileTypeValidateFunc = ($fileTypeValidateFunc)? $fileTypeValidateFunc:'noVidation';
64
        $this->checkType = ($checkType) ? $checkType : self::CHECK_SINGLE;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     *
70
     * @return array
71
     */
72
    public function check()
73
    {
74
        $origStage = Versioned::get_reading_mode();
75
        Versioned::set_reading_mode('Live');
76
77
        $files = $this->getFiles();
78
        if ($files) {
79
            $fileTypeValidateFunc = $this->fileTypeValidateFunc;
80
            if (method_exists($this, $fileTypeValidateFunc)) {
81
                $invalidFiles = array();
82
                $validFiles = array();
83
84
                foreach ($files as $file) {
85
                    if ($this->$fileTypeValidateFunc($file)) {
86
                        $validFiles[] = $file;
87
                    } else {
88
                        $invalidFiles[] = $file;
89
                    }
90
                }
91
92
                // If at least one file was valid, count as passed
93
                if ($this->checkType == self::CHECK_SINGLE && count($invalidFiles) < count($files)) {
94
                    $validFileList = "\n";
95
                    foreach ($validFiles as $vf) {
96
                        $validFileList .= $vf."\n";
97
                    }
98 View Code Duplication
                    if ($fileTypeValidateFunc == 'noVidation') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
                        $checkReturn = array(
100
                            EnvironmentCheck::OK,
101
                            sprintf('At least these file(s) accessible: %s', $validFileList)
102
                        );
103
                    } else {
104
                        $checkReturn = array(
105
                            EnvironmentCheck::OK,
106
                            sprintf('At least these file(s) passed file type validate function "%s": %s', $fileTypeValidateFunc, $validFileList)
107
                        );
108
                    }
109
                } else {
110
                    if (count($invalidFiles) == 0) {
111
                        $checkReturn = array(EnvironmentCheck::OK, 'All files valideted');
112
                    } else {
113
                        $invalidFileList = "\n";
114
                        foreach ($invalidFiles as $vf) {
115
                            $invalidFileList .= $vf."\n";
116
                        }
117
118 View Code Duplication
                        if ($fileTypeValidateFunc == 'noVidation') {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
                            $checkReturn = array(
120
                                EnvironmentCheck::ERROR,
121
                                sprintf('File(s) not accessible: %s', $invalidFileList)
122
                            );
123
                        } else {
124
                            $checkReturn = array(
125
                                EnvironmentCheck::ERROR,
126
                                sprintf('File(s) not passing the file type validate function "%s": %s', $fileTypeValidateFunc, $invalidFileList)
127
                            );
128
                        }
129
                    }
130
                }
131
            } else {
132
                $checkReturn =  array(
133
                    EnvironmentCheck::ERROR,
134
                    sprintf("Invalid file type validation method name passed: %s ", $fileTypeValidateFunc)
135
                );
136
            }
137
        } else {
138
            $checkReturn = array(
139
                EnvironmentCheck::ERROR,
140
                sprintf("No files accessible at path %s", $this->path)
141
            );
142
        }
143
144
        Versioned::set_reading_mode($origStage);
145
146
        return $checkReturn;
147
    }
148
149
    /**
150
     * @param string $file
151
     *
152
     * @return bool
153
     */
154
    private function jsonValidate($file)
155
    {
156
        $json = json_decode(file_get_contents($file));
157
        if (!$json) {
158
            return false;
159
        } else {
160
            return true;
161
        }
162
    }
163
164
    /**
165
     * @param string $file
166
     *
167
     * @return bool
168
     */
169
    protected function noVidation($file)
170
    {
171
        return true;
172
    }
173
174
    /**
175
     * Gets a list of absolute file paths.
176
     *
177
     * @return array
178
     */
179
    protected function getFiles()
180
    {
181
        return glob($this->path);
182
    }
183
}
184