Upload   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 46
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A getFileName() 0 4 1
A getTempName() 0 4 1
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\File;
15
16
use Maslosoft\Mangan\Interfaces\FileInterface;
17
use RuntimeException;
18
19
/**
20
 * Upload file helper.
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class Upload implements FileInterface
25
{
26
27
	/**
28
	 * Uploaded file name
29
	 * @var string
30
	 */
31
	private $fileName = '';
32
33
	/**
34
	 * Uploaded temporary file name
35
	 * @var string
36
	 */
37
	private $tempName = '';
38
39
	/**
40
	 * Get uploaded file data by input name
41
	 * @param string $inputName Input name from $_FILES array
42
	 * @throws RuntimeException
43
	 */
44 1
	public function __construct($inputName)
45
	{
46 1
		if (!isset($_FILES[$inputName]))
47
		{
48
			throw new RuntimeException(sprintf("Could not find uploaded file for input name `%s`", $inputName));
49
		}
50 1
		$upload = (object) $_FILES[$inputName];
51 1
		if ($upload->error)
52
		{
53
			throw new RuntimeException(sprintf("Upload failed for input name `%s`, error code: `%s`", $inputName, $upload->error), $upload->error);
54
		}
55 1
		$this->fileName = $upload->name;
56 1
		$this->tempName = $upload->tmp_name;
57 1
	}
58
59 1
	public function getFileName()
60
	{
61 1
		return $this->fileName;
62
	}
63
64 1
	public function getTempName()
65
	{
66 1
		return $this->tempName;
67
	}
68
69
}
70