Passed
Pull Request — 5.x (#1106)
by
unknown
02:44
created

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