Completed
Push — master ( cd64a1...cab8e0 )
by Hong
02:41
created

LocalDirTrait::deleteDir()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 8.5906
nc 8
cc 5
eloc 15
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
        $res = $this->makeDirectory($to);
81
82
        $files = $this->readDir($from);
83
        foreach ($files as $file) {
84
            if (false === $res) {
85
                return false;
86
            }
87
88
            $f = $from . \DIRECTORY_SEPARATOR . $file;
89
            $t = $to . \DIRECTORY_SEPARATOR . $file;
90
            if (is_dir($f)) {
91
                $res = $this->copyDir($f, $t);
92
            } else {
93
                $res = @copy($f, $t);
94
            }
95
        }
96
        return $res;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    protected function deleteDir(
103
        /*# string */ $realPath,
104
        /*# bool */ $keep = false
105
    )/*# : bool */ {
106
        $pref  = rtrim($realPath, '/\\') . \DIRECTORY_SEPARATOR;
107
        $files = $this->readDir($realPath, $pref);
108
109
        foreach ($files as $file) {
110
            if (is_dir($file)) {
111
                $res = $this->deleteDir($file);
112
            } else {
113
                $res = unlink($file);
114
            }
115
            if (false === $res) {
116
                return $this->setError(
117
                    Message::get(Message::STR_DELETE_FAIL, $file),
118
                    Message::STR_DELETE_FAIL
119
                );
120
            }
121
        }
122
123
        return $keep ? true : rmdir($realPath);
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    protected function makeDirectory(/*# string */ $realPath)/*# : bool */
130
    {
131
        if (!is_dir($realPath)) {
132
            $umask = umask(0);
133
            @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...
134
            umask($umask);
135
136 View Code Duplication
            if (!is_dir($realPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
137
                $this->setError(
138
                    Message::get(Message::STR_MKDIR_FAIL, $realPath),
139
                    Message::STR_MKDIR_FAIL
140
                );
141
                return false;
142
            }
143
        }
144
        return true;
145
    }
146
147
    abstract public function setError(
148
        /*# string */ $message = '',
149
        /*# string */ $code = ''
150
    )/*# : bool */;
151
}