Completed
Push — master ( ebcd99...f61a47 )
by Hong
02:11
created

LocalDriver   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 201
Duplicated Lines 16.42 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 26
c 6
b 0
f 1
lcom 2
cbo 4
dl 33
loc 201
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A realPath() 0 5 2
A realExists() 0 4 1
A openReadStream() 0 13 2
A readFile() 0 14 2
A getRealMeta() 0 18 2
A ensurePath() 0 8 2
A writeStream() 16 16 4
A writeFile() 17 17 3
A setRealMeta() 0 15 3
A renameFile() 0 6 1
A copyFile() 0 6 1
A deleteFile() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Driver;
16
17
use Phossa2\Storage\Message\Message;
18
use Phossa2\Storage\Traits\LocalDirTrait;
19
use Phossa2\Storage\Exception\LogicException;
20
use Phossa2\Storage\Interfaces\PermissionAwareInterface;
21
22
/**
23
 * LocalDriver
24
 *
25
 * @package Phossa2\Storage
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     DriverAbstract
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
class LocalDriver extends DriverAbstract
32
{
33
    use LocalDirTrait;
34
35
    /**
36
     * driver root
37
     *
38
     * @var    string
39
     * @access protected
40
     */
41
    protected $root;
42
43
    /**
44
     * @param  string $rootDir
45
     * @throws LogicException
46
     * @access public
47
     */
48
    public function __construct(/*# string */ $rootDir)
49
    {
50
        if (!$this->makeDirectory($rootDir)) {
51
            throw new LogicException(
52
                $this->getError(), $this->getErrorCode()
53
            );
54
        }
55
        $this->root = rtrim($rootDir, "/\\") . \DIRECTORY_SEPARATOR;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    protected function realPath(/*# string */ $path)/*# : string */
62
    {
63
        return $this->root . ('/' !== \DIRECTORY_SEPARATOR ?
64
            str_replace('/', \DIRECTORY_SEPARATOR, $path) : $path);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    protected function realExists(/*# string */ $realPath)/*# : bool */
71
    {
72
        return file_exists($realPath);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    protected function openReadStream(/*# string */ $realPath)
79
    {
80
        $stream = fopen($realPath, 'r');
81
        if (is_resource($stream)) {
82
            return $stream;
83
        } else {
84
            $this->setError(
85
                Message::get(Message::STR_OPENSTREAM_FAIL, $realPath),
86
                Message::STR_OPENSTREAM_FAIL
87
            );
88
            return null;
89
        }
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    protected function readFile(/*# string */ $realPath)
96
    {
97
        $str = file_get_contents($realPath);
98
99
        if (false === $str) {
100
            $this->setError(
101
                Message::get(Message::STR_OPENSTREAM_FAIL, $realPath),
102
                Message::STR_OPENSTREAM_FAIL
103
            );
104
            return null;
105
        }
106
107
        return $str;
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    protected function getRealMeta(/*# string */ $realPath)/*# : array */
114
    {
115
        try {
116
            $info = new \SplFileInfo($realPath);
117
            return [
118
                'size'  => $info->getSize(),
119
                'perm'  => $info->getPerms() & PermissionAwareInterface::PERM_ALL,
120
                'ctime' => $info->getCTime(),
121
                'mtime' => $info->getMTime(),
122
            ];
123
        } catch (\Exception $e) {
124
            $this->setError(
125
                Message::get(Message::STR_GETMETA_FAIL, $realPath),
126
                Message::STR_GETMETA_FAIL
127
            );
128
            return [];
129
        }
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135
    protected function ensurePath(/*# string */ $realPath)/*# : bool */
136
    {
137
        $parent = dirname($realPath);
138
        if (!$this->makeDirectory($parent)) {
139
            return false;
140
        }
141
        return true;
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147 View Code Duplication
    protected function writeStream(
148
        /*# string */ $realPath,
149
        $resource
150
    )/*# : bool */ {
151
        $tmpfname = tempnam(dirname($realPath), 'FOO');
152
        if (false !== $tmpfname) {
153
            $stream = fopen($tmpfname, 'w+b');
154
            if (is_resource($stream)) {
155
                stream_copy_to_stream($resource, $stream);
156
                fclose($stream);
157
                return rename($tmpfname, $realPath) &&
158
                    chmod($realPath, PermissionAwareInterface::PERM_ALL);
159
            }
160
        }
161
        return false;
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     */
167 View Code Duplication
    protected function writeFile(
168
        /*# string */ $realPath,
169
        /*# string */ $content
170
    )/*# : bool */ {
171
        // write to a temp file
172
        $tmpfname = tempnam(dirname($realPath), 'FOO');
173
        if (false !== $tmpfname) {
174
            $handle = fopen($tmpfname, 'w');
175
            fwrite($handle, $content);
176
            fclose($handle);
177
178
            // rename to $realPath
179
            return rename($tmpfname, $realPath) &&
180
                chmod($realPath, PermissionAwareInterface::PERM_ALL);
181
        }
182
        return false;
183
    }
184
185
    /**
186
     * {@inheritDoc}
187
     */
188
    protected function setRealMeta(
189
        /*# string */ $realPath,
190
        array $meta
191
    )/*# : bool */ {
192
        clearstatcache(true, $realPath);
193
194
        if (isset($meta['mtime'])) {
195
            touch($realPath, $meta['mtime']);
196
        }
197
198
        if (isset($meta['perm'])) {
199
            chmod($realPath, (int) $meta['perm']);
200
        }
201
        return true;
202
    }
203
204
    /**
205
     * {@inheritDoc}
206
     */
207
    protected function renameFile(
208
        /*# string */ $from,
209
        /*# string */ $to
210
    )/*# : bool */ {
211
        return rename($from, $to);
212
    }
213
214
    /**
215
     * {@inheritDoc}
216
     */
217
    protected function copyFile(
218
        /*# string */ $from,
219
        /*# string */ $to
220
    )/*# : bool */ {
221
        return copy($from, $to);
222
    }
223
224
    /**
225
     * {@inheritDoc}
226
     */
227
    protected function deleteFile(/*# string */ $realPath)/*# : bool */
228
    {
229
        return unlink($realPath);
230
    }
231
}
232