FakeGraphApiForResumableUpload::respondFinish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 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
namespace Facebook\Tests\Fixtures;
25
26
use GuzzleHttp\Psr7\Response;
27
use Http\Client\HttpClient;
28
use Psr\Http\Message\RequestInterface;
29
30
class FakeGraphApiForResumableUpload implements HttpClient
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 sendRequest(RequestInterface $request)
46
    {
47
        $body = $request->getBody()->__toString();
48
        // Could be start, transfer or finish
49
        if (strpos($body, 'transfer') !== false) {
50
            return $this->respondTransfer();
51
        } elseif (strpos($body, 'finish') !== false) {
52
            return $this->respondFinish();
53
        }
54
55
        return $this->respondStart();
56
    }
57
58
    private function respondStart()
59
    {
60
        if ($this->respondWith == 'FAIL_ON_START') {
61
            return new Response(
62
                500,
63
                ['Foo' => 'Bar'],
64
                '{"error":{"message":"Error validating access token: Session has expired on Monday, '.
65
                '10-Aug-15 01:00:00 PDT. The current time is Monday, 10-Aug-15 01:14:23 PDT.",'.
66
                '"type":"OAuthException","code":190,"error_subcode":463}}'
67
            );
68
        }
69
70
        return new Response(
71
            200,
72
            ['Foo' => 'Bar'],
73
            '{"video_id":"1337","start_offset":"0","end_offset":"20","upload_session_id":"42"}'
74
        );
75
    }
76
77
    private function respondTransfer()
78
    {
79
        if ($this->respondWith == 'FAIL_ON_TRANSFER') {
80
            return new Response(
81
                500,
82
                ['Foo' => 'Bar'],
83
                '{"error":{"message":"There was a problem uploading your video. Please try uploading it again.",'.
84
                '"type":"ApiException","code":6000,"error_subcode":1363019}}'
85
            );
86
        }
87
88
        switch ($this->transferCount) {
89
            case 0:
90
                $data = ['start_offset' => 20, 'end_offset' => 40];
91
                break;
92
            case 1:
93
                $data = ['start_offset' => 40, 'end_offset' => 50];
94
                break;
95
            default:
96
                $data = ['start_offset' => 50, 'end_offset' => 50];
97
                break;
98
        }
99
100
        $this->transferCount++;
101
102
        return new Response(200, ['Foo' => 'Bar'], json_encode($data));
103
    }
104
105
    private function respondFinish()
106
    {
107
        return new Response(500, ['Foo' => 'Bar'], '{"success":true}');
108
    }
109
}
110