Fill_Orthanc_Table::addtoDbSerie()   B
last analyzed

Complexity

Conditions 8
Paths 64

Size

Total Lines 78
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
nc 64
nop 1
dl 0
loc 78
c 0
b 0
f 0
cc 8
rs 8.4444

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * Fill Orthanc_Studies and Orthanc_Series table to link DICOM Orthanc storage with the web plateform
18
 * Used in the validation process of the upload
19
 */
20
21
class Fill_Orthanc_Table {
22
    
23
	private $linkpdo;
24
	private $username;
25
	private $studyOrthancObject;
26
	private $visitObject;
27
28
    
29
	public function __construct($id_visit, String $username, PDO $linkpdo) {
30
		$this->username=$username;
31
		$this->linkpdo=$linkpdo;
32
		$this->visitObject=new Visit($id_visit, $linkpdo);
33
	}
34
    
35
	/**
36
	 * return study details of a study stored in Orthanc
37
	 * @param String $studyID
38
	 * @return array
39
	 */
40
	public function parseData(String $studyID) {
41
		$studyData=new Orthanc_Study($studyID, null, null);
42
		$studyData->retrieveStudyData();
43
		$this->studyOrthancObject=$studyData;
44
		$studyDetails=get_object_vars($studyData);
45
		return $studyDetails;
46
	}
47
    
48
	/**
49
	 * Fill data in the database, write data for study and for each series
50
	 * Once done trigger the change upload status of visit to update upload status and eventually skip 
51
	 * local form and/or QC
52
	 */
53
	public function fillDB($anonFromOrthancStudyId) {
54
        
55
		//Check that original OrthancID is unknown for this study
56
		if ($this->visitObject->getParentStudyObject()->isOriginalOrthancNeverKnown($anonFromOrthancStudyId)) {
57
			try {
58
				//Fill la database
59
				$this->addToDbStudy($anonFromOrthancStudyId);
60
                
61
				foreach ($this->studyOrthancObject->orthancSeries as $serie) {
62
					//Fill series database
63
					$this->addtoDbSerie($serie);
64
				}
65
				$this->visitObject->changeUploadStatus(Visit::DONE, $this->username);
66
                
67
			} catch (Exception $e1) {
68
				throw new Exception("Error during import ".$e1->getMessage());
69
			}
70
		} else {
71
			throw new Exception("Error during import Study Already Known");
72
		}
73
	}
74
    
75
	/**
76
	 * Private function to write into Orthanc_Studies DB
77
	 * @param string $anonFromOrthancStudyId
78
	 */
79
	private function addToDbStudy(string $anonFromOrthancStudyId) {
80
			$studyAcquisitionDate2=$this->parseDateTime($this->studyOrthancObject->studyDate, 1);
81
			$studyAcquisitionTime2=$this->parseDateTime($this->studyOrthancObject->studyTime, 2);
82
            
83
			if ($studyAcquisitionDate2 != null && $studyAcquisitionTime2 != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $studyAcquisitionDate2 of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
Bug introduced by
It seems like you are loosely comparing $studyAcquisitionTime2 of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
84
				$acquisitionDateTime=$studyAcquisitionDate2." ".$studyAcquisitionTime2;
85
			}
86
                        
87
			$addBdd=$this->linkpdo->prepare('INSERT INTO orthanc_studies (id_visit, 
88
                                            uploader, 
89
                                            upload_date, 
90
                                            acquisition_date, 
91
                                            acquisition_time, 
92
                                            acquisition_datetime, 
93
                                            study_orthanc_id, 
94
                                            anon_from_orthanc_id, 
95
                                            study_uid, 
96
                                            study_description, 
97
                                            patient_orthanc_id, 
98
                                            patient_name, 
99
                                            patient_id, 
100
                                            number_of_series, 
101
                                            number_of_instances, 
102
                                            disk_size, 
103
                                            uncompressed_disk_size)
104
            VALUES(:id_visit, 
105
                    :uploader, 
106
                    :upload_date, 
107
                    :acquisition_date, 
108
                    :acquisition_time, 
109
                    :acquisition_datetime, 
110
                    :study_orthanc_id, 
111
                    :anon_from_orthanc_id, 
112
                    :study_uid, 
113
                    :study_description, 
114
                    :patient_orthanc_id, 
115
                    :patient_name, 
116
                    :patient_id, 
117
                    :number_of_series, 
118
                    :number_of_instances, 
119
                    :disk_size, 
120
                    :uncompressed_disk_size)
121
            
122
            ON DUPLICATE KEY UPDATE uploader=:uploader, 
123
                                    upload_date=:upload_date, 
124
                                    acquisition_date=:acquisition_date, 
125
                                    acquisition_time=:acquisition_time, 
126
                                    acquisition_datetime=:acquisition_datetime, 
127
                                    anon_from_orthanc_id=:anon_from_orthanc_id, 
128
                                    study_uid=:study_uid, 
129
                                    study_description=:study_description, 
130
                                    patient_orthanc_id=:patient_orthanc_id, 
131
                                    patient_name=:patient_name, 
132
                                    patient_id=:patient_id, 
133
                                    number_of_series=:number_of_series, 
134
                                    number_of_instances=:number_of_instances, 
135
                                    disk_size=:disk_size, 
136
                                    uncompressed_disk_size=:uncompressed_disk_size, 
137
                                    deleted=0 ');
138
           
139
			$addBdd->execute(array(
140
				'id_visit'=>$this->visitObject->id_visit,  
141
				'uploader'=>$this->username,
142
				'upload_date'=>date("Y-m-d H:i:s"),  
143
				'acquisition_date'=>$this->studyOrthancObject->studyDate, 
144
				'acquisition_time'=>$this->studyOrthancObject->studyTime,
145
				'acquisition_datetime'=> isset($acquisitionDateTime) ? $acquisitionDateTime : null,
146
				'study_orthanc_id'=>$this->studyOrthancObject->studyOrthancID,
147
				'anon_from_orthanc_id'=>$anonFromOrthancStudyId,
148
				'study_uid'=>$this->studyOrthancObject->studyInstanceUID, 
149
				'study_description'=>$this->studyOrthancObject->studyDescription, 
150
				'patient_orthanc_id'=>$this->studyOrthancObject->parentPartientOrthancID,
151
				'patient_name'=>$this->studyOrthancObject->parentPatientName, 
152
				'patient_id'=>$this->studyOrthancObject->parentPatientID, 
153
				'number_of_series'=>$this->studyOrthancObject->numberOfSeriesInStudy,
154
				'number_of_instances'=>$this->studyOrthancObject->countInstances,
155
				'disk_size'=>$this->studyOrthancObject->diskSizeMb,
156
				'uncompressed_disk_size'=>$this->studyOrthancObject->uncompressedSizeMb
157
			));
158
159
	}
160
    
161
	/**
162
	 * Private function to write into the Orthanc_Series DB
163
	 * @param Orthanc_Serie $serie
164
	 */
165
	private function addtoDbSerie(Orthanc_Serie $serie) {
166
        
167
		$serieAcquisitionDate2=$this->parseDateTime($serie->seriesDate, 1);
168
		$serieAcquisitionTime2=$this->parseDateTime($serie->seriesTime, 2);
169
        
170
		if ($serieAcquisitionDate2 != null && $serieAcquisitionTime2 != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $serieAcquisitionTime2 of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
Bug introduced by
It seems like you are loosely comparing $serieAcquisitionDate2 of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
171
			$acquisitionDateTime=$serieAcquisitionDate2." ".$serieAcquisitionTime2;
172
		}
173
        
174
		$addBddSeries=$this->linkpdo->prepare('INSERT INTO orthanc_series (
175
                                                study_orthanc_id, 
176
                                                modality, 
177
                                                acquisition_date, 
178
                                                acquisition_time, 
179
                                                acquisition_datetime, 
180
                                                series_description, 
181
                                                injected_dose, 
182
                                                radiopharmaceutical, 
183
                                                half_life, 
184
                                                injected_time, 
185
                                                injected_activity, 
186
                                                series_orthanc_id, 
187
                                                number_of_instances, 
188
                                                serie_uid, 
189
                                                serie_number, 
190
                                                patient_weight, 
191
                                                serie_disk_size, 
192
                                                serie_uncompressed_disk_size, 
193
                                                manufacturer, 
194
                                                model_name, 
195
                                                injected_datetime) 
196
                                                VALUES( :Study_Orthanc_ID, 
197
                                                        :Modality, 
198
                                                        :Acquisition_Date, 
199
                                                        :Acquisition_Time, 
200
                                                        :Acquisition_DateTime, 
201
                                                        :Series_Description, 
202
                                                        :Injected_Dose, 
203
                                                        :Radiopharmaceutical, 
204
                                                        :HalfLife, 
205
                                                        :Injected_Time, 
206
                                                        :Injected_Activity,
207
                                                        :Series_Orthanc_ID, 
208
                                                        :Number_Instances, 
209
                                                        :Serie_UID, 
210
                                                        :Serie_Number, 
211
                                                        :Patient_Weight, 
212
                                                        :Serie_Disk_Size, 
213
                                                        :Serie_Uncompressed_Disk_Size, 
214
                                                        :Manufacturer, 
215
                                                        :Model_Name, :Injected_DateTime)
216
        ON DUPLICATE KEY UPDATE study_orthanc_id=:Study_Orthanc_ID, modality=:Modality, acquisition_date=:Acquisition_Date, acquisition_time=:Acquisition_Time, acquisition_datetime=:Acquisition_DateTime, series_description=:Series_Description, injected_dose=:Injected_Dose, radiopharmaceutical=:Radiopharmaceutical, half_life=:HalfLife, injected_time=:Injected_Time, injected_activity=:Injected_Activity, number_of_instances=:Number_Instances, serie_number=:Serie_Number, patient_weight=:Patient_Weight, serie_disk_size=:Serie_Disk_Size, serie_uncompressed_disk_size=:Serie_Uncompressed_Disk_Size, manufacturer=:Manufacturer, model_name=:Model_Name, deleted=0, injected_datetime=:Injected_DateTime');
217
218
		$value=array(
219
				'Study_Orthanc_ID'=>$this->studyOrthancObject->studyOrthancID,
220
				'Modality'=>$serie->modality,
221
				'Acquisition_Date'=>$serie->seriesDate,
222
				'Acquisition_Time'=>$serie->seriesTime,
223
				'Acquisition_DateTime'=> isset($acquisitionDateTime) ? $acquisitionDateTime : null,
224
				'Series_Description'=> $serie->seriesDescription,
225
				'Injected_Dose'=> is_numeric($serie->injectedDose) ? $serie->injectedDose : null,
226
				'Radiopharmaceutical'=> $serie->radiopharmaceutical,
227
				'HalfLife'=> is_numeric($serie->halfLife) ? $serie->halfLife : null,
228
				'Injected_Time'=> $serie->injectedTime,
229
				'Injected_DateTime'=> $this->parseDateTime($serie->injectedDateTime, 0),
230
				'Injected_Activity'=> is_numeric($serie->injectedActivity) ? $serie->injectedActivity : null,
231
				'Series_Orthanc_ID'=> $serie->serieOrthancID,
232
				'Number_Instances'=> $serie->numberOfInstanceInOrthanc,
233
				'Serie_UID'=> $serie->seriesInstanceUID,
234
				'Serie_Number'=> $serie->seriesNumber,
235
				'Patient_Weight'=>is_numeric($serie->patientWeight) ? $serie->patientWeight : null,
236
				'Serie_Disk_Size'=> $serie->diskSizeMb,
237
				'Serie_Uncompressed_Disk_Size' => $serie->uncompressedSizeMb,
238
				'Manufacturer'=> $serie->seriesManufacturer,
239
				'Model_Name'=>$serie->seriesModelName
240
			);
241
            
242
		$addBddSeries->execute($value);
243
		
244
	}
245
    
246
	/**
247
	 * Parse a DICOM date or Time string and return a string ready to send to database
248
	 * Return null if non parsable
249
	 * @param string $string
250
	 * @param type 0=dateTime, 1=Date, 2=Time
0 ignored issues
show
Documentation Bug introduced by
The doc comment 0=dateTime, 1=Date, 2=Time at position 0 could not be parsed: Unknown type name '0=dateTime' at position 0 in 0=dateTime, 1=Date, 2=Time.
Loading history...
251
	 * return formated date for db saving, null if parse failed
252
	 */
253
	private function parseDateTime($string, int $type) {
254
		$parsedDateTime=null;
255
        
256
		//If contain time split the ms (after.) which are not constant
257
		if ($type == 0 || $type == 2) {
258
			if (strpos($string, ".")) {
259
				$timeWithoutms=explode(".", $string);
260
				$string=$timeWithoutms[0];
261
			}
262
            
263
		}
264
        
265
		if ($type == 2) {
266
			$dateObject=DateTime::createFromFormat('His', $string);
267
			if ($dateObject !== false) {
268
				$parsedDateTime=$dateObject->format('H:i:s');
269
			}
270
		} else if ($type == 1) {
271
			$dateObject=DateTime::createFromFormat('Ymd', $string);
272
			if ($dateObject !== false) {
273
				$parsedDateTime=$dateObject->format('Y-m-d');
274
			}
275
		} else if ($type == 0) {
276
			$dateObject=DateTime::createFromFormat('YmdHis', $string);
277
			if ($dateObject !== false) {
278
				$parsedDateTime=$dateObject->format('Y-m-d H:i:s');
279
			}
280
		}
281
        
282
		return $parsedDateTime;
283
  
284
	}
285
    
286
}
287
288