Completed
Push — master ( d64c7f...d0ede0 )
by Nicolas
11s
created

src/Gaufrette/Adapter/Dropbox.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Gaufrette\Adapter;
4
5
use Gaufrette\Adapter;
6
use Gaufrette\Util;
7
use Gaufrette\Exception;
8
use Dropbox_API as DropboxApi;
9
use Dropbox_Exception_NotFound as DropboxNotFoundException;
10
11
@trigger_error('The '.__NAMESPACE__.'\Dropbox adapter is deprecated since version 0.4 and will be removed in 1.0. You can move to our Flysystem adapter and use their Dropbox adapter if needed.', E_USER_DEPRECATED);
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...
12
13
/**
14
 * Dropbox adapter.
15
 *
16
 * @author Markus Bachmann <[email protected]>
17
 * @author Antoine Hérault <[email protected]>
18
 * @author Leszek Prabucki <[email protected]>
19
 *
20
 * @deprecated The Dropbox adapter is deprecated since version 0.4 and will be removed in 1.0.
21
 */
22
class Dropbox implements Adapter
23
{
24
    protected $client;
25
26
    /**
27
     * @param \Dropbox_API $client The Dropbox API client
28
     */
29
    public function __construct(DropboxApi $client)
30
    {
31
        $this->client = $client;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * @throws \Dropbox_Exception_Forbidden
38
     * @throws \Dropbox_Exception_OverQuota
39
     * @throws \OAuthException
40
     */
41
    public function read($key)
42
    {
43
        try {
44
            return $this->client->getFile($key);
45
        } catch (DropboxNotFoundException $e) {
46
            return false;
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function isDirectory($key)
54
    {
55
        try {
56
            $metadata = $this->getDropboxMetadata($key);
57
        } catch (Exception\FileNotFound $e) {
58
            return false;
59
        }
60
61
        return (boolean) isset($metadata['is_dir']) ? $metadata['is_dir'] : false;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @throws \Dropbox_Exception
68
     */
69
    public function write($key, $content)
70
    {
71
        $resource = tmpfile();
72
        fwrite($resource, $content);
73
        fseek($resource, 0);
74
75
        try {
76
            $this->client->putFile($key, $resource);
0 ignored issues
show
$resource is of type resource, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        } catch (\Exception $e) {
78
            fclose($resource);
79
80
            throw $e;
81
        }
82
83
        fclose($resource);
84
85
        return Util\Size::fromContent($content);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function delete($key)
92
    {
93
        try {
94
            $this->client->delete($key);
95
        } catch (DropboxNotFoundException $e) {
96
            return false;
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function rename($sourceKey, $targetKey)
106
    {
107
        try {
108
            $this->client->move($sourceKey, $targetKey);
109
        } catch (DropboxNotFoundException $e) {
110
            return false;
111
        }
112
113
        return true;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function mtime($key)
120
    {
121
        try {
122
            $metadata = $this->getDropboxMetadata($key);
123
        } catch (Exception\FileNotFound $e) {
124
            return false;
125
        }
126
127
        return strtotime($metadata['modified']);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function keys()
134
    {
135
        $metadata = $this->client->getMetaData('/', true);
136
        if (!isset($metadata['contents'])) {
137
            return array();
138
        }
139
140
        $keys = array();
141
        foreach ($metadata['contents'] as $value) {
142
            $file = ltrim($value['path'], '/');
143
            $keys[] = $file;
144
            if ('.' !== $dirname = \Gaufrette\Util\Path::dirname($file)) {
145
                $keys[] = $dirname;
146
            }
147
        }
148
        sort($keys);
149
150
        return $keys;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function exists($key)
157
    {
158
        try {
159
            $this->getDropboxMetadata($key);
160
161
            return true;
162
        } catch (Exception\FileNotFound $e) {
163
            return false;
164
        }
165
    }
166
167
    private function getDropboxMetadata($key)
168
    {
169
        try {
170
            $metadata = $this->client->getMetaData($key, false);
171
        } catch (DropboxNotFoundException $e) {
172
            throw new Exception\FileNotFound($key, 0, $e);
173
        }
174
175
        // TODO find a way to exclude deleted files
176
        if (isset($metadata['is_deleted']) && $metadata['is_deleted']) {
177
            throw new Exception\FileNotFound($key);
178
        }
179
180
        return $metadata;
181
    }
182
}
183