Completed
Push — master ( 0f12bf...94f146 )
by Oscar
02:51
created

Upload::save()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 7
nc 5
nop 2
1
<?php
2
3
namespace Uploader\Adapters;
4
5
use Uploader\Uploader;
6
7
/**
8
 * Adapter to save a file from upload ($_FILES).
9
 */
10
class Upload implements AdapterInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function check($original)
16
    {
17
        return is_array($original) && isset($original['tmp_name']);
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 View Code Duplication
    public static function fixDestination(Uploader $uploader, $original)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $path = Uploader::parsePath($original['name']);
26
27
        if (!$uploader->getFilename()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uploader->getFilename() of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            $uploader->setFilename($path['filename']);
29
        }
30
31
        if (!$uploader->getExtension()) {
32
            $uploader->setExtension($path['extension']);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function save($original, $destination)
40
    {
41
        if (empty($original['tmp_name']) || !empty($original['error'])) {
42
            throw new \RuntimeException('Unable to copy the uploaded file because has an error');
43
        }
44
45
        $moved = php_sapi_name() == 'cli' ? rename($original['tmp_name'], $destination) : move_uploaded_file($original['tmp_name'], $destination);
46
47
        if (!$moved) {
48
            throw new \RuntimeException("Unable to copy '{$original['tmp_name']}' to '{$destination}'");
49
        }
50
51
        chmod($destination, 0755);
52
    }
53
}
54