TikTok::getResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 23
rs 9.7666
1
<?php
2
3
namespace Daaner\TikTok;
4
5
use Daaner\TikTok\Contracts\TikTokInterface;
6
use Daaner\TikTok\Traits\Header;
7
use Illuminate\Support\Facades\Http;
8
9
class TikTok implements TikTokInterface
10
{
11
    protected $primaryHeader;
12
13
    use Header;
14
15
    protected $baseUri = 'https://api.tiktok.com/';
16
17
    /**
18
     * TikTok constructor main settings.
19
     */
20
    public function __construct()
21
    {
22
        $this->primaryHeader = config('tiktok.primary_header');
23
    }
24
25
    /**
26
     * This default response method.
27
     *
28
     * @param string $url
29
     * @param array|null $body
30
     * @param array|null $headers
31
     *
32
     * @return array
33
     */
34
    public function getResponse($url, $body = null, $headers = null)
35
    {
36
        $header = $this->getHeader($headers);
37
38
        $response = Http::timeout(config('tiktok.tt_timeout'))
39
            ->retry(config('tiktok.tt_retry.number', 1), config('tiktok.tt_retry.time', 200))
40
            ->withHeaders($header)
41
            ->get($url, $body);
42
43
        if ($response->failed()) {
44
            return [
45
                'success' => false,
46
                'result' => null,
47
                'info' => __('tiktok::tiktok.error_data'),
48
            ];
49
        }
50
51
        $answer = $response->json();
52
53
        return [
54
            'success' => true,
55
            'result' => $answer,
56
            'info' => '',
57
        ];
58
    }
59
}
60