Completed
Push — develop ( 2b0d56...088525 )
by Josef
02:15
created

TwitchRequest::getApiVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace jofner\SDK\TwitchTV;
4
5
/**
6
 * TwitchRequest for TwitchTV API SDK for PHP
7
 *
8
 * PHP SDK for interacting with the TwitchTV API
9
 *
10
 * @author Josef Ohnheiser <[email protected]>
11
 * @license https://github.com/jofner/Twitch-SDK/blob/master/LICENSE.md MIT
12
 * @homepage https://github.com/jofner/Twitch-SDK
13
 */
14
class TwitchRequest
15
{
16
    /** @var string Set the useragnet */
17
    private $userAgent = 'jofner TwitchSDK 2.*';
18
19
    /** @var integer Set connect timeout */
20
    public $connectTimeout = 30;
21
22
    /** @var integer Set timeout default. */
23
    public $timeout = 30;
24
25
    /** @var boolean Verify SSL Cert */
26
    public $sslVerifypeer = false;
27
28
    /** @var integer Contains the last HTTP status code returned */
29
    public $httpCode = 0;
30
31
    /** @var array Contains the last HTTP headers returned */
32
    public $httpInfo = array();
33
34
    /** @var array Contains the last Server headers returned */
35
    public $httpHeader = array();
36
37
    /** @var boolean Throw cURL errors */
38
    public $throwCurlErrors = true;
39
40
    /** @var int API version to use */
41
    private $apiVersion = 3;
42
43
    const URL_TWITCH = 'https://api.twitch.tv/kraken/';
44
    const URL_TWITCH_TEAM = 'http://api.twitch.tv/api/team/';
45
    const URI_AUTH = 'oauth2/authorize';
46
    const URI_AUTH_TOKEN = 'oauth2/token';
47
    const MIME_TYPE = 'application/vnd.twitchtv.v%d+json';
48
49
    /**
50
     * Set the API version to use
51
     * @param integer $version
52
     * @deprecated will be removed, force to use v3 API, which is current stable Twitch API version
53
     */
54
    public function setApiVersion($version)
55
    {
56
        if (ctype_digit(strval($version))) {
57
            $this->apiVersion = (int)$version;
58
        }
59
    }
60
61
    /**
62
     * Get the API version
63
     * @return int
64
     */
65
    public function getApiVersion()
66
    {
67
        return $this->apiVersion;
68
    }
69
70
    /**
71
     * TwitchAPI request
72
     * @param   string
73
     * @param   string
74
     * @param   string
75
     * @return  \stdClass
76
     * @throws  \jofner\SDK\TwitchTV\TwitchException
77
     */
78
    public function request($uri, $method = 'GET', $postfields = null)
79
    {
80
        $params = ['CURLOPT_SSL_VERIFYPEER'];
81
82
        return $this->generalRequest($params, self::URL_TWITCH . $uri, $method, $postfields);
83
    }
84
85
    /**
86
     * Twitch Team API request
87
     * @param   string
88
     * @param   string
89
     * @param   string
90
     * @return  \stdClass
91
     * @throws  \jofner\SDK\TwitchTV\TwitchException
92
     */
93
    public function teamRequest($uri, $method = 'GET', $postfields = null)
94
    {
95
        return $this->generalRequest([], self::URL_TWITCH_TEAM . $uri . '.json', $method, $postfields);
96
    }
97
98
    /**
99
     * TwitchAPI request
100
     * method used by teamRequest && request methods
101
     * because there are two different Twitch APIs
102
     * don't call it directly
103
     * @param   array
104
     * @param   string
105
     * @param   string
106
     * @param   string
107
     * @return  \stdClass
108
     * @throws  \jofner\SDK\TwitchTV\TwitchException
109
     */
110
    private function generalRequest($params, $uri, $method = 'GET', $postfields = null)
111
    {
112
        $this->httpInfo = array();
113
114
        $crl = curl_init();
115
        curl_setopt($crl, CURLOPT_USERAGENT, $this->userAgent);
116
        curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
117
        curl_setopt($crl, CURLOPT_TIMEOUT, $this->timeout);
118
        curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
119
        curl_setopt($crl, CURLOPT_HTTPHEADER, array('Expect:', 'Accept: ' . sprintf(self::MIME_TYPE, $this->getApiVersion())));
120
        if (array_key_exists('CURLOPT_SSL_VERIFYPEER', $params)) {
121
            curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, $this->sslVerifypeer);
122
        }
123
        curl_setopt($crl, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
124
        curl_setopt($crl, CURLOPT_HEADER, false);
125
126
        switch ($method) {
127
            case 'POST':
128
                curl_setopt($crl, CURLOPT_POST, true);
129
                if ($postfields !== null) {
130
                    curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?'));
131
                }
132
                break;
133 View Code Duplication
            case 'PUT':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
                curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'PUT');
135
                curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields)));
136
                if ($postfields !== null) {
137
                    curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?'));
138
                }
139
                break;
140 View Code Duplication
            case 'DELETE':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
                curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'DELETE');
142
                curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($postfields)));
143
                if ($postfields !== null) {
144
                    curl_setopt($crl, CURLOPT_POSTFIELDS, ltrim($postfields, '?'));
145
                }
146
        }
147
148
        curl_setopt($crl, CURLOPT_URL, $uri);
149
150
        $response = curl_exec($crl);
151
152
        $this->httpCode = curl_getinfo($crl, CURLINFO_HTTP_CODE);
153
        $this->httpInfo = array_merge($this->httpInfo, curl_getinfo($crl));
154
155
        if (curl_errno($crl) && $this->throwCurlErrors === true) {
156
            throw new TwitchException(curl_error($crl), curl_errno($crl));
157
        }
158
159
        curl_close($crl);
160
161
        return json_decode($response);
162
    }
163
164
    /**
165
     * Get the header info to store
166
     */
167
    private function getHeader($header)
168
    {
169
        $i = strpos($header, ':');
170
        if (!empty($i)) {
171
            $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
172
            $value = trim(substr($header, $i + 2));
173
            $this->httpHeader[$key] = $value;
174
        }
175
176
        return strlen($header);
177
    }
178
}
179