Passed
Pull Request — master (#888)
by Tobias
01:50
created

ResumableUploader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
namespace Facebook\FileUpload;
25
26
use Facebook\Authentication\AccessToken;
27
use Facebook\Exception\ResponseException;
28
use Facebook\Exception\ResumableUploadException;
29
use Facebook\Exception\SDKException;
30
use Facebook\Application;
31
use Facebook\Client;
32
use Facebook\Request;
33
34
/**
35
 *
36
 * @package Facebook
37
 */
38
class ResumableUploader
39
{
40
    /**
41
     * @var Application
42
     */
43
    protected $app;
44
45
    /**
46
     * @var string
47
     */
48
    protected $accessToken;
49
50
    /**
51
     * @var Client The Facebook client service.
52
     */
53
    protected $client;
54
55
    /**
56
     * @var string Graph version to use for this request.
57
     */
58
    protected $graphVersion;
59
60
    /**
61
     * @param Application             $app
62
     * @param Client          $client
63
     * @param AccessToken|string|null $accessToken
64
     * @param string                  $graphVersion
65
     */
66
    public function __construct(Application $app, Client $client, $accessToken, $graphVersion)
67
    {
68
        $this->app = $app;
69
        $this->client = $client;
70
        $this->accessToken = $accessToken;
71
        $this->graphVersion = $graphVersion;
72
    }
73
74
    /**
75
     * Upload by chunks - start phase
76
     *
77
     * @param string $endpoint
78
     * @param File $file
79
     *
80
     * @return TransferChunk
81
     *
82
     * @throws SDKException
83
     */
84
    public function start($endpoint, File $file)
85
    {
86
        $params = [
87
            'upload_phase' => 'start',
88
            'file_size' => $file->getSize(),
89
        ];
90
        $response = $this->sendUploadRequest($endpoint, $params);
91
92
        return new TransferChunk($file, $response['upload_session_id'], $response['video_id'], $response['start_offset'], $response['end_offset']);
93
    }
94
95
    /**
96
     * Upload by chunks - transfer phase
97
     *
98
     * @param string $endpoint
99
     * @param TransferChunk $chunk
100
     * @param boolean $allowToThrow
101
     *
102
     * @return TransferChunk
103
     *
104
     * @throws ResponseException
105
     */
106
    public function transfer($endpoint, TransferChunk $chunk, $allowToThrow = false)
107
    {
108
        $params = [
109
            'upload_phase' => 'transfer',
110
            'upload_session_id' => $chunk->getUploadSessionId(),
111
            'start_offset' => $chunk->getStartOffset(),
112
            'video_file_chunk' => $chunk->getPartialFile(),
113
        ];
114
115
        try {
116
            $response = $this->sendUploadRequest($endpoint, $params);
117
        } catch (ResponseException $e) {
118
            $preException = $e->getPrevious();
119
            if ($allowToThrow || !$preException instanceof ResumableUploadException) {
120
                throw $e;
121
            }
122
123
            // Return the same chunk entity so it can be retried.
124
            return $chunk;
125
        }
126
127
        return new TransferChunk($chunk->getFile(), $chunk->getUploadSessionId(), $chunk->getVideoId(), $response['start_offset'], $response['end_offset']);
128
    }
129
130
    /**
131
     * Upload by chunks - finish phase
132
     *
133
     * @param string $endpoint
134
     * @param string $uploadSessionId
135
     * @param array $metadata The metadata associated with the file.
136
     *
137
     * @return boolean
138
     *
139
     * @throws SDKException
140
     */
141
    public function finish($endpoint, $uploadSessionId, $metadata = [])
142
    {
143
        $params = array_merge($metadata, [
144
            'upload_phase' => 'finish',
145
            'upload_session_id' => $uploadSessionId,
146
        ]);
147
        $response = $this->sendUploadRequest($endpoint, $params);
148
149
        return $response['success'];
150
    }
151
152
    /**
153
     * Helper to make a FacebookRequest and send it.
154
     *
155
     * @param string $endpoint The endpoint to POST to.
156
     * @param array $params The params to send with the request.
157
     *
158
     * @return array
159
     */
160
    private function sendUploadRequest($endpoint, $params = [])
161
    {
162
        $request = new Request($this->app, $this->accessToken, 'POST', $endpoint, $params, null, $this->graphVersion);
163
164
        return $this->client->sendRequest($request)->getDecodedBody();
165
    }
166
}
167