Completed
Push — stable9 ( 66c84e...e336ce )
by Victor
7s
created

Scanner::initAsyncScan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
* ownCloud - files_antivirus
5
*
6
* @author Manuel Deglado
7
* @copyright 2012 Manuel Deglado [email protected]
8
*
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
* License as published by the Free Software Foundation; either
12
* version 3 of the License, or any later version.
13
*
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
*
19
* You should have received a copy of the GNU Affero General Public
20
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
*
22
*/
23
24
namespace OCA\Files_Antivirus;
25
26
abstract class Scanner {
27
	
28
	/**
29
	 * Scan result
30
	 * @var \OCA\Files_Antivirus\Status
31
	 */
32
	protected $status;
33
	
34
	/**
35
	 * @var \OCA\Files_Antivirus\AppConfig
36
	 */
37
	protected $appConfig;
38
	
39
	/**
40
	 * Close used resources
41
	 */
42
	abstract protected function shutdownScanner();
43
	
44
	/**
45
	 * Get a resource to write data into
46
	 */
47
	abstract protected function getWriteHandle();
48
	
49 3
	public function getStatus(){
50 3
		if ($this->status instanceof Status){
51 3
			return $this->status;
52
		}
53
		return new Status();
54
	}
55
56
	/**
57
	 * Synchronous scan
58
	 * @param IScannable $item
59
	 * @return Status
60
	 */
61 3
	public function scan(IScannable $item) {
62 3
		$this->initScanner();
63
64 3
		while (false !== ($chunk = $item->fread())) {
65 3
			fwrite(
66 3
					$this->getWriteHandle(), 
67 3
					$this->prepareChunk($chunk)
68 3
			);
69 3
		}
70
		
71 3
		$this->shutdownScanner();
72 3
		return $this->getStatus();
73
	}
74
	
75
	/**
76
	 * Async scan - prepare resources
77
	 */
78
	public function initAsyncScan(){
79
		$this->initScanner();
80
	}
81
	
82
	/**
83
	 * Async scan - new portion of data is available
84
	 * @param string $data
85
	 */
86
	public function onAsyncData($data){
87
		fwrite(
88
				$this->getWriteHandle(),
89
				$this->prepareChunk($data)
90
		);
91
	}
92
	
93
	/**
94
	 * Async scan - resource is closed
95
	 * @return Status
96
	 */
97
	public function completeAsyncScan(){
98
		$this->shutdownScanner();
99
		return $this->getStatus();
100
	}
101
	
102
	/**
103
	 * Open write handle. etc
104
	 */
105 3
	protected function initScanner(){
106 3
		$this->status = new Status();
107 3
	}
108
109
	/**
110
	 * Prepare chunk (if required)
111
	 */
112
	protected function prepareChunk($data){
113
		return $data;
114
	}
115
}
116