Completed
Push — master ( 6d9f7a...3b1f1a )
by Igor
05:47
created

LocalStorage::saveFile()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 2
crap 5
1
<?php
2
3
/**
4
 * @link https://github.com/rkit/filemanager-yii2
5
 * @copyright Copyright (c) 2015 Igor Romanov
6
 * @license [MIT](http://opensource.org/licenses/MIT)
7
 */
8
9
namespace rkit\filemanager\storages;
10
11
use Yii;
12
use yii\helpers\FileHelper;
13
use rkit\filemanager\StorageInterface;
14
15
/**
16
 * The local storage for files
17
 *
18
 * @author Igor Romanov <[email protected]>
19
 * @since 2.0
20
 */
21
class LocalStorage implements StorageInterface
22
{
23
    /**
24
     * Save the file to the storage
25
     *
26
     * @param string $source File path
27
     * @param string $destination Destination path
28
     * @return bool
29
     */
30 28
    public function saveFile($source, $destination)
31
    {
32 28
        if (file_exists($source)) {
33 27
            if (FileHelper::createDirectory(dirname($destination))) {
34 27
                $isConsole = Yii::$app instanceof \yii\console\Application;
35 27
                if (!is_uploaded_file($source) || $isConsole) {
36 27
                    return rename($source, $destination);
37
                }
38
                return move_uploaded_file($source, $destination); // @codeCoverageIgnore
39
            } // @codeCoverageIgnore
40
        } // @codeCoverageIgnore
41 1
        return false;
42
    }
43
44
    /**
45
     * Deletes the file from the storage
46
     *
47
     * @param string $path File path
48
     * @return bool
49
     */
50 2
    public function deleteFile($path)
51
    {
52 2
        if (file_exists($path)) {
53 1
            return unlink($path);
54
        } // @codeCoverageIgnore
55 1
        return false;
56
    }
57
58
    /**
59
     * Deletes the directory from the storage
60
     *
61
     * @param string $path Directory path
62
     */
63 7
    public function deleteDir($path)
64
    {
65 7
        FileHelper::removeDirectory(dirname($path));
66 7
    }
67
}
68