CurlDriver::getFileSize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 13
c 3
b 0
f 0
rs 9.4285
cc 3
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace BigFileTools\Driver;
4
use Brick\Math\BigInteger;
5
6
class CurlDriver implements ISizeDriver
7
{
8
	public function __construct()
9
	{
10
		// curl solution - cross platform and really cool :)
11
		if (!function_exists("curl_init")) {
12
			throw new PrerequisiteException("CurlDriver requires CURL extension to be loaded in PHP");
13
		}
14
	}
15
16
	/**
17
	 * Returns file size by using CURL extension
18
	 * @inheritdoc
19
	 * @link http://www.php.net/manual/en/function.filesize.php#100434
20
	 */
21
	public function getFileSize($path)
22
	{
23
		$ch = curl_init("file://" . rawurlencode($path));
24
		curl_setopt($ch, CURLOPT_NOBODY, true);
25
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
26
		curl_setopt($ch, CURLOPT_HEADER, true);
27
		$data = curl_exec($ch);
28
		curl_close($ch);
29
		if ($data !== false && preg_match('/Content-Length: (\d+)/', $data, $matches)) {
30
			return BigInteger::of($matches[1]);
31
		}
32
		throw new Exception("Curl haven't returned file size.");
33
	}
34
}
35