Skip to content

Commit 97281c6

Browse files
committed
removed duplicated code
1 parent 90a3744 commit 97281c6

File tree

8 files changed

+89
-121
lines changed

8 files changed

+89
-121
lines changed

src/Adapters/Adapter.php

Lines changed: 82 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Embed\Http\Response;
77
use Embed\Http\ImageResponse;
88
use Embed\Http\DispatcherInterface;
9+
use Embed\Providers\ProviderInterface;
910
use Embed\Bag;
1011

1112
/**
@@ -91,30 +92,21 @@ public function getProviders()
9192
*/
9293
public function getTitle()
9394
{
94-
foreach ($this->providers as $provider) {
95-
$title = $provider->getTitle();
96-
97-
if ($title !== null) {
98-
return $title;
99-
}
100-
}
95+
$default = $this->url;
10196

102-
//If there's no title, returns the url instead
103-
return $this->url;
97+
return $this->getFirstFromProviders(function ($provider) {
98+
return $provider->getTitle();
99+
}, $default);
104100
}
105101

106102
/**
107103
* {@inheritdoc}
108104
*/
109105
public function getDescription()
110106
{
111-
foreach ($this->providers as $provider) {
112-
$description = $provider->getDescription();
113-
114-
if ($description !== null) {
115-
return $description;
116-
}
117-
}
107+
return $this->getFirstFromProviders(function ($provider) {
108+
return $provider->getDescription();
109+
});
118110
}
119111

120112
/**
@@ -150,43 +142,27 @@ public function getType()
150142
//If it has code, it's not a link
151143
unset($types['link']);
152144

153-
return self::getBigger($types) ?: 'rich';
145+
return static::getBigger($types) ?: 'rich';
154146
}
155147

156148
/**
157149
* {@inheritdoc}
158150
*/
159151
public function getTags()
160152
{
161-
$tags = [];
162-
163-
foreach ($this->providers as $provider) {
164-
foreach ($provider->getTags() as $tag) {
165-
if (!in_array($tag, $tags)) {
166-
$tags[] = $tag;
167-
}
168-
}
169-
}
170-
171-
return $tags;
153+
return $this->getAllFromProviders(function ($provider) {
154+
return $provider->getTags();
155+
});
172156
}
173157

174158
/**
175159
* {@inheritdoc}
176160
*/
177161
public function getFeeds()
178162
{
179-
$feeds = [];
180-
181-
foreach ($this->providers as $provider) {
182-
foreach ($provider->getFeeds() as $feed) {
183-
if (!in_array($feed, $feeds)) {
184-
$feeds[] = $feed;
185-
}
186-
}
187-
}
188-
189-
return $feeds;
163+
return $this->getAllFromProviders(function ($provider) {
164+
return $provider->getFeeds();
165+
});
190166
}
191167

192168
/**
@@ -239,43 +215,31 @@ public function getCode()
239215
*/
240216
public function getUrl()
241217
{
242-
foreach ($this->providers as $provider) {
243-
$url = $provider->getUrl();
244-
245-
if ($url !== null) {
246-
return $url;
247-
}
248-
}
218+
$default = (string) $this->getResponse()->getUri();
249219

250-
return (string) $this->getResponse()->getUri();
220+
return $this->getFirstFromProviders(function ($provider) {
221+
return $provider->getUrl();
222+
}, $default);
251223
}
252224

253225
/**
254226
* {@inheritdoc}
255227
*/
256228
public function getAuthorName()
257229
{
258-
foreach ($this->providers as $provider) {
259-
$authorName = $provider->getAuthorName();
260-
261-
if ($authorName !== null) {
262-
return $authorName;
263-
}
264-
}
230+
return $this->getFirstFromProviders(function ($provider) {
231+
return $provider->getAuthorName();
232+
});
265233
}
266234

267235
/**
268236
* {@inheritdoc}
269237
*/
270238
public function getAuthorUrl()
271239
{
272-
foreach ($this->providers as $provider) {
273-
$authorUrl = $provider->getAuthorUrl();
274-
275-
if ($authorUrl !== null) {
276-
return $authorUrl;
277-
}
278-
}
240+
return $this->getFirstFromProviders(function ($provider) {
241+
return $provider->getAuthorUrl();
242+
});
279243
}
280244

281245
/**
@@ -324,39 +288,32 @@ public function getProviderIcon()
324288
$sizes[$icon['url']] = $icon['size'];
325289
}
326290

327-
return self::getBigger($sizes);
291+
return static::getBigger($sizes);
328292
}
329293

330294
/**
331295
* {@inheritdoc}
332296
*/
333297
public function getProviderName()
334298
{
335-
foreach ($this->providers as $provider) {
336-
$providerName = $provider->getProviderName();
299+
$default = $this->getResponse()->getUri()->getDomain();
337300

338-
if (!empty($providerName)) {
339-
return $providerName;
340-
}
341-
}
342-
343-
return $this->getResponse()->getUri()->getDomain();
301+
return $this->getFirstFromProviders(function ($provider) {
302+
return $provider->getProviderName();
303+
}, $default);
344304
}
345305

346306
/**
347307
* {@inheritdoc}
348308
*/
349309
public function getProviderUrl()
350310
{
351-
foreach ($this->providers as $provider) {
352-
if (($url = $provider->getProviderUrl()) !== null) {
353-
return $url;
354-
}
355-
}
356-
357311
$uri = $this->getResponse()->getUri();
312+
$default = $uri->getScheme().'://'.$uri->getDomain(true);
358313

359-
return $uri->getScheme().'://'.$uri->getDomain(true);
314+
return $this->getFirstFromProviders(function ($provider) {
315+
return $provider->getProviderUrl();
316+
}, $default);
360317
}
361318

