Skip to content

Commit f136509

Browse files
Added Facebook Video Service and more tests
1 parent bee349b commit f136509

File tree

8 files changed

+302
-2
lines changed

8 files changed

+302
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ echo $video->getEmbedCode(500,500);
128128
* Youtube
129129
* Vimeo
130130
* Dailymotion
131+
* Facebook Videos
131132

132133
# TODO List goals for release 1.0:
133134

config/config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
),
2828
'factory' => '\\RicardoFiorani\\Adapter\\Dailymotion\\Factory\\DailymotionServiceAdapterFactory',
2929
),
30+
'Facebook' => array(
31+
'patterns' => array(
32+
'~\bfacebook\.com.*?\bv=(\d+)~',
33+
'~^https?://www\.facebook\.com/video\.php\?v=(\d+)|.*?/videos/(\d+)$~m',
34+
'~^https?://www\.facebook\.com/.*?/videos/(\d+)/?$~m',
35+
36+
),
37+
'factory' => '\\RicardoFiorani\\Adapter\\Facebook\\Factory\\FacebookServiceAdapterFactory',
38+
),
3039
),
3140
'renderer' => array(
3241
'name' => 'DefaultRenderer',
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 02/09/2015
6+
* Time: 22:42
7+
*/
8+
9+
namespace RicardoFiorani\Adapter\Facebook;
10+
11+
12+
use RicardoFiorani\Adapter\AbstractServiceAdapter;
13+
use RicardoFiorani\Exception\InvalidThumbnailSizeException;
14+
use RicardoFiorani\Exception\ThumbnailSizeNotAvailable;
15+
use RicardoFiorani\Renderer\EmbedRendererInterface;
16+
17+
class FacebookServiceAdapter extends AbstractServiceAdapter
18+
{
19+
20+
const THUMBNAIL_SIZE_DEFAULT = 'default';
21+
22+
/**
23+
* AbstractVideoAdapter constructor.
24+
* @param string $url
25+
* @param string $pattern
26+
* @param EmbedRendererInterface $renderer
27+
*/
28+
public function __construct($url, $pattern, EmbedRendererInterface $renderer)
29+
{
30+
$match = array();
31+
preg_match($pattern, $url, $match);
32+
$this->setVideoId($match[1]);
33+
34+
return parent::__construct($url, $pattern, $renderer);
35+
}
36+
37+
38+
/**
39+
* Returns the service name (ie: "Youtube" or "Vimeo")
40+
* @return string
41+
*/
42+
public function getServiceName()
43+
{
44+
return 'Facebook';
45+
}
46+
47+
/**
48+
* Returns if the service has a thumbnail image
49+
* @return bool
50+
*/
51+
public function hasThumbnail()
52+
{
53+
return true;
54+
}
55+
56+
/**
57+
* Returns all thumbnails available sizes
58+
* @return array
59+
*/
60+
public function getThumbNailSizes()
61+
{
62+
return array(self::THUMBNAIL_SIZE_DEFAULT);
63+
}
64+
65+
/**
66+
* @param string $size
67+
* @return string
68+
* @throws InvalidThumbnailSizeException
69+
*/
70+
public function getThumbnail($size)
71+
{
72+
if (false == in_array($size, $this->getThumbNailSizes())) {
73+
throw new InvalidThumbnailSizeException;
74+
}
75+
76+
return 'https://graph.facebook.com/' . $this->getVideoId() . '/picture';
77+
}
78+
79+
/**
80+
* Returns the small thumbnail's url
81+
* @return string
82+
* @throws ThumbnailSizeNotAvailable
83+
*/
84+
public function getSmallThumbnail()
85+
{
86+
throw new ThumbnailSizeNotAvailable;
87+
}
88+
89+
/**
90+
* Returns the medium thumbnail's url
91+
* @return string
92+
*/
93+
public function getMediumThumbnail()
94+
{
95+
return $this->getThumbnail(self::THUMBNAIL_SIZE_DEFAULT);
96+
}
97+
98+
/**
99+
* Returns the large thumbnail's url
100+
* @return string
101+
* @throws ThumbnailSizeNotAvailable
102+
*/
103+
public function getLargeThumbnail()
104+
{
105+
throw new ThumbnailSizeNotAvailable;
106+
}
107+
108+
/**
109+
* Returns the largest thumnbnaail's url
110+
* @return string
111+
*/
112+
public function getLargestThumbnail()
113+
{
114+
return $this->getThumbnail(self::THUMBNAIL_SIZE_DEFAULT);
115+
}
116+
117+
/**
118+
* @param bool $autoplay
119+
* @return string
120+
*/
121+
public function getEmbedUrl($autoplay = false)
122+
{
123+
return 'https://www.facebook.com/video/embed?video_id=' . $this->getVideoId();
124+
}
125+
126+
/**
127+
* @return bool
128+
*/
129+
public function isEmbeddable()
130+
{
131+
return true;
132+
}
133+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 02/09/2015
6+
* Time: 22:41
7+
*/
8+
9+
namespace RicardoFiorani\Adapter\Facebook\Factory;
10+
11+
12+
use RicardoFiorani\Adapter\Facebook\FacebookServiceAdapter;
13+
use RicardoFiorani\Adapter\Factory\CallableServiceAdapterFactoryInterface;
14+
use RicardoFiorani\Adapter\VideoAdapterInterface;
15+
use RicardoFiorani\Renderer\EmbedRendererInterface;
16+
17+
class FacebookServiceAdapterFactory implements CallableServiceAdapterFactoryInterface
18+
{
19+
20+
/**
21+
* @param string $url
22+
* @param string $pattern
23+
* @param EmbedRendererInterface $renderer
24+
* @return VideoAdapterInterface
25+
*/
26+
public function __invoke($url, $pattern, EmbedRendererInterface $renderer)
27+
{
28+
return new FacebookServiceAdapter($url, $pattern, $renderer);
29+
}
30+
}

src/Detector/VideoServiceDetector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public function parse($url)
5353
}
5454
}
5555
}
56-
throw new ServiceNotAvailableException();
56+
throw new ServiceNotAvailableException(sprintf('The url "%s" could not be parsed by any of the services available.',
57+
$url));
5758
}
5859

