Form_Processor_File::getMaxSizeMb()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 Copyright (C) 2018-2020 KANOUN Salim
4
 This program is free software; you can redistribute it and/or modify
5
 it under the terms of the Affero GNU General Public v.3 License as published by
6
 the Free Software Foundation;
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
 Affero GNU General Public Public for more details.
11
 You should have received a copy of the Affero GNU General Public Public along
12
 with this program; if not, write to the Free Software Foundation, Inc.,
13
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14
 */
15
16
/**
17
 * Abstract class to handle review in the system
18
 * Each Study-Visit should extend this abstract class and redifine the two abstract methods
19
 * - saveSpecificForm : Which recieve the raw form and should write data to the specific table of database
20
 * - setVisitValidation : Which should define the rules to change status of review's visit (ex : Wait Adjudication or Done)
21
 * 
22
 * These two methodes will be launched for each new review recieved by the system
23
 * 
24
 * The makeReviewUnavailable() can be overrided to let the visit available for review even if Review status "Done" is reached 
25
 * (by default it will no longer accept new review after reaching this status)
26
 * @author salim
27
 *
28
 */
29
30
abstract class Form_Processor_File extends Form_Processor {
31
32
	function __construct(Visit $visitObject, bool $local, string $username, PDO $linkpdo) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
		parent::__construct($visitObject, $local, $username, $linkpdo);
34
	}
35
36
	//Should return an array of string of allowed key files
37
	abstract function getAllowedFileKeys() : array;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
39
	/**
40
	 * Can be overwitten if need to allow other MIME Type
41
	 */
42
	protected function getAllowedType() : array {
43
		return array('text/csv', 'application/vnd.ms-excel');
44
	}
45
    
46
	protected function getMaxSizeMb() : int{
47
		//NB : if more than 5Mb need to update root htaccess
48
		return 5;
49
	}
50
51
	private function createEmptySpecificForm(){
52
		$insertion = $this->linkpdo->prepare('INSERT INTO '.$this->specificTable.' (id_review) VALUES (:id_review)');
53
		$insertion->execute(array(
54
			'id_review' => $this->reviewObject->id_review )
55
		);
56
	}
57
58
	/**
59
	 * Store or overwirte a file, each file is defined by a Key (visit specific)
60
	 */
61
	public function storeAssociatedFile(string $fileKey, string $mime, int $fileSize, string $uploadedTempFile) {
62
63
		//If first form upload create a draft form to insert file uploaded data
64
		if (empty($this->reviewObject)) {
65
			$this->createReview();
66
			$this->createEmptySpecificForm();
67
		}else {
68
			//If review exist but validated throw exception
69
			if ($this->reviewObject->validated) {
70
				throw new Exception('Validated Review, can\'t add File');
71
			}
72
		}
73
        
74
		//Get extension of file and check extension is allowed
75
		//Get filesize and check it matches limits
76
		$sizeMb=$fileSize/1048576;
77
		if ($sizeMb > $this->getMaxSizeMb()) throw new Exception('File over limits');
78
		if (!$this->isInDeclaredKey($fileKey)) throw new Exception('Unhauthrized file key'); 
79
		if (!$this->isInAllowedType($mime)) throw new Exception('Extension not allowed');
80
81
		$mimes=new \Mimey\MimeTypes;
0 ignored issues
show
Bug introduced by
The type Mimey\MimeTypes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
		$extension=$mimes->getExtension($mime);
83
		$fileName=$this->visitObject->patientCode.'_'.$this->visitObject->visitType.'_'.$fileKey.'.'.$extension;
84
85
		$associatedFinalFile=$this->reviewObject->storeAssociatedFile($uploadedTempFile, $fileName);
86
        
87
		//Add or overide file key and write to database
88
		$fileArray=$this->reviewObject->associatedFiles;
89
		$fileArray[$fileKey]=$associatedFinalFile;
90
		$this->reviewObject->updateAssociatedFiles($fileArray);
91
92
	}
93
    
94
	private function isInDeclaredKey(string $fileKey) {
95
		return in_array($fileKey, $this->getAllowedFileKeys());
96
	}
97
98
	private function isInAllowedType(string $extension) {
99
		return in_array($extension, $this->getAllowedType());
100
	}
101
102
103
}