Completed
Push — master ( 1011ea...8b8857 )
by Raza
01:44
created

UploadContent::finishUploadSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 6
crap 1
1
<?php
2
3
namespace Srmklive\Dropbox;
4
5
trait UploadContent
6
{
7
    /**
8
     * The file should be uploaded in chunks if it size exceeds the 150 MB threshold
9
     * or if the resource size could not be determined (eg. a popen() stream).
10
     *
11
     * @param string|resource $contents
12
     *
13
     * @return bool
14
     */
15 1
    protected function shouldUploadChunk($contents)
16
    {
17 1
        $size = is_string($contents) ? strlen($contents) : fstat($contents)['size'];
18
19 1
        return ($this->isPipe($contents) || ($size === null) || ($size > static::MAX_CHUNK_SIZE)) ? true : false;
20
    }
21
22
    /**
23
     * Check if the contents is a pipe stream (not seekable, no size defined).
24
     *
25
     * @param string|resource $contents
26
     *
27
     * @return bool
28
     */
29 1
    protected function isPipe($contents)
30
    {
31 1
        return is_resource($contents) ? (fstat($contents)['mode'] & 010000) != 0 : false;
32
    }
33
34
    /**
35
     * Upload file split in chunks. This allows uploading large files, since
36
     * Dropbox API v2 limits the content size to 150MB.
37
     *
38
     * The chunk size will affect directly the memory usage, so be careful.
39
     * Large chunks tends to speed up the upload, while smaller optimizes memory usage.
40
     *
41
     * @param string          $path
42
     * @param string|resource $contents
43
     * @param string          $mode
44
     * @param int             $chunkSize
45
     *
46
     * @throws \Exception
47
     *
48
     * @return array
49
     */
50 3
    public function uploadChunk($path, $contents, $mode = 'add', $chunkSize = null)
51
    {
52 3
        $chunkSize = empty($chunkSize) ? static::MAX_CHUNK_SIZE : $chunkSize;
53 3
        $stream = $contents;
54
55
        // This method relies on resources, so we need to convert strings to resource
56 3
        if (is_string($contents)) {
57 1
            $stream = fopen('php://memory', 'r+');
58 1
            fwrite($stream, $contents);
59 1
            rewind($stream);
60 1
        }
61
62 3
        $data = self::readChunk($stream, $chunkSize);
0 ignored issues
show
Bug introduced by
It seems like $stream defined by $contents on line 53 can also be of type string; however, Srmklive\Dropbox\UploadContent::readChunk() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63 3
        $cursor = null;
64
65 3
        while (!((strlen($data) < $chunkSize) || feof($stream))) {
66
            // Start upload session on first iteration, then just append on subsequent iterations
67 2
            $cursor = isset($cursor) ? $this->appendContentToUploadSession($data, $cursor) : $this->startUploadSession($data);
68 2
            $data = self::readChunk($stream, $chunkSize);
0 ignored issues
show
Bug introduced by
It seems like $stream defined by $contents on line 53 can also be of type string; however, Srmklive\Dropbox\UploadContent::readChunk() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69 2
        }
70
71
        // If there's no cursor here, our stream is small enough to a single request
72 3
        if (!isset($cursor)) {
73 1
            $cursor = $this->startUploadSession($data);
74 1
            $data = '';
75 1
        }
76
77 3
        return $this->finishUploadSession($data, $cursor, $path, $mode);
78
    }
79
80
    /**
81
     * Upload sessions allow you to upload a single file in one or more requests,
82
     * for example where the size of the file is greater than 150 MB.
83
     * This call starts a new upload session with the given data.
84
     *
85
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
86
     *
87
     * @param string $contents
88
     * @param bool   $close
89
     *
90
     * @return \Srmklive\Dropbox\DropboxUploadCounter
91
     */
92 1
    public function startUploadSession($contents, $close = false)
93
    {
94 1
        $this->setupRequest(
0 ignored issues
show
Bug introduced by
It seems like setupRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
95 1
            compact('close')
96 1
        );
97
98 1
        $this->apiEndpoint = 'files/upload_session/start';
0 ignored issues
show
Bug introduced by
The property apiEndpoint does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
99
100 1
        $this->content = $contents;
0 ignored issues
show
Bug introduced by
The property content does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
101
102 1
        $response = json_decode(
103 1
            $this->doDropboxApiContentRequest()->getBody(),
0 ignored issues
show
Bug introduced by
It seems like doDropboxApiContentRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
104
            true
105 1
        );
106
107 1
        return new DropboxUploadCounter($response['session_id'], strlen($contents));
108
    }
109
110
    /**
111
     * Append more data to an upload session.
112
     * When the parameter close is set, this call will close the session.
113
     * A single request should not upload more than 150 MB.
114
     *
115
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
116
     *
117
     * @param string               $contents
118
     * @param DropboxUploadCounter $cursor
119
     * @param bool                 $close
120
     *
121
     * @return \Srmklive\Dropbox\DropboxUploadCounter
122
     */
123 1
    public function appendContentToUploadSession($contents, DropboxUploadCounter $cursor, $close = false)
124
    {
125 1
        $this->setupRequest(compact('cursor', 'close'));
0 ignored issues
show
Bug introduced by
It seems like setupRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
126
127 1
        $this->apiEndpoint = 'files/upload_session/append_v2';
128
129 1
        $this->content = $contents;
130
131 1
        $this->doDropboxApiContentRequest()->getBody();
0 ignored issues
show
Bug introduced by
It seems like doDropboxApiContentRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
132
133 1
        $cursor->offset += strlen($contents);
134
135 1
        return $cursor;
136
    }
137
138
    /**
139
     * Finish an upload session and save the uploaded data to the given file path.
140
     * A single request should not upload more than 150 MB.
141
     *
142
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
143
     *
144
     * @param string                                 $contents
145
     * @param \Srmklive\Dropbox\DropboxUploadCounter $cursor
146
     * @param string                                 $path
147
     * @param string|array                           $mode
148
     * @param bool                                   $autorename
149
     * @param bool                                   $mute
150
     *
151
     * @return array
152
     */
153 1
    public function finishUploadSession($contents, DropboxUploadCounter $cursor, $path, $mode = 'add', $autorename = false, $mute = false)
154
    {
155 1
        $arguments = compact('cursor');
156 1
        $arguments['commit'] = compact('path', 'mode', 'autorename', 'mute');
157
158 1
        $this->setupRequest($arguments);
0 ignored issues
show
Bug introduced by
It seems like setupRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
159
160 1
        $this->apiEndpoint = 'files/upload_session/finish';
161
162 1
        $this->content = $contents;
163
164 1
        $response = $this->doDropboxApiContentRequest();
0 ignored issues
show
Bug introduced by
It seems like doDropboxApiContentRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
165
166 1
        $metadata = json_decode($response->getBody(), true);
167
168 1
        $metadata['.tag'] = 'file';
169
170 1
        return $metadata;
171
    }
172
173
    /**
174
     * Sometimes fread() returns less than the request number of bytes (for example, when reading
175
     * from network streams).  This function repeatedly calls fread until the requested number of
176
     * bytes have been read or we've reached EOF.
177
     *
178
     * @param resource $stream
179
     * @param int      $chunkSize
180
     *
181
     * @throws \Exception
182
     *
183
     * @return string
184
     */
185 3
    protected static function readChunk($stream, $chunkSize)
186
    {
187 3
        $chunk = '';
188 3
        while (!feof($stream) && $chunkSize > 0) {
189 3
            $part = fread($stream, $chunkSize);
190
191 3
            if ($part === false) {
192
                throw new \Exception('Error reading from $stream.');
193
            }
194
195 3
            $chunk .= $part;
196 3
            $chunkSize -= strlen($part);
197 3
        }
198
199 3
        return $chunk;
200
    }
201
}
202