Completed
Push — master ( 641bb6...ea06d2 )
by Nelson J
02:37
created

Request   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 0
loc 176
ccs 52
cts 64
cp 0.8125
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setOptions() 0 10 3
A getOptions() 0 4 1
A asXml() 0 5 1
A asJson() 0 5 1
B exec() 0 33 3
A getBodyContent() 0 12 3
B buildDefaultHeaders() 0 27 4
A getUserAgent() 0 9 1
1
<?php
2
3
namespace Njasm\Soundcloud\Request;
4
5
use Njasm\Soundcloud\Resource\ResourceInterface;
6
use Njasm\Soundcloud\UrlBuilder\UrlBuilderInterface;
7
8
use Njasm\Soundcloud\Soundcloud;
9
use Psr\Container\ContainerInterface;
10
11
/**
12
 * SoundCloud API wrapper in PHP
13
 *
14
 * @author      Nelson J Morais <[email protected]>
15
 * @copyright   2014 Nelson J Morais <[email protected]>
16
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
17
 * @link        http://github.com/njasm/soundcloud
18
 * @package     Njasm\Soundcloud
19
 */
20
21
class Request implements RequestInterface
22
{
23
    const VERB_GET = 'get';
24
    const VERB_PUT = 'put';
25
    const VERB_POST = 'post';
26
    const VERB_DELETE = 'delete';
27
28
    private $resource;
29
    private $urlBuilder;
30
    private $container;
31
32
    private $options = [
33
        CURLOPT_HTTPHEADER => [],
34
        CURLOPT_RETURNTRANSFER => true,
35
        CURLOPT_SSL_VERIFYPEER => false,
36
        CURLOPT_TIMEOUT => 600,
37
        CURLOPT_HEADER => true
38
    ];
39
40
    private $responseFormat = 'application/json';
41
42 11
    public function __construct(
43
        ResourceInterface $resource, UrlBuilderInterface $urlBuilder, ContainerInterface $container
44
    ) {
45 11
        $this->resource = $resource;
46 11
        $this->urlBuilder = $urlBuilder;
47 11
        $this->container = $container;
48 11
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @return Request
54
     */
55 7
    public function setOptions(array $options)
56
    {
57 7
        if (!empty($options)) {
58 7
            foreach($options as $index => $value) {
59 7
                $this->options[$index] = $value;
60
            }
61
        }
62
63 7
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @return array
70
     */
71 2
    public function getOptions()
72
    {
73 2
        return $this->options;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @deprecated Soundcloud does not support XML responses anymore.
80
     * @see https://github.com/njasm/soundcloud/issues/16
81
     *
82
     * @return Request
83
     */
84
    public function asXml()
85
    {
86
        $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...
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * @deprecated Soundcloud does not support XML responses anymore and calling this method is redundant.
94
     * @see https://github.com/njasm/soundcloud/issues/16
95
     *
96
     * @return Request
97
     */
98 7
    public function asJson()
99
    {
100 7
        $this->responseFormat = 'application/json';
101 7
        return $this;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     *
107
     * @return ResponseInterface
108
     */
109 7
    public function exec()
110
    {
111 7
        $verb = strtoupper($this->resource->getVerb());
112 7
        $this->buildDefaultHeaders();
113
114 7
        $curlHandler = curl_init();
115
116
        //curl_setopt_array($curlHandler, $this->options);
117
        // workaround for issue njasm/soundcloud#28 on github.
118
        // for some reason curl_setopt_array does not wanna work well with 7.0 on some PHP builds.
119
        // needs further investigation.
120 7
        foreach($this->options as $index => $value) {
121 7
            curl_setopt($curlHandler, $index, $value);
122
        }
123
124 7
        curl_setopt($curlHandler, CURLOPT_USERAGENT, $this->getUserAgent());
125 7
        curl_setopt($curlHandler, CURLOPT_CUSTOMREQUEST, $verb);
126 7
        curl_setopt($curlHandler, CURLOPT_URL, $this->urlBuilder->getUrl());
127
128 7
        if ($verb != 'GET') {
129
            curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $this->getBodyContent());
130
        }
131
132 7
        $response = curl_exec($curlHandler);
133 7
        $info = curl_getinfo($curlHandler);
134 7
        $errno = curl_errno($curlHandler);
135 7
        $errorString = curl_error($curlHandler);
136 7
        curl_close($curlHandler);
137
138 7
        $this->options[CURLOPT_HTTPHEADER] = [];
139
140 7
        return $this->container->get(ResponseInterface::class, [$response, $info, $errno, $errorString]);
0 ignored issues
show
Unused Code introduced by
The call to ContainerInterface::get() has too many arguments starting with array($response, $info, $errno, $errorString).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
141
    }
142
143
    protected function getBodyContent()
144
    {
145
        if (in_array('Content-Type: application/json', $this->options[CURLOPT_HTTPHEADER])) {
146
            return json_encode($this->resource->getParams());
147
        }
148
149
        if (in_array('Content-Type: application/x-www-form-urlencoded', $this->options[CURLOPT_HTTPHEADER])) {
150
            return http_build_query($this->resource->getParams());
151
        }
152
153
        return $this->resource->getParams();
154
    }
155
156 7
    protected function buildDefaultHeaders()
157
    {
158 7
        $headers = array('Accept: ' . $this->responseFormat);
159
160 7
        $data = $this->resource->getParams();
161 7
        if (isset($data['oauth_token'])) {
162
            $oauth = $data['oauth_token'];
163
            array_push($headers, 'Authorization: OAuth ' . $oauth);
164
        }
165
166
        // set default content-type if non-existent
167 7
        $found = false;
168 7
        array_map(
169 7
            function ($value) use (&$found) {
170 4
                if (stripos($value, 'content-type') !== false) {
171 4
                    $found = true;
172
                }
173 7
            },
174 7
            $this->options[CURLOPT_HTTPHEADER]
175
        );
176
177 7
        if (!$found) {
178 3
            array_push($this->options[CURLOPT_HTTPHEADER], "Content-Type: application/json");
179
        }
180
        //merge headers
181 7
        $this->options[CURLOPT_HTTPHEADER] = array_merge($this->options[CURLOPT_HTTPHEADER], $headers);
182 7
    }
183
184
    /**
185
     * @return string the User-Agent string
186
     */
187 8
    public function getUserAgent()
188
    {
189
        // Mozilla/5.0 (compatible; Njasm-Soundcloud/2.2.0; +https://www.github.com/njasm/soundcloud)
190 8
        $userAgent = "Mozilla/5.0 (compatible; ";
191 8
        $userAgent .= Soundcloud::LIB_NAME . '/' . Soundcloud::VERSION . '; +' . Soundcloud::LIB_URL;
192 8
        $userAgent .= ')';
193
194 8
        return $userAgent;
195
    }
196
}
197