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.

Providers::addProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

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