Skip to content

Commit 903e26b

Browse files
added Vimeo Service and divided Video Service Detector into a ServicesContainer to keep SRP
1 parent 35f2c26 commit 903e26b

File tree

7 files changed

+387
-107
lines changed

7 files changed

+387
-107
lines changed

config/config.php

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

src/Adapter/Vimeo/VimeoServiceAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct($url, $pattern, EmbedRendererInterface $renderer)
3939
$this->setVideoId($videoId);
4040

4141
/*Sends the video ID to the API to get the thumbnails and other infos*/
42-
$hash = unserialize(@file_get_contents("http://vimeo.com/api/v2/video/$videoId.php"));
42+
$hash = unserialize(@file_get_contents("http://vimeo.com/api/v2/video/" . $this->getVideoId() . ".php"));
4343
$data = $hash[0];
4444

4545

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

166166
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 31/08/2015
6+
* Time: 21:07
7+
*/
8+
9+
namespace RicardoFiorani\Container\Factory;
10+
11+
12+
use RicardoFiorani\Container\ServicesContainer;
13+
14+
class ServicesContainerFactory
15+
{
16+
17+
public function __invoke()
18+
{
19+
$configFile = require __DIR__ . '/../../../config/config.php';
20+
$servicesContainer = new ServicesContainer($configFile);
21+
22+
return $servicesContainer;
23+
}
24+
25+
public static function createNewServiceDetector()
26+
{
27+
$factory = new self();
28+
29+
return $factory();
30+
}
31+
}

src/Container/ServicesContainer.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 31/08/2015
6+
* Time: 21:06
7+
*/
8+
9+
namespace RicardoFiorani\Container;
10+
11+
12+
use RicardoFiorani\Adapter\Factory\CallableServiceAdapterFactoryInterface;
13+
use RicardoFiorani\Exception\DuplicatedServiceNameException;
14+
use RicardoFiorani\Renderer\EmbedRendererInterface;
15+
16+
class ServicesContainer
17+
{
18+
/**
19+
* @var array
20+
*/
21+
private $services = array();
22+
23+
/**
24+
* @var array
25+
*/
26+
private $patterns = array();
27+
28+
/**
29+
* @var array
30+
*/
31+
private $factories = array();
32+
33+
/**
34+
* @var array
35+
*/
36+
private $instantiatedFactories = array();
37+
38+
/**
39+
* @var EmbedRendererInterface
40+
*/
41+
private $renderer;
42+
43+
/**
44+
* @var string
45+
*/
46+
private $rendererName;
47+
48+
/**
49+
* ServicesContainer constructor.
50+
* @param array $config
51+
*/
52+
public function __construct(array $config = array())
53+
{
54+
if (false == empty($config)) {
55+
$this->registerFromConfig($config);
56+
}
57+
}
58+
59+
/**
60+
* Loads de default config file
61+
* @param array $config
62+
* @throws DuplicatedServiceNameException
63+
*/
64+
private function registerFromConfig(array $config)
65+
{
66+
foreach ($config['services'] as $serviceName => $serviceConfig) {
67+
$this->registerService($serviceName, $serviceConfig['patterns'], $serviceConfig['factory']);
68+
}
69+
$this->setRenderer($config['renderer']['name'], $config['renderer']['factory']);
70+
}
71+
72+
/**
73+
* Register a Service
74+
* @param string $serviceName
75+
* @param array $regex
76+
* @param string|callable $factory
77+
* @throws DuplicatedServiceNameException
78+
*/
79+
public function registerService($serviceName, array $regex, $factory)
80+
{
81+
if ($this->hasService($serviceName)) {
82+
throw new DuplicatedServiceNameException();
83+
}
84+
85+
$this->services[] = $serviceName;
86+
$this->patterns[$serviceName] = $regex;
87+
$this->factories[$serviceName] = $factory;
88+
}
89+
90+
91+
/**
92+
* @param string $rendererName
93+
* @param string $rendererFactory
94+
*/
95+
public function setRenderer($rendererName, $rendererFactory)
96+
{
97+
$this->rendererName = $rendererName;
98+
$factory = new $rendererFactory();
99+
$this->renderer = $factory();
100+
}
101+
102+
/**
103+
* @return EmbedRendererInterface
104+
*/
105+
public function getRenderer()
106+
{
107+
return $this->renderer;
108+
}
109+
110+
111+
/**
112+
* @return array
113+
*/
114+
public function getServiceNameList()
115+
{
116+
return $this->services;
117+
}
118+
119+
120+
/**
121+
* @param string $serviceName
122+
* @return bool
123+
*/
124+
public function hasService($serviceName)
125+
{
126+
return in_array($serviceName, $this->services);
127+
}
128+
129+
/**
130+
* @return array
131+
*/
132+
public function getServices()
133+
{
134+
return $this->services;
135+
}
136+
137+
/**
138+
* @return array
139+
*/
140+
public function getPatterns()
141+
{
142+
return $this->patterns;
143+
}
144+
145+
/**
146+
* @param string $service
147+
* @return CallableServiceAdapterFactoryInterface
148+
*/
149+
public function getFactory($service)
150+
{
151+
$factory = new $this->factories[$service]();
152+
if (isset($this->instantiatedFactories[$service])) {
153+
return $this->instantiatedFactories[$service];
154+
}
155+
156+
return $this->instantiatedFactories[$service] = $factory;
157+
}
158+
159+
160+
}

0 commit comments

Comments
 (0)