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 (#31)
by Jérôme
10:19 queued 07:48
created

Service::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
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 object Instance of \Embera\Oembed */
27
    protected $oembed = null;
28
29
    /** @var array Associative array with config/parameters to be sent to the oembed provider */
30
    protected $config = array();
31
32
    /** @var array Array with all the errors */
33
    protected $errors = array();
34
35
    /** @var string The api url for the current service */
36
    protected $apiUrl = null;
37
38
    /**
39
     * Validates that the url belongs to this service.
40
     * Should be implemented on all children and should
41
     * return a boolean (preg_match returns 0 or 1 that
42
     * is why I'm also allowing 'int' as a return type).
43
     *
44
     * The current url is made available via $this->url
45
     *
46
     * @return bool|int
47
     */
48
    abstract protected function validateUrl();
49
50
    /**
51
     * Construct
52
     *
53
     * @param string $url
54
     * @param array  $config
55
     * @param object $oembed
56
     * @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...
57
     *
58
     * @throws InvalidArgumentException when the given url doesnt match the current service
59
     */
60 168
    public function __construct($url, array $config = array(), \Embera\Oembed $oembed)
61
    {
62 168
        $this->url = new \Embera\Url($url);
63 168
        $this->normalizeUrl();
64
65 168
        if (!$this->validateUrl()) {
66 140
            throw new \InvalidArgumentException('Url ' . $this->url . ' seems to be invalid for the ' . get_class($this) . ' service');
67
        }
68
69 168
        $this->config = array_replace_recursive(array(
70
            'params' => array(
71 168
                'maxwidth' => 0,
72 168
                'maxheight' => 0,
73
            )
74 168
        ), $config);
75
76 168
        $this->oembed = $oembed;
77 168
    }
78
79
    /**
80
     * Gets the information from an Oembed provider
81
     * when this fails, it tries to provide a fakeResponse.
82
     * Returns an associative array with a (common) Oembed response.
83
     *
84
     * @return array
85
     */
86 162
    public function getInfo()
87
    {
88
        try {
89
90 162
            if ($res = $this->oembed->getResourceInfo($this->config['oembed'], $this->apiUrl, (string) $this->url, $this->config['params'])) {
91 142
                return $this->modifyResponse($res);
92
            }
93
94 77
        } catch (\Exception $e) {
95
            $this->errors[] = $e->getMessage();
96
        }
97
98
        /**
99
         * Use fakeResponses when the oembed setting is null or false
100
         * If the oembed config is true, the user strictly wants real responses
101
         */
102 77
        if (!$this->config['oembed'] && $response = $this->fakeResponse()) {
103 77
            $fakeResponse = new \Embera\FakeResponse($this->config, $response);
104 77
            return $this->modifyResponse($fakeResponse->buildResponse());
105
        }
106
107
        return array();
108
    }
109
110
    /**
111
     * Appends custom parameters for the oembed request
112
     *
113
     * @param array $params
114
     * @return void
115
     */
116 6
    public function appendParams(array $params = array())
117
    {
118 6
        $this->config['params'] = array_merge($this->config['params'], $params);
119 6
    }
120
121
    /**
122
     * Returns the url
123
     *
124
     * @return string
125
     */
126 49
    public function getUrl()
127
    {
128 49
        return (string) $this->url;
129
    }
130
131
    /**
132
     * Returns an array with all the parameters for the oembed request
133
     *
134
     * @return array
135
     */
136 4
    public function getParams()
137
    {
138 4
        return $this->config['params'];
139
    }
140
141
    /**
142
     * Returns an array with found errors
143
     *
144
     * @return array
145
     */
146 26
    public function getErrors()
147
    {
148 26
        return $this->errors;
149
    }
150
151
    /**
152
     * This method fakes a Oembed response.
153
     *
154
     * It should be overwritten by the service
155
     * itself if the service is capable to determine
156
     * an html embed code based on the url or by other methods.
157
     *
158
     * @return array with data that the oembed response should have
159
     * @codeCoverageIgnore
160
     */
161
    public function fakeResponse()
162
    {
163
        return array();
164
    }
165
166
    /**
167
     * Normalizes a url.
168
     * This method should be overwritten by the
169
     * service itself, if needed.
170
     *
171
     * Use the $this->url property to do the job
172
     *
173
     * @return void
174
     */
175
    protected function normalizeUrl() {}
176
177
    /**
178
     * Gives the ability to modify the response/fake-response array
179
     * from an oembed provider.
180
     *
181
     * It should be overwritten by the service when needed
182
     *
183
     * @param array $response
184
     * @return array
185
     */
186 140
    protected function modifyResponse(array $response = array())
187
    {
188 140
        return $response;
189
    }
190
}
191
192
?>
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...
193