Completed
Push — master ( 64094d...91e60d )
by Hong
02:16
created

LocalDirTrait::deleteDir()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 10
nc 3
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Storage
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Storage\Traits;
16
17
use Phossa2\Storage\Message\Message;
18
19
/**
20
 * LocalDirTrait
21
 *
22
 * Dealing with directories in LocalDriver
23
 *
24
 * @package Phossa2\Storage
25
 * @author  Hong Zhang <[email protected]>
26
 * @version 2.0.0
27
 * @since   2.0.0 added
28
 */
29
trait LocalDirTrait
30
{
31
    /**
32
     * {@inheritDoc}
33
     */
34
    protected function isRealDir(/*# string */ $realPath)/*# : bool */
35
    {
36
        return is_dir($realPath);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function readDir(
43
        /*# string */ $realPath,
44
        /*# string */ $prefix = ''
45
    )/*# : array */ {
46
        try {
47
            $res = [];
48
            foreach (new \DirectoryIterator($realPath) as $fileInfo) {
49
                if($fileInfo->isDot()) continue;
50
                $res[] = $prefix . $fileInfo->getFilename();
51
            }
52
            return $res;
53
54
        } catch (\Exception $e) {
55
            $this->setError(
56
                Message::get(Message::STR_READDIR_FAIL, $realPath),
57
                Message::STR_READDIR_FAIL
58
            );
59
            return [];
60
        }
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    protected function renameDir(
67
        /*# string */ $from,
68
        /*# string */ $to
69
    )/*# : bool */ {
70
        return @rename($from, $to);
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    protected function copyDir(
77
        /*# string */ $from,
78
        /*# string */ $to
79
    )/*# : bool */ {
80
        $this->makeDirectory($to);
81
        foreach ($this->readDir($from) as $file) {
82
            $f = $from . \DIRECTORY_SEPARATOR . $file;
83
            $t = $to . \DIRECTORY_SEPARATOR . $file;
84
            $res = is_dir($f) ? $this->copyDir($f, $t) : @copy($f, $t);
85
            if (false === $res) {
86
                return $this->setError(
87
                    Message::get(Message::STR_COPY_FAIL, $f),
88
                    Message::STR_COPY_FAIL
89
                );
90
            }
91
        }
92
        return true;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    protected function deleteDir(
99
        /*# string */ $realPath,
100
        /*# bool */ $keep = false
101
    )/*# : bool */ {
102
        $pref = rtrim($realPath, '/\\') . \DIRECTORY_SEPARATOR;
103
        foreach ($this->readDir($realPath, $pref) as $file) {
104
            if (! (is_dir($file) ? $this->deleteDir($file) : unlink($file))) {
105
                return $this->setError(
106
                    Message::get(Message::STR_DELETE_FAIL, $file),
107
                    Message::STR_DELETE_FAIL
108
                );
109
            }
110
        }
111
        return $this->removeDir($realPath, $keep);
112
    }
113
114
    /**
115
     * Remove the directory itself
116
     *
117
     * @param  string $realPath
118
     * @param  bool $keep
119
     * @return bool
120
     * @access protected
121
     */
122 View Code Duplication
    protected function removeDir(
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...
123
        /*# string */ $realPath,
124
        /*# bool */ $keep = false
125
    )/*# : bool */ {
126
        if ($keep || @rmdir($realPath)) {
127
            return true;
128
        } else {
129
            return $this->setError(
130
                Message::get(Message::STR_DELETE_FAIL, $realPath),
131
                Message::STR_DELETE_FAIL
132
            );
133
        }
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139
    protected function makeDirectory(/*# string */ $realPath)/*# : bool */
140
    {
141
        if (!is_dir($realPath)) {
142
            $umask = umask(0);
143
            @mkdir($realPath, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
144
            umask($umask);
145
146
            if (!is_dir($realPath)) {
147
                $this->setError(
148
                    Message::get(Message::STR_MKDIR_FAIL, $realPath),
149
                    Message::STR_MKDIR_FAIL
150
                );
151
                return false;
152
            }
153
        }
154
        return true;
155
    }
156
157
    abstract public function setError(
158
        /*# string */ $message = '',
159
        /*# string */ $code = ''
160
    )/*# : bool */;
161
}