Code Duplication    Length = 176-176 lines in 2 locations

src/Gaufrette/Adapter/Local/src/LocalStream.php 1 location

@@ 13-188 (lines=176) @@
10
 *
11
 * @author Antoine Hérault <[email protected]>
12
 */
13
class LocalStream implements Stream
14
{
15
    private $path;
16
    private $mode;
17
    private $fileHandle;
18
19
    /**
20
     * @param string $path
21
     */
22
    public function __construct($path)
23
    {
24
        $this->path = $path;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function open(StreamMode $mode)
31
    {
32
        $baseDirPath = \Gaufrette\Util\Path::dirname($this->path);
33
        if ($mode->allowsWrite() && !is_dir($baseDirPath)) {
34
            @mkdir($baseDirPath, 0755, true);
35
        }
36
        try {
37
            $fileHandle = @fopen($this->path, $mode->getMode());
38
        } catch (\Exception $e) {
39
            $fileHandle = false;
40
        }
41
42
        if (false === $fileHandle) {
43
            throw new \RuntimeException(sprintf('File "%s" cannot be opened', $this->path));
44
        }
45
46
        $this->mode = $mode;
47
        $this->fileHandle = $fileHandle;
48
49
        return true;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function read($count)
56
    {
57
        if (!$this->fileHandle) {
58
            return false;
59
        }
60
61
        if (false === $this->mode->allowsRead()) {
62
            throw new \LogicException('The stream does not allow read.');
63
        }
64
65
        return fread($this->fileHandle, $count);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function write($data)
72
    {
73
        if (!$this->fileHandle) {
74
            return false;
75
        }
76
77
        if (false === $this->mode->allowsWrite()) {
78
            throw new \LogicException('The stream does not allow write.');
79
        }
80
81
        return fwrite($this->fileHandle, $data);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function close()
88
    {
89
        if (!$this->fileHandle) {
90
            return false;
91
        }
92
93
        $closed = fclose($this->fileHandle);
94
95
        if ($closed) {
96
            $this->mode = null;
97
            $this->fileHandle = null;
98
        }
99
100
        return $closed;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function flush()
107
    {
108
        if ($this->fileHandle) {
109
            return fflush($this->fileHandle);
110
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function seek($offset, $whence = SEEK_SET)
119
    {
120
        if ($this->fileHandle) {
121
            return 0 === fseek($this->fileHandle, $offset, $whence);
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function tell()
131
    {
132
        if ($this->fileHandle) {
133
            return ftell($this->fileHandle);
134
        }
135
136
        return false;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function eof()
143
    {
144
        if ($this->fileHandle) {
145
            return feof($this->fileHandle);
146
        }
147
148
        return true;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function stat()
155
    {
156
        if ($this->fileHandle) {
157
            return fstat($this->fileHandle);
158
        } elseif (!is_resource($this->fileHandle) && is_dir($this->path)) {
159
            return stat($this->path);
160
        }
161
162
        return false;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function cast($castAs)
169
    {
170
        if ($this->fileHandle) {
171
            return $this->fileHandle;
172
        }
173
174
        return false;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function unlink()
181
    {
182
        if ($this->mode && $this->mode->impliesExistingContentDeletion()) {
183
            return @unlink($this->path);
184
        }
185
186
        return false;
187
    }
188
}
189

src/Gaufrette/Stream/Local.php 1 location

@@ 15-190 (lines=176) @@
12
 *
13
 * @author Antoine Hérault <[email protected]>
14
 */
15
class Local implements Stream
16
{
17
    private $path;
18
    private $mode;
19
    private $fileHandle;
20
21
    /**
22
     * @param string $path
23
     */
24
    public function __construct($path)
25
    {
26
        $this->path = $path;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function open(StreamMode $mode)
33
    {
34
        $baseDirPath = \Gaufrette\Util\Path::dirname($this->path);
35
        if ($mode->allowsWrite() && !is_dir($baseDirPath)) {
36
            @mkdir($baseDirPath, 0755, true);
37
        }
38
        try {
39
            $fileHandle = @fopen($this->path, $mode->getMode());
40
        } catch (\Exception $e) {
41
            $fileHandle = false;
42
        }
43
44
        if (false === $fileHandle) {
45
            throw new \RuntimeException(sprintf('File "%s" cannot be opened', $this->path));
46
        }
47
48
        $this->mode = $mode;
49
        $this->fileHandle = $fileHandle;
50
51
        return true;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function read($count)
58
    {
59
        if (!$this->fileHandle) {
60
            return false;
61
        }
62
63
        if (false === $this->mode->allowsRead()) {
64
            throw new \LogicException('The stream does not allow read.');
65
        }
66
67
        return fread($this->fileHandle, $count);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function write($data)
74
    {
75
        if (!$this->fileHandle) {
76
            return false;
77
        }
78
79
        if (false === $this->mode->allowsWrite()) {
80
            throw new \LogicException('The stream does not allow write.');
81
        }
82
83
        return fwrite($this->fileHandle, $data);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function close()
90
    {
91
        if (!$this->fileHandle) {
92
            return false;
93
        }
94
95
        $closed = fclose($this->fileHandle);
96
97
        if ($closed) {
98
            $this->mode = null;
99
            $this->fileHandle = null;
100
        }
101
102
        return $closed;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function flush()
109
    {
110
        if ($this->fileHandle) {
111
            return fflush($this->fileHandle);
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function seek($offset, $whence = SEEK_SET)
121
    {
122
        if ($this->fileHandle) {
123
            return 0 === fseek($this->fileHandle, $offset, $whence);
124
        }
125
126
        return false;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function tell()
133
    {
134
        if ($this->fileHandle) {
135
            return ftell($this->fileHandle);
136
        }
137
138
        return false;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function eof()
145
    {
146
        if ($this->fileHandle) {
147
            return feof($this->fileHandle);
148
        }
149
150
        return true;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function stat()
157
    {
158
        if ($this->fileHandle) {
159
            return fstat($this->fileHandle);
160
        } elseif (!is_resource($this->fileHandle) && is_dir($this->path)) {
161
            return stat($this->path);
162
        }
163
164
        return false;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function cast($castAs)
171
    {
172
        if ($this->fileHandle) {
173
            return $this->fileHandle;
174
        }
175
176
        return false;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function unlink()
183
    {
184
        if ($this->mode && $this->mode->impliesExistingContentDeletion()) {
185
            return @unlink($this->path);
186
        }
187
188
        return false;
189
    }
190
}
191