Completed
Push — fix-125 ( 2c3e6e...981995 )
by Victor
25:47
created

Scanner::getStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
	/** @var  int */
35
	protected $byteCount;
36
37
	/** @var  resource */
38
	protected $writeHandle;
39
40
	/** @var  resource */
41
	protected $readHandle;
42
43
	
44
	/** @var \OCA\Files_Antivirus\AppConfig */
45
	protected $appConfig;
46
47
	
48
	/**
49
	 * Close used resources
50
	 */
51
	abstract protected function shutdownScanner();
52
53
	
54
	public function getStatus(){
55
		if ($this->status instanceof Status){
56
			return $this->status;
57
		}
58
		return new Status();
59
	}
60
61
	/**
62
	 * Synchronous scan
63
	 * @param IScannable $item
64
	 * @return Status
65
	 */
66
	public function scan(IScannable $item) {
67
		$this->initScanner();
68
69
		while (false !== ($chunk = $item->fread())) {
70
			$this->writeChunk($chunk);
71
		}
72
		
73
		$this->shutdownScanner();
74
		return $this->getStatus();
75
	}
76
	
77
	/**
78
	 * Async scan - prepare resources
79
	 */
80
	public function initAsyncScan(){
81
		$this->initScanner();
82
	}
83
	
84
	/**
85
	 * Async scan - new portion of data is available
86
	 * @param string $data
87
	 */
88
	public function onAsyncData($data){
89
		$this->writeChunk($data);
90
	}
91
	
92
	/**
93
	 * Async scan - resource is closed
94
	 * @return Status
95
	 */
96
	public function completeAsyncScan(){
97
		$this->shutdownScanner();
98
		return $this->getStatus();
99
	}
100
	
101
	/**
102
	 * Open write handle. etc
103
	 */
104
	protected function initScanner(){
105
		$this->status = new Status();
106
	}
107
108
	/**
109
	 * @param string $chunk
110
	 */
111
	protected function writeChunk($chunk){
112
		$this->fwrite(
113
			$this->prepareChunk($chunk)
114
		);
115
	}
116
117
	protected function fwrite($data){
118
		$bytesWritten = @fwrite($this->getWriteHandle(), $data);
0 ignored issues
show
Unused Code introduced by
$bytesWritten is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
	}
120
121
	/**
122
	 * Get a resource to write data into
123
	 * @return resource
124
	 */
125
	protected function getWriteHandle(){
126
		return $this->writeHandle;
127
	}
128
129
	/**
130
	 * Prepare chunk (if required)
131
	 */
132
	protected function prepareChunk($data){
133
		return $data;
134
	}
135
}
136