Completed
Push — master ( f15f2a...6d9f7a )
by Igor
02:25
created

LocalStorage::save()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 8.8571
cc 6
eloc 11
nc 6
nop 1
crap 6
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
 * The local storage for files
17
 *
18
 * @author Igor Romanov <[email protected]>
19
 * @since 2.0
20
 */
21
class LocalStorage extends Storage
22
{
23
    /**
24
     * Upload directory
25
     *
26
     * @return string
27
     */
28 34
    private function uploadDir()
29
    {
30 34
        if ($this->getFile()->isProtected()) {
31 12
            return Yii::getAlias(Yii::$app->fileManager->uploadDirProtected);
32
        }
33 22
        return Yii::getAlias(Yii::$app->fileManager->uploadDirUnprotected);
34
    }
35
36
    /**
37
     * Path to the temporary directory of the file
38
     *
39
     * @param bool $realPath The real path of the directory
40
     * @return string
41
     */
42 29
    private function dirTmp($realPath = false)
43
    {
44 29
        $file = $this->getFile();
45
46 29
        $path  = $realPath ? $this->uploadDir() : '';
47 29
        $path .= '/' . Yii::$app->fileManager->publicPath . '/tmp';
48 29
        $path .= '/' . $file->getDateOfFile();
49 29
        $path .= '/' . $file->owner_type . '/' . $file->id;
50
51 29
        return $path;
52
    }
53
54
    /**
55
     * Path to the directory of the file
56
     *
57
     * @param bool $realPath The real path of the directory
58
     * @return string
59
     */
60 34
    private function dir($realPath = false)
61
    {
62 34
        $file = $this->getFile();
63
64 34
        if ($file->tmp) {
65 29
            return $this->dirTmp($realPath);
66
        }
67 24
        $path  = $realPath ? $this->uploadDir() : '';
68 24
        $path .= '/' . Yii::$app->fileManager->publicPath;
69 24
        $path .= '/' . $file->getDateOfFile();
70 24
        $path .= '/' . $file->owner_type . '/' . $file->owner_id . '/' . $file->id;
71
72 24
        return $path;
73
    }
74
75
    /**
76
     * Path to the temporary file
77
     *
78
     * @param bool $realPath The real path of the file
79
     * @return string
80
     */
81 19
    private function pathTmp($realPath = false)
82
    {
83 19
        return $this->dirTmp($realPath) . '/'. $this->getFile()->name;
84
    }
85
86
    /**
87
     * Path to the file
88
     *
89
     * @param bool $realPath The real path of the file
90
     * @return string
91
     */
92 34
    public function path($realPath = false)
93
    {
94 34
        return $this->dir($realPath) . '/'. $this->getFile()->name;
95
    }
96
97
    /**
98
     * Save the file to the storage
99
     * If the file is temporary, then in the temporary directory
100
     *
101
     * @param string $path The path of the file
102
     * @return \rkit\filemanager\models\File|bool
103
     * @SuppressWarnings(PHPMD.ElseExpression)
104
     */
105 35
    public function save($path)
106
    {
107 35
        if (file_exists($path)) {
108 34
            if (FileHelper::createDirectory($this->dir(true))) {
109 34
                $isConsole = Yii::$app instanceof \yii\console\Application;
110 34
                if (!is_uploaded_file($path) || $isConsole) {
111 34
                    $saved = rename($path, $this->path(true));
112 34
                } else {
113
                    $saved = move_uploaded_file($path, $this->path(true)); // @codeCoverageIgnore
114
                }
115
116 34
                if ($saved) {
117 34
                    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...
118
                }
119
            } // @codeCoverageIgnore
120
        } // @codeCoverageIgnore
121
122 1
        return false;
123
    }
124
125
    /**
126
     * Save the temporary file to the storage
127
     *
128
     * @return bool
129
     */
130 19
    public function saveTemporaryFileToStorage()
131
    {
132 19
        if (file_exists($this->pathTmp(true))) {
133 19
            FileHelper::copyDirectory($this->dirTmp(true), $this->dir(true));
134 19
            FileHelper::removeDirectory($this->dirTmp(true));
135 19
            return true;
136
        } // @codeCoverageIgnore
137
138 2
        return false;
139
    }
140
141 8
    public function delete()
142
    {
143 8
        FileHelper::removeDirectory($this->dir(true));
144 8
    }
145
}
146