Completed
Push — master ( 587ec5...6da211 )
by Morris
8s
created

Local   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 65
ccs 26
cts 28
cp 0.9286
rs 10
c 4
b 1
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A initScanner() 0 15 2
A shutdownScanner() 0 10 1
A getWriteHandle() 0 3 1
A prepareChunk() 0 3 1
1
<?php
2
/**
3
 * Copyright (c) 2014 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
10
namespace OCA\Files_Antivirus\Scanner;
11
12
use OCA\Files_Antivirus\AppConfig;
13
14
class Local extends \OCA\Files_Antivirus\Scanner{
15
	
16
	/**
17
	 * @var string
18
	 */
19
	protected $avPath;
20
	
21
	/**
22
	 * STDIN and STDOUT descriptors
23
	 * @var array of resources
24
	 */
25
	private $pipes = array();
26
	
27
	/**
28
	 * Process handle
29
	 * @var resource
30
	 */
31
	private $process;
32
	
33 2
	public function __construct(AppConfig $config){
34 2
		$this->appConfig = $config;
35
		// get the path to the executable
36 2
		$this->avPath = escapeshellcmd($this->appConfig->getAvPath());
37
38
		// check that the executable is available
39 2
		if (!file_exists($this->avPath)) {
40
			throw new \RuntimeException('The antivirus executable could not be found at '.$this->avPath);
41
		}
42 2
	}
43
	
44 2
	public function initScanner(){
45 2
		parent::initScanner();
46
		
47
		// using 2>&1 to grab the full command-line output.
48 2
		$cmd = $this->avPath . " " . $this->appConfig->getCmdline() ." - 2>&1";
49
		$descriptorSpec = array(
50 2
			0 => array("pipe","r"), // STDIN
51 2
			1 => array("pipe","w")  // STDOUT
52 2
		);
53
		
54 2
		$this->process = proc_open($cmd, $descriptorSpec, $this->pipes);
55 2
		if (!is_resource($this->process)) {
56
			throw new \RuntimeException('Error starting process');
57
		}
58 2
	}
59
	
60 2
	protected function shutdownScanner(){
61 2
		fclose($this->pipes[0]);
62 2
		$output = stream_get_contents($this->pipes[1]);
63 2
		fclose($this->pipes[1]);
64
		
65 2
		$result = proc_close($this->process);
66
		
67 2
		\OCP\Util::writeLog('files_antivirus', 'Exit code :: ' . $result . ' Response :: ' . $output, \OCP\Util::DEBUG);
68 2
		$this->status->parseResponse($output, $result);
69 2
	}
70
	
71 2
	protected function getWriteHandle(){
72 2
		return $this->pipes[0];
73
	}
74
	
75 2
	protected function prepareChunk($data){
76 2
		return $data;
77
	}
78
}
79