Completed
Push — master ( c9b397...a68c62 )
by Nelson J
03:37
created

Request::asJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Njasm\Soundcloud\Request;
4
5
use Njasm\Soundcloud\Resource\ResourceInterface;
6
use Njasm\Soundcloud\UrlBuilder\UrlBuilderInterface;
7
use Njasm\Soundcloud\Factory\FactoryInterface;
8
use Njasm\Soundcloud\Soundcloud;
9
10
/**
11
 * SoundCloud API wrapper in PHP
12
 *
13
 * @author      Nelson J Morais <[email protected]>
14
 * @copyright   2014 Nelson J Morais <[email protected]>
15
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
16
 * @link        http://github.com/njasm/soundcloud
17
 * @package     Njasm\Soundcloud
18
 */
19
20
class Request implements RequestInterface
21
{
22
    private $resource;
23
    private $urlBuilder;
24
    private $factory;
25
26
    private $options = array(
27
        CURLOPT_HTTPHEADER => array(),
28
        CURLOPT_RETURNTRANSFER => true,
29
        CURLOPT_SSL_VERIFYPEER => false,
30
        CURLOPT_TIMEOUT => 600,
31
        CURLOPT_HEADER => true
32
    );
33
34
    private $responseFormat = 'application/json';
35
36 11
    public function __construct(ResourceInterface $resource, UrlBuilderInterface $urlBuilder, FactoryInterface $factory)
37
    {
38 11
        $this->resource = $resource;
39 11
        $this->urlBuilder = $urlBuilder;
40 11
        $this->factory = $factory;
41 11
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @return Request
47
     */
48 7
    public function setOptions(array $options)
49
    {
50 7
        if (!empty($options)) {
51 7
            foreach($options as $index => $value) {
52 7
                $this->options[$index] = $value;
53 7
            }
54 7
        }
55
56 7
        return $this;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @return array
63
     */
64 2
    public function getOptions()
65
    {
66 2
        return $this->options;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @deprecated Soundcloud does not support XML responses anymore.
73
     * @see https://github.com/njasm/soundcloud/issues/16
74
     *
75
     * @return Request
76
     */
77
    public function asXml()
78
    {
79
        $this->asJson();
0 ignored issues
show
Deprecated Code introduced by
The method Njasm\Soundcloud\Request\Request::asJson() has been deprecated with message: Soundcloud does not support XML responses anymore and calling this method is redundant.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @deprecated Soundcloud does not support XML responses anymore and calling this method is redundant.
87
     * @see https://github.com/njasm/soundcloud/issues/16
88
     *
89
     * @return Request
90
     */
91 7
    public function asJson()
92
    {
93 7
        $this->responseFormat = 'application/json';
94 7
        return $this;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     *
100
     * @return ResponseInterface
101
     */
102 7
    public function exec()
103
    {
104 7
        $verb = strtoupper($this->resource->getVerb());
105 7
        $this->buildDefaultHeaders();
106
107 7
        $curlHandler = curl_init();
108
109
        //curl_setopt_array($curlHandler, $this->options);
110
        // workaround for issue njasm/soundcloud#28 on github.
111
        // for some reason curl_setopt_array does not wanna work well with 7.0 on some PHP builds.
112
        // needs further investigation.
113 7
        foreach($this->options as $index => $value) {
114 7
            curl_setopt($curlHandler, $index, $value);
115 7
        }
116
117 7
        curl_setopt($curlHandler, CURLOPT_USERAGENT, $this->getUserAgent());
118 7
        curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, $verb);
119 7
        curl_setopt($curlHandler, CURLOPT_URL, $this->urlBuilder->getUrl());
120
121 7
        if ($verb != 'GET') {
122 2
            curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $this->getBodyContent());
123 2
        }
124
125 7
        $response = curl_exec($curlHandler);
126 7
        $info = curl_getinfo($curlHandler);
127 7
        $errno = curl_errno($curlHandler);
128 7
        $errorString = curl_error($curlHandler);
129 7
        curl_close($curlHandler);
130
131 7
        $this->options[CURLOPT_HTTPHEADER] = array();
132
133 7
        return $this->factory->make('ResponseInterface', array($response, $info, $errno, $errorString));
134
    }
135
136 2
    protected function getBodyContent()
137
    {
138 2
        if (in_array('Content-Type: application/json', $this->options[CURLOPT_HTTPHEADER])) {
139 1
            return json_encode($this->resource->getParams());
140
        }
141
142 1
        if (in_array('Content-Type: application/x-www-form-urlencoded', $this->options[CURLOPT_HTTPHEADER])) {
143
            return http_build_query($this->resource->getParams());
144
        }
145
146 1
        return $this->resource->getParams();
147
    }
148
149 7
    protected function buildDefaultHeaders()
150
    {
151 7
        $headers = array('Accept: ' . $this->responseFormat);
152
153 7
        $data = $this->resource->getParams();
154 7
        if (isset($data['oauth_token'])) {
155
            $oauth = $data['oauth_token'];
156
            array_push($headers, 'Authorization: OAuth ' . $oauth);
157
        }
158
159
        // set default content-type if non-existent
160 7
        $found = false;
161 7
        array_map(
162 7
            function ($value) use (&$found) {
163 4
                if (stripos($value, 'content-type') !== false) {
164 4
                    $found = true;
165 4
                }
166 7
            },
167 7
            $this->options[CURLOPT_HTTPHEADER]
168 7
        );
169
170 7
        if (!$found) {
171 3
            array_push($this->options[CURLOPT_HTTPHEADER], "Content-Type: application/json");
172 3
        }
173
        //merge headers
174 7
        $this->options[CURLOPT_HTTPHEADER] = array_merge($this->options[CURLOPT_HTTPHEADER], $headers);
175 7
    }
176
177
    /**
178
     * @return string the User-Agent string
179
     */
180 8
    public function getUserAgent()
181
    {
182
        // Mozilla/5.0 (compatible; Njasm-Soundcloud/2.2.0; +https://www.github.com/njasm/soundcloud)
183 8
        $userAgent = "Mozilla/5.0 (compatible; ";
184 8
        $userAgent .= Soundcloud::LIB_NAME . '/' . Soundcloud::VERSION . '; +' . Soundcloud::LIB_URL;
185 8
        $userAgent .= ')';
186
187 8
        return $userAgent;
188
    }
189
}
190