Skip to content

Commit 6d64b26

Browse files
minor fixes and tests
1 parent 8d67ca9 commit 6d64b26

File tree

7 files changed

+87
-16
lines changed

7 files changed

+87
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ require __DIR__ . '/vendor/autoload.php';
109109
$vsd = new VideoServiceDetector();
110110

111111
//This is where the magic is done
112-
$vsd->setRenderer('MyOwnRenderer', 'MyVendor\\MyRenderer\\Factory\\MyOwnRendererFactory');
112+
$vsd->getServiceContainer()->setRenderer('MyOwnRenderer', 'MyVendor\\MyRenderer\\Factory\\MyOwnRendererFactory');
113113

114114
$video = $vsd->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw');
115115

@@ -121,9 +121,9 @@ echo $video->getEmbedCode(500,500);
121121
### Currently Suported Services
122122
* Youtube
123123
* Vimeo
124+
* Dailymotion
124125

125126
# TODO List goals for release 1.0:
126127

127128
* Fix the Exceptions Messages
128129
* Create PHPUnit Tests
129-
* Add more Services

example/RegisteringANewService.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ $patterns = array(
174174
);
175175

176176
//Register the new service
177-
$vsd->registerService($serviceName, $patterns, "\\MyVendor\\ServiceAdapter\\Factory\\DailyMotionServiceAdapterFactory");
177+
$vsd->getServiceContainer()->registerService($serviceName, $patterns, "\\MyVendor\\ServiceAdapter\\Factory\\DailyMotionServiceAdapterFactory");
178178

179179
//This will get you an DailyMotionServiceAdapter
180180
$video = $vsd->parse('http://www.dailymotion.com/video/x33ncwc_kittens-fight-in-tiny-boxing-ring_animals');

src/Adapter/Vimeo/VimeoServiceAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function getThumbnail($size)
160160
*/
161161
public function getEmbedUrl($autoplay = false)
162162
{
163-
return "http://player.vimeo.com/video/" . $this->getVideoId() . "?byline=0&portrait=0&amp" . ($autoplay ? '&amp&autoplay=1' : '');
163+
return "http://player.vimeo.com/video/" . $this->getVideoId() . ($autoplay ? '?autoplay=1' : '');
164164
}
165165

166166
/**

src/Adapter/Youtube/YoutubeServiceAdapter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class YoutubeServiceAdapter extends AbstractServiceAdapter
3131
public function __construct($url, $pattern, EmbedRendererInterface $renderer)
3232
{
3333
preg_match($pattern, $url, $match);
34-
$videoId = $match[2];
34+
if (isset($match[2])) {
35+
$videoId = $match[2];
36+
}
3537
if (empty($videoId)) {
3638
$videoId = $match[1];
3739
}

src/Detector/VideoServiceDetector.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class VideoServiceDetector
1717
*/
1818
private $serviceContainer;
1919

20+
/**
21+
* @var array
22+
*/
23+
private $parsedUrls = array();
24+
2025
/**
2126
* VideoServiceDetector constructor.
2227
*/
@@ -32,6 +37,9 @@ public function __construct()
3237
*/
3338
public function parse($url)
3439
{
40+
if (isset($this->parsedUrls[$url])) {
41+
return $this->parsedUrls[$url];
42+
}
3543
/** @var array $patterns */
3644
/** @var string $serviceName */
3745
foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) {
@@ -40,7 +48,8 @@ public function parse($url)
4048
if (false != preg_match($pattern, $url)) {
4149
$factory = $this->getServiceContainer()->getFactory($serviceName);
4250

43-
return $factory($url, $pattern, $this->getServiceContainer()->getRenderer());
51+
return $this->parsedUrls[$url] = $factory($url, $pattern,
52+
$this->getServiceContainer()->getRenderer());
4453
}
4554
}
4655
}

src/Exception/DuplicatedServiceNameException.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,4 @@
1414
class DuplicatedServiceNameException extends Exception
1515
{
1616

17-
/**
18-
* DuplicatedServiceNameException constructor.
19-
* @param string $message
20-
* @param int $code
21-
* @param Exception $previous
22-
*/
23-
public function __construct($code = 0, Exception $previous = null)
24-
{
25-
return parent::__construct("", $code, $previous);
26-
}
2717
}

test/Detector/ServiceDetectorTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 31/08/2015
6+
* Time: 21:54
7+
*/
8+
9+
namespace RicardoFiorani\Test;
10+
11+
use PHPUnit_Framework_TestCase;
12+
use RicardoFiorani\Detector\VideoServiceDetector;
13+
use RicardoFiorani\Exception\ServiceNotAvailableException;
14+
15+
class ServiceDetectorTest extends PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @dataProvider youtubeDataProvider
19+
* @param $url
20+
* @param $expectedEmbedCode
21+
* @throws ServiceNotAvailableException
22+
*/
23+
public function testCanParseYoutubeUrl($url, $expectedEmbedCode)
24+
{
25+
$detector = new VideoServiceDetector();
26+
$video = $detector->parse($url);
27+
$this->assertEquals($video->getEmbedUrl(), $expectedEmbedCode);
28+
}
29+
30+
public function youtubeDataProvider()
31+
{
32+
33+
return array(
34+
'Normal Youtube URL' => array(
35+
'https://www.youtube.com/watch?v=mWRsgZuwf_8',
36+
'http://www.youtube.com/embed/mWRsgZuwf_8',
37+
),
38+
'Short Youtube URL' => array(
39+
'https://youtu.be/JMLBOKVfHaA',
40+
'http://www.youtube.com/embed/JMLBOKVfHaA',
41+
),
42+
'Embed Youtube URL' => array(
43+
'<iframe width="420" height="315" src="https://www.youtube.com/embed/vwp9JkaESdg" frameborder="0" allowfullscreen></iframe>',
44+
'http://www.youtube.com/embed/vwp9JkaESdg',
45+
),
46+
);
47+
}
48+
49+
/**
50+
* @dataProvider vimeoDataProvider
51+
* @param string $url
52+
* @param string $expectedEmbedCode
53+
*/
54+
public function testCanParseVimeoUrl($url, $expectedEmbedCode)
55+
{
56+
$detector = new VideoServiceDetector();
57+
$video = $detector->parse($url);
58+
$this->assertEquals($video->getEmbedUrl(), $expectedEmbedCode);
59+
}
60+
61+
public function vimeoDataProvider()
62+
{
63+
return array(
64+
'Common Vimeo URL' => array(
65+
'https://vimeo.com/137781541',
66+
'http://player.vimeo.com/video/137781541'
67+
)
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)