362319
/**
@@ -423,7 +380,7 @@ public function getImage()
423380
$sizes[$image['url']] = $image['size'];
424381
}
425382

426-
$image = self::getBigger($sizes);
383+
$image = static::getBigger($sizes);
427384
} else {
428385
reset($images);
429386
$image = current($images);
@@ -492,43 +449,29 @@ public function getAspectRatio()
492449
*/
493450
public function getPublishedTime()
494451
{
495-
foreach ($this->providers as $provider) {
496-
$publishedTime = $provider->getPublishedTime();
497-
498-
if ($publishedTime !== null) {
499-
return $publishedTime;
500-
}
501-
}
452+
return $this->getFirstFromProviders(function ($provider) {
453+
return $provider->getPublishedTime();
454+
});
502455
}
503456

504457
/**
505458
* {@inheritdoc}
506459
*/
507460
public function getLicense()
508461
{
509-
foreach ($this->providers as $provider) {
510-
$license = $provider->getLicense();
511-
512-
if ($license !== null) {
513-
return $license;
514-
}
515-
}
462+
return $this->getFirstFromProviders(function ($provider) {
463+
return $provider->getLicense();
464+
});
516465
}
517466

518467
/**
519468
* {@inheritdoc}
520469
*/
521470
public function getLinkedData()
522471
{
523-
$data = [];
524-
525-
foreach ($this->providers as $provider) {
526-
foreach ($provider->getLinkedData() as $value) {
527-
$data[] = $value;
528-
}
529-
}
530-
531-
return $data;
472+
return $this->getAllFromProviders(function ($provider) {
473+
return $provider->getLinkedData();
474+
});
532475
}
533476

534477
/**
@@ -571,7 +514,7 @@ function (ImageResponse $response) {
571514
*
572515
* @return string|null
573516
*/
574-
private static function getBigger(array $values)
517+
protected static function getBigger(array $values)
575518
{
576519
$bigger = null;
577520

@@ -583,4 +526,41 @@ private static function getBigger(array $values)
583526

584527
return $bigger;
585528
}
529+
530+
/**
531+
* Returns the first value of the providers
532+
*
533+
* @param callable $callable
534+
*
535+
* @return string|null
536+
*/
537+
protected function getFirstFromProviders(callable $callable, $default = null)
538+
{
539+
$values = array_filter(array_map($callable, $this->providers));
540+
541+
return empty($values) ? $default : current($values);
542+
}
543+
544+
/**
545+
* Returns the all values from the providers
546+
*
547+
* @param callable $callable
548+
*
549+
* @return string|null
550+
*/
551+
protected function getAllFromProviders(callable $callable)
552+
{
553+
$values = array_filter(array_map($callable, $this->providers));
554+
$all = [];
555+
556+
foreach ($values as $value) {
557+
foreach ($value as $v) {
558+
if (!in_array($v, $all, true)) {
559+
$all[] = $v;
560+
}
561+
}
562+
}
563+
564+
return $all;
565+
}
586566
}

src/Providers/Api/GoogleMaps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Embed\Utils;
99

1010
/**
11-
* Provider to use the API of google.
11+
* Provider to use the API of google.com
1212
*/
1313
class GoogleMaps extends Provider implements ProviderInterface
1414
{

src/Providers/Dcterms.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use Embed\Adapters\AdapterInterface;
66

77
/**
8-
* Generic Dublin Core provider.
9-
*
10-
* Load the Dublin Core data of an url and store it
8+
* Provider to get the data from the Dublin Core data elements in the HTML
119
*/
1210
class Dcterms extends Provider implements ProviderInterface
1311
{

src/Providers/Html.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
use Exception;
1010

1111
/**
12-
* Generic html provider.
13-
*
14-
* Load the html data of an url and store it
12+
* Provider to get the data from the HTML code
1513
*/
1614
class Html extends Provider implements ProviderInterface
1715
{

src/Providers/OEmbed.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use Embed\Http\Uri;
88

99
/**
10-
* Generic oembed provider.
11-
*
12-
* Load the oembed data of an url and store it
10+
* Provider to get the data using the oEmbed API
1311
*/
1412
class OEmbed extends Provider implements ProviderInterface
1513
{

src/Providers/OpenGraph.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use Embed\Utils;
77

88
/**
9-
* Generic opengraph provider.
10-
*
11-
* Load the opengraph data of an url and store it
9+
* Provider to get the data from the Open Graph elements in the HTML
1210
*/
1311
class OpenGraph extends Provider implements ProviderInterface
1412
{

src/Providers/Sailthru.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use Embed\Adapters\AdapterInterface;
66

77
/**
8-
* Generic Salithru provider.
9-
*
10-
* Load the Salithru data of an url and store it
8+
* Provider to get the data from the Sailthru data elements in the HTML
119
*/
1210
class Sailthru extends Provider implements ProviderInterface
1311
{

src/Providers/TwitterCards.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use Embed\Utils;
77

88
/**
9-
* Generic twitter cards provider.
10-
*
11-
* Load the twitter cards data of an url and store it
9+
* Provider to get the data from the Twitter Cards elements in the HTML
1210
*/
1311
class TwitterCards extends Provider implements ProviderInterface
1412
{

0 commit comments

Comments
 (0)