GithubAdapter::listContents()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Potherca\Flysystem\Github;
4
5
use League\Flysystem\Adapter\AbstractAdapter;
6
use League\Flysystem\Adapter\Polyfill\StreamedTrait;
7
use League\Flysystem\Config;
8
use League\Flysystem\Exception;
9
use League\Flysystem\Util;
10
11
/**
12
 *
13
 */
14
class GithubAdapter extends AbstractAdapter
15
{
16
    use StreamedTrait;
17
18
    const COMMITTER_MAIL = 'email';
19
    const COMMITTER_NAME = 'name';
20
21
    const VISIBILITY_PRIVATE = 'private';
22
    const VISIBILITY_PUBLIC = 'public';
23
24
    /** @var ApiInterface */
25
    private $api;
26
27
    /**
28
     * @return ApiInterface
29
     */
30
    final public function getApi()
31
    {
32
        return $this->api;
33
    }
34
35
    /**
36
     * @param ApiInterface $api
37
     */
38
    public function __construct(ApiInterface $api)
39
    {
40
        $this->api = $api;
41
    }
42
43
    /**
44
     * Write a new file.
45
     *
46
     * @param string $path
47
     * @param string $contents
48
     * @param Config $config Config object
49
     *
50
     * @return array|false false on failure file meta data on success
51
     */
52
    public function write($path, $contents, Config $config)
53
    {
54
        throw new Exception('Write action are not (yet) supported');
55
        //@TODO: return $this->getApi()->create($path, $contents);
56
    }
57
58
    /**
59
     * Update a file.
60
     *
61
     * @param string $path
62
     * @param string $contents
63
     * @param Config $config Config object
64
     *
65
     * @return array|false false on failure file meta data on success
66
     */
67
    public function update($path, $contents, Config $config)
68
    {
69
        throw new Exception('Write action are not (yet) supported');
70
        // @TODO: return $this->getApi()->update($path, $contents);
71
    }
72
73
    /**
74
     * Rename a file.
75
     *
76
     * @param string $path
77
     * @param string $newpath
78
     *
79
     * @return bool
80
     */
81
    public function rename($path, $newpath)
82
    {
83
        throw new Exception('Write action are not (yet) supported');
84
        // @TODO: return $this->getApi()->rename($path, $newPath);
85
    }
86
87
    /**
88
     * Copy a file.
89
     *
90
     * @param string $path
91
     * @param string $newpath
92
     *
93
     * @return bool
94
     */
95
    public function copy($path, $newpath)
96
    {
97
        throw new Exception('Write action are not (yet) supported');
98
        // @TODO: return $this->getApi()->copy($path, $newPath);
99
    }
100
101
    /**
102
     * Delete a file.
103
     *
104
     * @param string $path
105
     *
106
     * @return bool
107
     */
108
    public function delete($path)
109
    {
110
        throw new Exception('Write action are not (yet) supported');
111
        // @TODO: return $this->getApi()->delete($path);
112
    }
113
114
    /**
115
     * Delete a directory.
116
     *
117
     * @param string $dirname
118
     *
119
     * @return bool
120
     */
121
    public function deleteDir($dirname)
122
    {
123
        throw new Exception('Write action are not (yet) supported');
124
        // @TODO: return $this->getApi()->deleteDir($dirname);
125
    }
126
127
    /**
128
     * Create a directory.
129
     *
130
     * @param string $dirname directory name
131
     * @param Config $config
132
     *
133
     * @return array|false
134
     */
135
    public function createDir($dirname, Config $config)
136
    {
137
        throw new Exception('Write action are not (yet) supported');
138
        // @TODO: return $this->getApi()->createDir($dirname);
139
    }
140
141
    /**
142
     * Set the visibility for a file.
143
     *
144
     * @param string $path
145
     * @param string $visibility
146
     *
147
     * @return array|false file meta data
0 ignored issues
show
Documentation introduced by
Should the return type not be array|false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
148
     */
149
    public function setVisibility($path, $visibility)
150
    {
151
        // TODO: Implement setVisibility() method.
152
    }
153
154
    /**
155
     * Check that a file or directory exists in the repository
156
     *
157
     * @param string $path
158
     *
159
     * @return array|bool|null
160
     */
161
    public function has($path)
162
    {
163
        return $this->getApi()->exists($path);
164
    }
165
166
    /**
167
     * Read a file
168
     *
169
     * @param string $path
170
     *
171
     * @return array|false
172
     *
173
     * @throws \Github\Exception\ErrorException
174
     */
175
    public function read($path)
176
    {
177
        return [ApiInterface::KEY_CONTENTS => $this->getApi()->getFileContents($path)];
178
    }
179
180
    /**
181
     * List contents of a directory.
182
     *
183
     * @param string $path
184
     * @param bool $recursive
185
     *
186
     * @return array
187
     */
188
    public function listContents($path = '/', $recursive = false)
189
    {
190
        $contents = $this->getApi()->getTreeMetadata($path, $recursive);
191
192
        if ($this->isDirectoryContents($contents) === false) {
193
            $contents = [];
194
        }
195
196
        return $contents;
197
    }
198
199
    /**
200
     * Get all the meta data of a file or directory.
201
     *
202
     * @param string $path
203
     *
204
     * @return array|false
0 ignored issues
show
Documentation introduced by
Should the return type not be array|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
205
     */
206
    public function getMetadata($path)
207
    {
208
        return $this->getApi()->getMetaData($path);
209
    }
210
211
    /**
212
     * Get all the meta data of a file or directory.
213
     *
214
     * @param string $path
215
     *
216
     * @return array|false
0 ignored issues
show
Documentation introduced by
Should the return type not be array|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
217
     */
218
    public function getSize($path)
219
    {
220
        return $this->getApi()->getMetaData($path);
221
    }
222
223
    /**
224
     * Get the mimetype of a file.
225
     *
226
     * @param string $path
227
     *
228
     * @return array|false
229
     */
230
    public function getMimetype($path)
231
    {
232
        return ['mimetype' => $this->getApi()->guessMimeType($path)];
233
    }
234
235
    /**
236
     * Get the timestamp of a file.
237
     *
238
     * @param string $path
239
     *
240
     * @return array|false
241
     */
242
    public function getTimestamp($path)
243
    {
244
        return $this->getApi()->getLastUpdatedTimestamp($path);
245
    }
246
247
    /**
248
     * Get the visibility of a file.
249
     *
250
     * @param string $path
251
     *
252
     * @return array|false
253
     */
254
    public function getVisibility($path)
255
    {
256
        $recursive = false;
257
        $metadata = $this->getApi()->getTreeMetadata($path, $recursive);
258
        return $metadata[0];
259
    }
260
261
    /**
262
     * @param $contents
263
     * @return bool
264
     */
265
    private function isDirectoryContents($contents)
266
    {
267
        $isDirectory = false;
268
269
        if (is_array($contents)) {
270
            $isDirectory = array_key_exists(Api::KEY_TYPE, $contents) === false
271
                || $contents[Api::KEY_TYPE] === Api::KEY_DIRECTORY
272
            ;
273
        }
274
275
        return $isDirectory;
276
    }
277
}
278
279
/*EOF*/
280