Completed
Push — master ( 601585...574186 )
by Igor
03:13
created

LocalStorage::saveTemporaryFileToStorage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2
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\Storage;
14
15
/**
16
 * This is the local storage for files
17
 */
18
class LocalStorage extends Storage
19
{
20
    /**
21
     * Upload directory
22
     *
23
     * @return string
24
     */
25 35
    private function uploadDir()
26
    {
27 35
        if ($this->getFile()->isProtected()) {
28 8
            return Yii::getAlias(Yii::$app->fileManager->uploadDirProtected);
29
        } else {
30 27
            return Yii::getAlias(Yii::$app->fileManager->uploadDirUnprotected);
31
        }
32
    }
33
34
    /**
35
     * Path to the temporary directory of the file
36
     *
37
     * @param bool $realPath
38
     * @return string
39
     */
40 29
    private function dirTmp($realPath = false)
41
    {
42 29
        $file = $this->getFile();
43
44 29
        $path  = $realPath ? $this->uploadDir() : '';
45 29
        $path .= '/' . Yii::$app->fileManager->publicPath . '/tmp';
46 29
        $path .= '/' . $file->getDateOfFile();
47 29
        $path .= '/' . $file->owner_type . '/' . $file->id;
48
49 29
        return $path;
50
    }
51
52
    /**
53
     * Path to the directory of the file
54
     *
55
     * @param bool $realPath
56
     * @return string
57
     */
58 35
    private function dir($realPath = false)
59
    {
60 35
        $file = $this->getFile();
61
62 35
        if ($file->tmp) {
63 29
            return $this->dirTmp($realPath);
64
        } else {
65 32
            $path  = $realPath ? $this->uploadDir() : '';
66 32
            $path .= '/' . Yii::$app->fileManager->publicPath;
67 32
            $path .= '/' . $file->getDateOfFile();
68 32
            $path .= '/' . $file->owner_type . '/' . $file->owner_id . '/' . $file->id;
69
70 32
            return $path;
71
        }
72
    }
73
74
    /**
75
     * Path to the temporary file
76
     *
77
     * @param bool $realPath
78
     * @return string
79
     */
80 26
    private function pathTmp($realPath = false)
81
    {
82 26
        return $this->dirTmp($realPath) . '/'. $this->getFile()->name;
83
    }
84
85
    /**
86
     * Path to the file
87
     *
88
     * @param bool $realPath
89
     * @return string
90
     */
91 35
    public function path($realPath = false)
92
    {
93 35
        return $this->dir($realPath) . '/'. $this->getFile()->name;
94
    }
95
96
    /**
97
     * Save the file to the storage
98
     * If the file is temporary, then in the temporary directory
99
     *
100
     * @param string $path
101
     * @param bool $isUploadedFile File has been uploaded or manually created
102
     * @return \rkit\filemanager\models\File|bool
103
     */
104 36
    public function save($path, $isUploadedFile = true)
105
    {
106 36
        if (file_exists($path)) {
107 35
            if (FileHelper::createDirectory($this->dir(true))) {
108 35
                $isConsole = Yii::$app instanceof \yii\console\Application;
109 35
                if (!$isUploadedFile || $isConsole) {
110 35
                    $saved = rename($path, $this->path(true));
111 35
                } else {
112
                    $saved = move_uploaded_file($path, $this->path(true)); // @codeCoverageIgnore
113
                }
114
115 35
                if ($saved) {
116 35
                    return $this->getFile();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFile(); (rkit\filemanager\File) is incompatible with the return type declared by the interface rkit\filemanager\StorageInterface::save of type rkit\filemanager\models\File|boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
117
                }
118
            } // @codeCoverageIgnore
119
        } // @codeCoverageIgnore
120
121 1
        return false;
122
    }
123
124
    /**
125
     * Save the temporary file to the storage
126
     *
127
     * @return bool
128
     */
129 26
    public function saveTemporaryFileToStorage()
130
    {
131 26
        if (file_exists($this->pathTmp(true))) {
132 26
            FileHelper::copyDirectory($this->dirTmp(true), $this->dir(true));
133 26
            FileHelper::removeDirectory($this->dirTmp(true));
134 26
            return true;
135
        } // @codeCoverageIgnore
136
137 2
        return false;
138
    }
139
140 11
    public function delete()
141
    {
142 11
        FileHelper::removeDirectory($this->dir(true));
143 11
    }
144
}
145