DataArchiveFileField::saveInto()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 6
nop 1
1
<?php
2
3
/**
4
 * Class DataArchiveFileField
5
 *
6
 * Overwrite field to save into a {@link DataArchive}, using generateFilepath().
7
 * This mainly just works around the limitation
8
 * of FileField to set the folder path *before* uploading the file,
9
 * at which point we don't have a {@link DataTransfer} ID yet, so can't generate the path.
10
 */
11
class DataArchiveFileField extends FileField {
12
13
	/**
14
	 * @param DataObjectInterface $record
15
	 * @return bool|DataArchiveFileField
16
	 */
17
	public function saveInto(DataObjectInterface $record) {
0 ignored issues
show
Coding Style introduced by
saveInto uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
18
		if(!isset($_FILES[$this->name])) {
19
			return false;
20
		}
21
22
		if(!($record instanceof DNDataArchive)) {
23
			throw new LogicException('Saving into wrong type, expected DNDataArchive');
24
		}
25
26
		$dataArchive = $record;
27
28
		/** @var DNDataTransfer $dataTransfer */
29
		$dataTransfer = $dataArchive->DataTransfers()->First();
30
		if(!$dataTransfer) {
31
			throw new LogicException('No transfer found');
32
		}
33
34
		$fileClass = File::get_class_for_file_extension(pathinfo($_FILES[$this->name]['name'], PATHINFO_EXTENSION));
35
		$file = new $fileClass();
36
		// Hack: loadIntoFile assumes paths relative to assets,
37
		//  otherwise it creates the whole structure *within* that folder
38
		$absolutePath = $dataArchive->generateFilepath($dataTransfer);
39
		$relativePath = preg_replace('#^' . preg_quote(ASSETS_PATH) . '/#', '', $absolutePath);
40
		$this->upload->loadIntoFile($_FILES[$this->name], $file, $relativePath);
0 ignored issues
show
Documentation introduced by
$relativePath is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
		if($this->upload->isError()) {
42
			return false;
43
		}
44
45
		$file = $this->upload->getFile();
46
		if($this->relationAutoSetting) {
47
			// save to record
48
			$record->{$this->name . 'ID'} = $file->ID;
49
		}
50
51
		return $this;
52
	}
53
}
54