Completed
Push — 5.x ( 239280...4f1c91 )
by Yassine
22s queued 11s
created

failOnTransferAndUploadNewChunk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
25
namespace Facebook\Tests\Fixtures;
26
27
use Facebook\Http\GraphRawResponse;
28
use Facebook\HttpClients\FacebookHttpClientInterface;
29
30
class FakeGraphApiForResumableUpload implements FacebookHttpClientInterface
31
{
32
    public $transferCount = 0;
33
    private $respondWith = 'SUCCESS';
34
35
    public function failOnStart()
36
    {
37
        $this->respondWith = 'FAIL_ON_START';
38
    }
39
40
    public function failOnTransfer()
41
    {
42
        $this->respondWith = 'FAIL_ON_TRANSFER';
43
    }
44
45
    public function failOnTransferAndUploadNewChunk()
46
    {
47
        $this->respondWith = 'FAIL_ON_TRANSFER_AND_UPLOAD_NEW_CHUNK';
48
    }
49
50
    public function send($url, $method, $body, array $headers, $timeOut)
51
    {
52
        // Could be start, transfer or finish
53
        if (strpos($body, 'transfer') !== false) {
54
            return $this->respondTransfer();
55
        } elseif (strpos($body, 'finish') !== false) {
56
            return $this->respondFinish();
57
        }
58
59
        return $this->respondStart();
60
    }
61
62
    private function respondStart()
63
    {
64
        if ($this->respondWith == 'FAIL_ON_START') {
65
            return new GraphRawResponse(
66
                "HTTP/1.1 500 OK\r\nFoo: Bar",
67
                '{"error":{"message":"Error validating access token: Session has expired on Monday, ' .
68
                '10-Aug-15 01:00:00 PDT. The current time is Monday, 10-Aug-15 01:14:23 PDT.",' .
69
                '"type":"OAuthException","code":190,"error_subcode":463}}'
70
            );
71
        }
72
73
        return new GraphRawResponse(
74
            "HTTP/1.1 200 OK\r\nFoo: Bar",
75
            '{"video_id":"1337","start_offset":"0","end_offset":"20","upload_session_id":"42"}'
76
        );
77
    }
78
79
    private function respondTransfer()
80
    {
81
        if ($this->respondWith == 'FAIL_ON_TRANSFER') {
82
            return new GraphRawResponse(
83
                "HTTP/1.1 500 OK\r\nFoo: Bar",
84
                '{"error":{"message":"There was a problem uploading your video. Please try uploading it again.",' .
85
                '"type":"FacebookApiException","code":6000,"error_subcode":1363019}}'
86
            );
87
        }
88
89
        if ($this->respondWith == 'FAIL_ON_TRANSFER_AND_UPLOAD_NEW_CHUNK') {
90
            return new GraphRawResponse(
91
                "HTTP/1.1 500 OK\r\nFoo: Bar",
92
                '{"error":{"message":"There was a problem uploading your video. Please try uploading it again.",' .
93
                '"type":"OAuthException","code":6001,"error_subcode":1363037,' .
94
                '"error_data":{"start_offset":40,"end_offset":50}}}'
95
            );
96
        }
97
98
        switch ($this->transferCount) {
99
            case 0:
100
                $data = ['start_offset' => 20, 'end_offset' => 40];
101
                break;
102
            case 1:
103
                $data = ['start_offset' => 40, 'end_offset' => 50];
104
                break;
105
            default:
106
                $data = ['start_offset' => 50, 'end_offset' => 50];
107
                break;
108
        }
109
110
        $this->transferCount++;
111
112
        return new GraphRawResponse(
113
            "HTTP/1.1 200 OK\r\nFoo: Bar",
114
            json_encode($data)
115
        );
116
    }
117
118
    private function respondFinish()
119
    {
120
        return new GraphRawResponse(
121
            "HTTP/1.1 200 OK\r\nFoo: Bar",
122
            '{"success":true}'
123
        );
124
    }
125
}
126