LocalDirTrait::readDir()   A
last analyzed

Complexity

Conditions 4
Paths 7

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 15
nc 7
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()) {
50
                    continue;
51
                }
52
                $res[] = $prefix . $fileInfo->getFilename();
53
            }
54
            return $res;
55
        } catch (\Exception $e) {
56
            $this->setError(
57
                Message::get(Message::STR_READDIR_FAIL, $realPath),
58
                Message::STR_READDIR_FAIL
59
            );
60
            return [];
61
        }
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    protected function renameDir(
68
        /*# string */ $from,
69
        /*# string */ $to
70
    )/*# : bool */ {
71
        return @rename($from, $to);
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    protected function copyDir(
78
        /*# string */ $from,
79
        /*# string */ $to
80
    )/*# : bool */ {
81
        $this->makeDirectory($to);
82
        foreach ($this->readDir($from) as $file) {
83
            $f = $from . \DIRECTORY_SEPARATOR . $file;
84
            $t = $to . \DIRECTORY_SEPARATOR . $file;
85
            $res = is_dir($f) ? $this->copyDir($f, $t) : @copy($f, $t);
86
            if (false === $res) {
87
                return $this->setError(
88
                    Message::get(Message::STR_COPY_FAIL, $f),
89
                    Message::STR_COPY_FAIL
90
                );
91
            }
92
        }
93
        return true;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    protected function deleteDir(
100
        /*# string */ $realPath,
101
        /*# bool */ $keep = false
102
    )/*# : bool */ {
103
        $pref = rtrim($realPath, '/\\') . \DIRECTORY_SEPARATOR;
104
        foreach ($this->readDir($realPath, $pref) as $file) {
105
            if (! (is_dir($file) ? $this->deleteDir($file) : unlink($file))) {
106
                return $this->setError(
107
                    Message::get(Message::STR_DELETE_FAIL, $file),
108
                    Message::STR_DELETE_FAIL
109
                );
110
            }
111
        }
112
        return $this->removeDir($realPath, $keep);
113
    }
114
115
    /**
116
     * Remove the directory itself
117
     *
118
     * @param  string $realPath
119
     * @param  bool $keep
120
     * @return bool
121
     * @access protected
122
     */
123 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...
124
        /*# string */ $realPath,
125
        /*# bool */ $keep = false
126
    )/*# : bool */ {
127
        if ($keep || @rmdir($realPath)) {
128
            return true;
129
        } else {
130
            return $this->setError(
131
                Message::get(Message::STR_DELETE_FAIL, $realPath),
132
                Message::STR_DELETE_FAIL
133
            );
134
        }
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    protected function makeDirectory(/*# string */ $realPath)/*# : bool */
141
    {
142
        if (!is_dir($realPath)) {
143
            $umask = umask(0);
144
            @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...
145
            umask($umask);
146
147
            if (!is_dir($realPath)) {
148
                $this->setError(
149
                    Message::get(Message::STR_MKDIR_FAIL, $realPath),
150
                    Message::STR_MKDIR_FAIL
151
                );
152
                return false;
153
            }
154
        }
155
        return true;
156
    }
157
158
    abstract public function setError(
159
        /*# string */ $message = '',
160
        /*# string */ $code = ''
161
    )/*# : bool */;
162
}
163