Passed
Push — master ( 868bbf...054527 )
by Milan
01:31
created

Ftp   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 2
cbo 1
dl 0
loc 50
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createName() 0 4 1
A createURI() 0 4 1
A isFileExists() 0 4 1
A save() 0 10 2
A remove() 0 7 1
1
<?php
2
3
namespace h4kuna\Upload\Driver;
4
5
use h4kuna\Upload,
6
	Nette\Http;
7
8
class Ftp implements Upload\IDriver
9
{
10
	/** @var string */
11
	private $hostUrl;
12
13
	/** @var \Ftp */
14
	private $ftp;
15
16
	public function __construct($hostUrl, \Ftp $ftp)
17
	{
18
		$this->hostUrl = $hostUrl;
19
		$this->ftp = $ftp;
20
	}
21
22
	public function createName(Http\FileUpload $fileUpload)
23
	{
24
		return null;
25
	}
26
27
	public function createURI($relativePath)
28
	{
29
		return $this->hostUrl . '/' . Upload\Utils::makeRelativePath($relativePath);
30
	}
31
32
	public function isFileExists($relativePath)
33
	{
34
		return $this->ftp->fileExists(Upload\Utils::makeRelativePath($relativePath));
35
	}
36
37
	public function save(Http\FileUpload $fileUpload, $relativePath)
38
	{
39
		$path = Upload\Utils::makeRelativePath($relativePath);
40
		$dir = dirname($relativePath);
41
		if($dir !== '.') {
42
			$this->ftp->mkDirRecursive($dir);
43
		}
44
		$this->ftp->put($path, $fileUpload->getTemporaryFile(), FTP_BINARY);
45
		@unlink($fileUpload->getTemporaryFile());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
46
	}
47
48
	public function remove($relativePath)
49
	{
50
		$path = Upload\Utils::makeRelativePath($relativePath);
51
		$this->ftp->delete($path);
52
53
		return !$this->isFileExists($path);
54
	}
55
56
57
}