FakeGraphApiForResumableUpload   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 82
rs 10
wmc 12
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A failOnStart() 0 4 1
A failOnTransfer() 0 4 1
A send() 0 11 3
A respondStart() 0 16 2
B respondTransfer() 0 29 4
A respondFinish() 0 7 1
1
<?php
2
/**
3
 * Copyright 2016 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 send($url, $method, $body, array $headers, $timeOut)
46
    {
47
        // Could be start, transfer or finish
48
        if (strpos($body, 'transfer') !== false) {
49
            return $this->respondTransfer();
50
        } elseif (strpos($body, 'finish') !== false) {
51
            return $this->respondFinish();
52
        }
53
54
        return $this->respondStart();
55
    }
56
57
    private function respondStart()
58
    {
59
        if ($this->respondWith == 'FAIL_ON_START') {
60
            return new GraphRawResponse(
61
                "HTTP/1.1 500 OK\r\nFoo: Bar",
62
                '{"error":{"message":"Error validating access token: Session has expired on Monday, ' .
63
                '10-Aug-15 01:00:00 PDT. The current time is Monday, 10-Aug-15 01:14:23 PDT.",' .
64
                '"type":"OAuthException","code":190,"error_subcode":463}}'
65
            );
66
        }
67
68
        return new GraphRawResponse(
69
            "HTTP/1.1 200 OK\r\nFoo: Bar",
70
            '{"video_id":"1337","start_offset":"0","end_offset":"20","upload_session_id":"42"}'
71
        );
72
    }
73
74
    private function respondTransfer()
75
    {
76
        if ($this->respondWith == 'FAIL_ON_TRANSFER') {
77
            return new GraphRawResponse(
78
                "HTTP/1.1 500 OK\r\nFoo: Bar",
79
                '{"error":{"message":"There was a problem uploading your video. Please try uploading it again.",' .
80
                '"type":"FacebookApiException","code":6000,"error_subcode":1363019}}'
81
            );
82
        }
83
84
        switch ($this->transferCount) {
85
            case 0:
86
                $data = ['start_offset' => 20, 'end_offset' => 40];
87
                break;
88
            case 1:
89
                $data = ['start_offset' => 40, 'end_offset' => 50];
90
                break;
91
            default:
92
                $data = ['start_offset' => 50, 'end_offset' => 50];
93
                break;
94
        }
95
96
        $this->transferCount++;
97
98
        return new GraphRawResponse(
99
            "HTTP/1.1 200 OK\r\nFoo: Bar",
100
            json_encode($data)
101
        );
102
    }
103
104
    private function respondFinish()
105
    {
106
        return new GraphRawResponse(
107
            "HTTP/1.1 200 OK\r\nFoo: Bar",
108
            '{"success":true}'
109
        );
110
    }
111
}
112