Completed
Push — master ( e848ca...5ddaaa )
by Nelson J
16:16 queued 10:29
created

Request::setOptions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4286
cc 3
eloc 5
nc 2
nop 1
crap 3
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 => 90,
31
        CURLOPT_HEADER => true
32
    );
33
34
    private $responseFormat = 'application/json';
35
    
36 12
    public function __construct(ResourceInterface $resource, UrlBuilderInterface $urlBuilder, FactoryInterface $factory)
37
    {
38 12
        $this->resource = $resource;
39 12
        $this->urlBuilder = $urlBuilder;
40 12
        $this->factory = $factory;
41 12
    }
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
    public function asJson()
92
    {
93
        $this->responseFormat = 'application/json';
94
        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);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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)
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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