GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#41)
by
unknown
04:25
created

Service::appendParametersToResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Service.php
4
 *
5
 * @package Adapters
6
 * @author Michael Pratt <[email protected]>
7
 * @link   http://www.michael-pratt.com/
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Embera\Adapters;
14
15
/**
16
 * This is an abstract class and all Service/Providers should extend it.
17
 */
18
abstract class Service
19
{
20
    /**
21
     * @var mixed Instance of \Embera\Url
22
     * (Marking this as mixed instead of object because scrutinizer complains about type check on preg_match)
23
     */
24
    protected $url;
25
26
    /** @var mixes Additional Parameters from the Original URL to be appended to the iframe src **/
27
    protected $parameters;
28
29
    /** @var object Instance of \Embera\Oembed */
30
    protected $oembed = null;
31
32
    /** @var array Associative array with config/parameters to be sent to the oembed provider */
33
    protected $config = array();
34
35
    /** @var array Array with all the errors */
36
    protected $errors = array();
37
38
    /** @var string The api url for the current service */
39
    protected $apiUrl = null;
40
41
    /**
42
     * Validates that the url belongs to this service.
43
     * Should be implemented on all children and should
44
     * return a boolean (preg_match returns 0 or 1 that
45
     * is why I'm also allowing 'int' as a return type).
46
     *
47
     * The current url is made available via $this->url
48
     *
49
     * @return bool|int
50
     */
51
    abstract protected function validateUrl();
52
53
    /**
54
     * Construct
55
     *
56
     * @param string $url
57
     * @param array  $config
58
     * @param object $oembed
59
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
60 177
     *
61
     * @throws InvalidArgumentException when the given url doesnt match the current service
62 177
     */
63 177
    public function __construct($url, array $config = array(), \Embera\Oembed $oembed)
64
    {
65 177
        $this->url = new \Embera\Url($url);
66 145
        $this->normalizeUrl();
67
68
        if (!$this->validateUrl()) {
69 177
            throw new \InvalidArgumentException('Url ' . $this->url . ' seems to be invalid for the ' . get_class($this) . ' service');
70
        }
71 177
72 177
        $this->config = array_replace_recursive(array(
73
            'params' => array(
74 177
                'maxwidth' => 0,
75
                'maxheight' => 0,
76 177
            )
77 177
        ), $config);
78
79
        $this->oembed = $oembed;
80
    }
81
82
    /**
83
     * Gets the information from an Oembed provider
84
     * when this fails, it tries to provide a fakeResponse.
85
     * Returns an associative array with a (common) Oembed response.
86 171
     *
87
     * @return array
88
     */
89
    public function getInfo()
90 171
    {
91 147
        try {
92
93
            if ($res = $this->oembed->getResourceInfo($this->config['oembed'], $this->apiUrl, (string) $this->url, $this->config['params'])) {
94 83
                $res = $this->modifyResponse($res);
95 1
                return $this->appendParametersToResponse($res);
96
            }
97
98
        } catch (\Exception $e) {
99
            $this->errors[] = $e->getMessage();
100
        }
101
102 83
        /**
103 82
         * Use fakeResponses when the oembed setting is null or false
104 82
         * If the oembed config is true, the user strictly wants real responses
105
         */
106
        if (!$this->config['oembed'] && $response = $this->fakeResponse()) {
107 1
            $fakeResponse = new \Embera\FakeResponse($this->config, $response);
108
            $res = $this->modifyResponse($fakeResponse->buildResponse());
109
            return $this->appendParametersToResponse($res);
110
        }
111
112
        return array();
113
    }
114
115
    /**
116 6
     * Appends custom parameters for the oembed request
117
     *
118 6
     * @param array $params
119 6
     * @return void
120
     */
121
    public function appendParams(array $params = array())
122
    {
123
        $this->config['params'] = array_merge($this->config['params'], $params);
124
    }
125
126 49
    /**
127
     * Returns the url
128 49
     *
129
     * @return string
130
     */
131
    public function getUrl()
132
    {
133
        return (string) $this->url;
134
    }
135
136 4
    /**
137
     * Returns an array with all the parameters for the oembed request
138 4
     *
139
     * @return array
140
     */
141
    public function getParams()
142
    {
143
        return $this->config['params'];
144
    }
145
146 30
    /**
147
     * Returns an array with found errors
148 30
     *
149
     * @return array
150
     */
151
    public function getErrors()
152
    {
153
        return $this->errors;
154
    }
155
156
    /**
157
     * This method fakes a Oembed response.
158
     *
159
     * It should be overwritten by the service
160
     * itself if the service is capable to determine
161
     * an html embed code based on the url or by other methods.
162
     *
163
     * @return array with data that the oembed response should have
164
     * @codeCoverageIgnore
165
     */
166
    public function fakeResponse()
167
    {
168
        return array();
169
    }
170
171
    /**
172
     * Normalizes a url.
173
     * This method should be overwritten by the
174
     * service itself, if needed.
175
     *
176
     * Use the $this->url property to do the job
177
     *
178
     * @return void
179
     */
180
    protected function normalizeUrl() {}
181
182
    /**
183
     * Gives the ability to modify the response/fake-response array
184
     * from an oembed provider.
185
     *
186 147
     * It should be overwritten by the service when needed
187
     *
188 147
     * @param array $response
189
     * @return array
190
     */
191
    protected function modifyResponse(array $response = array())
192
    {
193
        return $response;
194
    }
195
196
    /**
197
     * Gives the ability to modify the response/fake-response array
198
     * from an oembed provider.
199
     *
200
     * It should be overwritten by the service when needed
201
     *
202
     * @param array $response
203
     * @return array
204
     */
205
    protected function appendParametersToResponse(array $response = array())
206
    {
207
        if( $this->parameters ){
208
            preg_match('~(src="[^\"]+)(")~', $response['html'], $m);
209
            if( strpos($m[1], '?')===false )
210
                $this->parameters = '?'.substr($this->parameters, 1);
0 ignored issues
show
Documentation Bug introduced by
It seems like '?' . substr($this->parameters, 1) of type string is incompatible with the declared type object<Embera\Adapters\mixes> of property $parameters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
211
            $response['html'] = preg_replace('~(<iframe[^>]+src="[^\"]+)(")~', '$1'.$this->parameters.'$2', $response['html']);
212
        }
213
        return $response;
214
    }
215
}
216
217
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
218