5960
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 02/09/2015
6+
* Time: 22:55
7+
*/
8+
9+
namespace RicardoFiorani\Exception;
10+
11+
12+
use Exception;
13+
14+
class ThumbnailSizeNotAvailable extends Exception
15+
{
16+
17+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 02/09/2015
6+
* Time: 23:55
7+
*/
8+
9+
namespace RicardoFiorani\Test\Adapter;
10+
11+
12+
use PHPUnit_Framework_TestCase;
13+
use RicardoFiorani\Adapter\Dailymotion\DailymotionServiceAdapter;
14+
use RicardoFiorani\Detector\VideoServiceDetector;
15+
use RicardoFiorani\Exception\ServiceNotAvailableException;
16+
17+
class DailymotionServiceAdapterTest extends PHPUnit_Framework_TestCase
18+
{
19+
20+
/**
21+
* @dataProvider exampleUrlDataProvider
22+
* @param string $url
23+
*/
24+
public function testServiceNameIsString($url)
25+
{
26+
$dailymotionVideo = $this->getMockingObject($url);
27+
$this->assertInternalType('string', $dailymotionVideo->getServiceName());
28+
29+
}
30+
31+
/**
32+
* @dataProvider exampleUrlDataProvider
33+
* @param string $url
34+
*/
35+
public function testHasThumbnailIsBoolean($url)
36+
{
37+
$dailymotionVideo = $this->getMockingObject($url);
38+
$this->assertInternalType('bool', $dailymotionVideo->hasThumbnail());
39+
}
40+
41+
/**
42+
* @dataProvider exampleUrlDataProvider
43+
* @param string $url
44+
*/
45+
public function testGetThumbnailSizesIsArray($url)
46+
{
47+
$dailymotionVideo = $this->getMockingObject($url);
48+
$this->assertInternalType('array', $dailymotionVideo->getThumbNailSizes());
49+
}
50+
51+
/**
52+
* @dataProvider exampleUrlDataProvider
53+
* @param string $url
54+
*/
55+
public function testIfGetThumbnailIsString($url)
56+
{
57+
$dailymotionVideo = $this->getMockingObject($url);
58+
$this->assertInternalType('string',
59+
$dailymotionVideo->getThumbnail(DailymotionServiceAdapter::THUMBNAIL_DEFAULT));
60+
61+
$this->assertInternalType('string', $dailymotionVideo->getSmallThumbnail());
62+
$this->assertInternalType('string', $dailymotionVideo->getMediumThumbnail());
63+
$this->assertInternalType('string', $dailymotionVideo->getLargeThumbnail());
64+
$this->assertInternalType('string', $dailymotionVideo->getLargestThumbnail());
65+
66+
67+
}
68+
69+
/**
70+
* @dataProvider exampleUrlDataProvider
71+
* @param string $url
72+
*/
73+
public function testThrowsExceptionOnRequestThumbnailWithAnInvalidSize($url)
74+
{
75+
$dailymotionVideo = $this->getMockingObject($url);
76+
$this->setExpectedException('\\RicardoFiorani\\Exception\\InvalidThumbnailSizeException');
77+
$dailymotionVideo->getThumbnail('This Size does not exists :)');
78+
}
79+
80+
/**
81+
* @return array
82+
*/
83+
public function exampleUrlDataProvider()
84+
{
85+
return array(
86+
array(
87+
'http://www.dailymotion.com/video/x332a71_que-categoria-jogador-lucas-lima-faz-golaco-em-treino-do-santos_sport'
88+
),
89+
);
90+
}
91+
92+
/**
93+
* @param $url
94+
* @return DailymotionServiceAdapter
95+
* @throws ServiceNotAvailableException
96+
*/
97+
public function getMockingObject($url)
98+
{
99+
$videoParser = new VideoServiceDetector();
100+
$dailymotionVideo = $videoParser->parse($url);
101+
102+
return $dailymotionVideo;
103+
}
104+
105+
}

test/Detector/ServiceDetectorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Time: 21:54
77
*/
88

9-
namespace RicardoFiorani\Test;
9+
namespace RicardoFiorani\Test\Detector;
1010

1111
use PHPUnit_Framework_TestCase;
1212
use RicardoFiorani\Container\Factory\ServicesContainerFactory;
@@ -55,6 +55,10 @@ public function videoUrlProvider()
5555
'http://www.dailymotion.com/video/x332a71_que-categoria-jogador-lucas-lima-faz-golaco-em-treino-do-santos_sport',
5656
'\\RicardoFiorani\\Adapter\\Dailymotion\\DailymotionServiceAdapter',
5757
),
58+
'Commom Facebook Video URL' => array(
59+
'https://www.facebook.com/RantPets/videos/583336855137988/',
60+
'\\RicardoFiorani\\Adapter\\Facebook\\FacebookServiceAdapter',
61+
)
5862
);
5963
}
6064

0 commit comments

Comments
 (0)