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 (#37)
by
unknown
02:36
created

Providers::findServices()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.2
cc 4
eloc 11
nc 4
nop 1
crap 4
1
<?php
2
/**
3
 * Providers.php
4
 *
5
 * @package Embera
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;
14
15
/**
16
 * Finds and loads Services/Providers based on the
17
 * host of a url.
18
 */
19
class Providers
20
{
21
    /** @var array Configuration Settings */
22
    protected $config = array();
23
24
    /** @var object instance of \Embera\Oembed*/
25
    protected $oembed;
26
27
    /** @var array Custom parameters for each host/provider */
28
    protected $customParams = array();
29
30
    /** @var array Hosts with wildcards, calculated at runtime */
31
    protected $wildCardHosts = array();
32
33
    /** @var array Massive array with the mapping of host -> provider class relation. */
34
    protected $services = array(
35
        '23hq.com' => '\Embera\Providers\Hq23',
36
        'live.amcharts.com' => '\Embera\Providers\AmCharts',
37
        'animatron.com' => '\Embera\Providers\Animatron',
38
        'animoto.com' => '\Embera\Providers\Animoto',
39
        'on.aol.com' => '\Embera\Providers\AolOn',
40
        '5min.com' => '\Embera\Providers\AolOn',
41
        'alpha.app.net' => '\Embera\Providers\AppNet',
42
        'audiosnaps.com' => '\Embera\Providers\AudioSnaps',
43
        'photos.app.net' => '\Embera\Providers\AppNet',
44
        'bambuser.com' => '\Embera\Providers\Bambuser',
45
        '*.blip.tv' => '\Embera\Providers\BlipTV',
46
        'cacoo.com' => '\Embera\Providers\Cacoo',
47
        'public.chartblocks.com' => '\Embera\Providers\Chartblocks',
48
        'chirb.it' => '\Embera\Providers\Chirbit',
49
        'clipland.com' => '\Embera\Providers\Clipland',
50
        'clyp.it' => '\Embera\Providers\Clyp',
51
        'codepoints.net' => '\Embera\Providers\Codepoints',
52
        'circuitlab.com' => '\Embera\Providers\CircuitLab',
53
        'collegehumor.com' => '\Embera\Providers\CollegeHumor',
54
        'coub.com' => '\Embera\Providers\Coub',
55
        'crowdranking.com' => '\Embera\Providers\CrowdRanking',
56
        'c9ng.com' => '\Embera\Providers\CrowdRanking',
57
        'dailymile.com' => '\Embera\Providers\DailyMile',
58
        '*.dailymotion.com' => '\Embera\Providers\DailyMotion',
59
        'dai.ly' => '\Embera\Providers\DailyMotion',
60
        '*.deviantart.com' => '\Embera\Providers\Deviantart',
61
        'fav.me' => '\Embera\Providers\Deviantart',
62
        'sta.sh' => '\Embera\Providers\Deviantart',
63
        '*.didacte.com' => '\Embera\Providers\Didacte',
64
        'dipity.com' => '\Embera\Providers\Dipity',
65
        'dotsub.com' => '\Embera\Providers\DotSub',
66
        'edocr.com' => '\Embera\Providers\Edocr',
67
        'egliseinfo.catholique.fr' => '\Embera\Providers\EgliseInfo',
68
        'facebook.com' => '\Embera\Providers\Facebook',
69
        'flickr.com' => '\Embera\Providers\Flickr',
70
        'flic.kr' => '\Embera\Providers\Flickr',
71
        'funnyordie.com' => '\Embera\Providers\FunnyOrDie',
72
        'geograph.org.uk' => '\Embera\Providers\GeographUk',
73
        'geograph.co.uk' => '\Embera\Providers\GeographUk',
74
        'geograph.ie' => '\Embera\Providers\GeographUk',
75
        'geo.hlipp.de' => '\Embera\Providers\GeographDe',
76
        'geo-en.hlipp.de' => '\Embera\Providers\GeographDe',
77
        'germany.geograph.org' => '\Embera\Providers\GeographDe',
78
        'channel-islands.geographs.org' => '\Embera\Providers\GeographCI',
79
        'channel-islands.geograph.org' => '\Embera\Providers\GeographCI',
80
        'channel-islands.geographs.org.gg' => '\Embera\Providers\GeographCI',
81
        'channel-islands.geographs.org.je' => '\Embera\Providers\GeographCI',
82
        'gettyimages.com' => '\Embera\Providers\GettyImages',
83
        'gty.im' => '\Embera\Providers\GettyImages',
84
        'gfycat.com' => '\Embera\Providers\Gfycat',
85
        'gist.github.com' => '\Embera\Providers\GithubGist',
86
        'gmep.org' => '\Embera\Providers\Gmep',
87
        'huffduffer.com' => '\Embera\Providers\Huffduffer',
88
        'hulu.com' => '\Embera\Providers\Hulu',
89
        'ifixit.com' => '\Embera\Providers\IFixIt',
90
        'ifttt.com' => '\Embera\Providers\IFTTT',
91
        'infogr.am' => '\Embera\Providers\Infogram',
92
        'instagram.com' => '\Embera\Providers\Instagram',
93
        'instagr.am' => '\Embera\Providers\Instagram',
94
        'issuu.com' => '\Embera\Providers\Issuu',
95
        'kickstarter.com' => '\Embera\Providers\Kickstarter',
96
        'learningapps.org' => '\Embera\Providers\LearningApps',
97
        'meetup.com' => '\Embera\Providers\Meetup',
98
        'meetu.ps' => '\Embera\Providers\Meetup',
99
        'mixcloud.com' => '\Embera\Providers\MixCloud',
100
        'mobypicture.com' => '\Embera\Providers\MobyPicture',
101
        'moby.to' => '\Embera\Providers\MobyPicture',
102
        'nfb.ca' => '\Embera\Providers\NFB',
103
        'mix.office.com' => '\Embera\Providers\Officemix',
104
        'official.fm' => '\Embera\Providers\OfficialFM',
105
        'oumy.com' => '\Embera\Providers\Oumy',
106
        'pastery.net' => '\Embera\Providers\Pastery',
107
        '*.polldaddy.com' => '\Embera\Providers\PollDaddy',
108
        'polleverywhere.com' => '\Embera\Providers\PollEveryWhere',
109
        'portfolium.com' => '\Embera\Providers\Portfolium',
110
        'rapidengage.com' => '\Embera\Providers\Rapidengage',
111
        'rdio.com' => '\Embera\Providers\Rdio',
112
        'rd.io' => '\Embera\Providers\Rdio',
113
        'releasewire.com' => '\Embera\Providers\ReleaseWire',
114
        'rwire.com' => '\Embera\Providers\ReleaseWire',
115
        'repubhub.icopyright.net' => '\Embera\Providers\RepubHub',
116
        'reverbnation.com' => '\Embera\Providers\Reverbnation',
117
        'revision3.com' => '\Embera\Providers\Revision3',
118
        'roomshare.jp' => '\Embera\Providers\Roomshare',
119
        'rutube.ru' => '\Embera\Providers\Rutube',
120
        'videos.sapo.pt' => '\Embera\Providers\Sapo',
121
        'screenr.com' => '\Embera\Providers\Screenr',
122
        'scribd.com' => '\Embera\Providers\Scribd',
123
        'shortnote.jp' => '\Embera\Providers\ShortNote',
124
        '*.shoudio.com' => '\Embera\Providers\Shoudio',
125
        '*.shoud.io' => '\Embera\Providers\Shoudio',
126
        'showtheway.io' => '\Embera\Providers\Showtheway',
127
        '*.silk.co' => '\Embera\Providers\Silk',
128
        'sketchfab.com' => '\Embera\Providers\Sketchfab',
129
        '*.slideshare.net' => '\Embera\Providers\SlideShare',
130
        '*.soundcloud.com' => '\Embera\Providers\SoundCloud',
131
        'speakerdeck.com' => '\Embera\Providers\Speakerdeck',
132
        '*.spotify.com' => '\Embera\Providers\Spotify',
133
        'spoti.fi' => '\Embera\Providers\Spotify',
134
        'streamable.com' => '\Embera\Providers\Streamable',
135
        'content.streamonecloud.net' => '\Embera\Providers\StreamOneCloud',
136
        'sway.com' => '\Embera\Providers\Sway',
137
        'ted.com' => '\Embera\Providers\Ted',
138
        'theysaidso.com' => '\Embera\Providers\TheySaidSo',
139
        'twitter.com' => '\Embera\Providers\Twitter',
140
        'ustream.tv' => '\Embera\Providers\Ustream',
141
        'ustream.com' => '\Embera\Providers\Ustream',
142
        'verse.media' => '\Embera\Providers\Verse',
143
        'viddler.com' => '\Embera\Providers\Viddler',
144
        'videofork.com' => '\Embera\Providers\VideoFork',
145
        'videojug.com' => '\Embera\Providers\VideoJug',
146
        'vevo.com' => '\Embera\Providers\Vevo',
147
        'vimeo.com' => '\Embera\Providers\Vimeo',
148
        'wordpress.tv' => '\Embera\Providers\WordpressTV',
149
        'blog.wordpress.tv' => '\Embera\Providers\WordpressTV',
150
        'yfrog.com' => '\Embera\Providers\YFrog',
151
        'yfrog.us' => '\Embera\Providers\YFrog',
152
        'twitter.yfrog.com' => '\Embera\Providers\YFrog',
153
        'video.yandex.ru' => '\Embera\Providers\Yandex',
154
        'm.youtube.com' => '\Embera\Providers\Youtube',
155
        'youtube.com' => '\Embera\Providers\Youtube',
156
        'youtu.be' => '\Embera\Providers\Youtube',
157
        'vine.co' => '\Embera\Providers\Vine',
158
    );
159
160
    /**
161
     * Construct
162
     *
163
     * @param array $config  Associative array with configuration options
164
     * @param object $oembed Instance of \Embera\Oembed
165
     * @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...
166
     */
167 426
    public function __construct(array $config = array(), \Embera\Oembed $oembed)
168
    {
169 426
        $this->config = array_replace_recursive(array(
170 426
            'params' => array(),
171 426
            'custom_params' => array(),
172 426
            'fake' => array()
173 426
        ), $config);
174
175 426
        $this->oembed = $oembed;
176 426
    }
177
178
    /**
179
     * Finds services for the given $urls.
180
     *
181
     * @param array $urls An array with all the available urls
182
     * @return array An Array with loaded services
183
     */
184 418
    protected function findServices(array $urls = array())
185
    {
186 418
        $return = array();
187 418
        $this->extractCustomParams($this->config['custom_params']);
188 418
        foreach (array_unique($urls) as $u) {
189
190 418
            $host = $this->getHost($u);
191 418
            if (isset($this->services[$host])) {
192 220
                $provider = new \ReflectionClass($this->services[$host]);
193 220
                $return[$u] = $provider->newInstance($u, $this->config, $this->oembed);
194
195 220
                if (isset($this->customParams[$host])) {
196 2
                    $return[$u]->appendParams($this->customParams[$host]);
197 2
                }
198 220
            }
199 418
        }
200
201 418
        return $return;
202
    }
203
204
    /**
205
     * Adds a new Provider into the service map
206
     *
207
     * @param string $host The host for the map
208
     * @param string|object $class The class or object that should manage the provider
209
     * @param array $params Custom parameters that should be sent in the url for this Provider
210
     * @return void
211
     */
212
    public function addProvider($host, $class, array $params = array())
213
    {
214
        $host = preg_replace('~^(?:www)\.~i', '', strtolower($host));
215
        $this->services[$host] = $class;
216
        $this->customParams[$host] = $params;
217
    }
218
219
    /**
220
     * Gets a normalized host for the given $url
221
     *
222
     * @param string $url
223
     * @return string
224
     *
225
     * @throws InvalidArgumentException when the url seems to be invalid
226
     */
227 418
    protected function getHost($url)
228
    {
229 418
        $data = parse_url($url);
230 418
        if (empty($data['host'])) {
231
            throw new \InvalidArgumentException('The Url: ' . $url . ' seems to be invalid');
232
        }
233
234 418
        $host = preg_replace('~^(?:www|player)\.~i', '', strtolower($data['host']));
235 418
        if (isset($this->services[$host])) {
236 204
            return $host;
237 232
        } else if (isset($this->services['*.' . $host])) {
238 22
            return '*.' . $host;
239 228
        } else if (!empty($this->wildCardHosts)) {
240 228
            $trans = array('\*' => '(?:.*)');
241 228
            foreach ($this->wildCardHosts as $value) {
242 228
                $regex = strtr(preg_quote($value, '~'), $trans);
243 228
                if (preg_match('~' . $regex . '~i', $host)) {
244 20
                    return $value;
245
                }
246 226
            }
247 208
        }
248
249 208
        return $host;
250
    }
251
252
    /**
253
     * Extracts custom parameters for a Provider
254
     *
255
     * @param array $params
256
     * @return array
257
     */
258 418
    protected function extractCustomParams(array $params = array())
259
    {
260 418
        foreach ($params as $name => $values) {
261 2
            foreach ($this->services as $host => $service) {
262 2
                if (preg_match('~' . preg_quote($name, '~') . '~i', $service)) {
263 2
                    $this->customParams[$host] = (array) $values;
264 2
                }
265 2
            }
266 418
        }
267 418
    }
268
269
    /**
270
     * Returns an array with all valid services found.
271
     *
272
     * @param array|string $urls  An array with urls or a url string
273
     * @return array
274
     */
275
    public function getAll($urls)
276
    {
277 418
        $this->wildCardHosts = array_filter(array_keys($this->services), function($key) {
278 418
            return (strpos($key, '*') !== false);
279 418
        });
280
281 418
        return $this->findServices((array) $urls);
282
    }
283
}
284
285
?>
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...
286