Passed
Pull Request — master (#6)
by
unknown
02:54
created

DocumentConverter::import()   C

Complexity

Conditions 8
Paths 40

Size

Total Lines 69
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 6.5437
c 0
b 0
f 0
cc 8
eloc 44
nc 40
nop 0

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
namespace SilverStripe\DocumentConverter;
4
5
use CURLFile;
6
use SilverStripe\Assets\Folder;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\ORM\DataObject;
9
use ZipArchive;
10
11
/**
12
 * Utility class hiding the specifics of the document conversion process.
13
 */
14
class DocumentConverter {
15
16
	use Configurable;
17
18
	/**
19
	 * @var array Docvert connection details
20
	 * @config
21
	 */
22
	private static $docvert_details = [
0 ignored issues
show
introduced by
The private property $docvert_details is not used, and could be removed.
Loading history...
23
		'username' => '',
24
		'password' => '',
25
		'url' => ''
26
	];
27
28
	/**
29
	 * Associative array of:
30
	 * - name: the full name of the file including the extension.
31
	 * - path: the path to the file on the local filesystem.
32
	 * - mimeType
33
	 */
34
	protected $fileDescriptor;
35
36
	/**
37
	 * @var int
38
	 * ID of a SilverStripe\Assets\Folder
39
	 */
40
	protected $chosenFolderID;
41
42
	/**
43
	 * @var array instance specific connection details
44
	 * initially filled with the config settings
45
	 */
46
	protected $docvertDetails = [
47
		'username' => '',
48
		'password' => '',
49
		'url' => ''
50
	];
51
52
	public function __construct($fileDescriptor, $chosenFolderID = null) {
53
		$this->fileDescriptor = $fileDescriptor;
54
		$this->chosenFolderID = $chosenFolderID;
55
		array_merge($this->docvertDetails, (array)$this->config()->get('docvert_details'));
56
	}
57
58
	public function setDocvertUsername($username = null)  {
59
		$this->docvertDetails['username'] = $username;
60
	}
61
62
	public function getDocvertUsername() {
63
		return $this->docvertDetails['username'];
64
	}
65
66
	public function setDocvertPassword($password = null) {
67
		$this->docvertDetails['password'] = $password;
68
	}
69
70
	public function getDocvertPassword() {
71
		return $this->docvertDetails['password'];
72
	}
73
74
	public function setDocvertUrl($url = null) {
75
		$this->docvertDetails['url'] = $url;
76
	}
77
78
	public function getDocvertUrl() {
79
		return $this->docvertDetails['url'];
80
	}
81
82
	public function import() {
83
		$ch = curl_init();
84
85
		// PHP 5.5+ introduced CURLFile which makes the '@/path/to/file' syntax deprecated.
86
		if(class_exists('CURLFile')) {
87
			$file = new CURLFile(
88
				$this->fileDescriptor['path'],
89
				$this->fileDescriptor['mimeType'],
90
				$this->fileDescriptor['name']
91
			);
92
		} else {
93
			$file = '@' . $this->fileDescriptor['path'];
94
		}
95
96
		curl_setopt_array($ch, array(
97
			CURLOPT_URL => $this->getDocvertUrl(),
98
			CURLOPT_USERPWD => sprintf('%s:%s', $this->getDocvertUsername(), $this->getDocvertPassword()),
99
			CURLOPT_POST => 1,
100
			CURLOPT_POSTFIELDS => array('file' => $file),
101
			CURLOPT_CONNECTTIMEOUT => 25,
102
			CURLOPT_TIMEOUT => 100,
103
		));
104
105
		$chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null;
106
		$folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : '';
107
		$outname = tempnam(ASSETS_PATH, 'convert');
108
		$outzip = $outname . '.zip';
109
110
		$out = fopen($outzip, 'w');
111
		curl_setopt($ch, CURLOPT_FILE, $out);
112
		$returnValue = curl_exec($ch);
113
		$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
114
		curl_close($ch);
115
		fclose($out);
0 ignored issues
show
Bug introduced by
It seems like $out can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
		fclose(/** @scrutinizer ignore-type */ $out);
Loading history...
116
		chmod($outzip, 0666);
117
118
		if (!$returnValue || ($status != 200)) {
119
			return array('error' => _t(
120
				__CLASS__ . '.SERVERUNREACHABLE',
121
				'Could not contact document conversion server. Please try again later or contact your system administrator.',
122
				'Document Converter process Word documents into HTML.'
123
			));
124
		}
125
126
		// extract the converted document into assets
127
		// you need php zip, i.e. port install php5-zip
128
		$zip = new ZipArchive();
129
130
		if($zip->open($outzip)) {
131
			$zip->extractTo(ASSETS_PATH .$folderName);
132
		}
133
134
		// remove temporary files
135
		unlink($outname);
136
		unlink($outzip);
137
138
		if (!file_exists(ASSETS_PATH . $folderName . '/index.html')) {
139
			return array('error' =>  _t(
140
				__CLASS__ . '.PROCESSFAILED',
141
				'Could not process document, please double-check you uploaded a .doc or .docx format.',
142
				'Document Converter processes Word documents into HTML.'
143
			));
144
		}
145
146
		$content = file_get_contents(ASSETS_PATH . $folderName . '/index.html');
147
148
		unlink(ASSETS_PATH . $folderName . '/index.html');
149
150
		return $content;
151
	}
152
153
}
154