Bläddra i källkod

Refactorisation de la facrory youtube

Sevajol Bastien 11 år sedan
förälder
incheckning
cf13273036

+ 5 - 0
src/Muzich/CoreBundle/Factory/ElementFactory.php Visa fil

@@ -38,6 +38,11 @@ abstract class ElementFactory
38 38
     $this->url_analyzer = new UrlAnalyzer($element, $this->url_matchs);
39 39
   }
40 40
   
41
+  protected function setUrlAnalyzer($url_matchs)
42
+  {
43
+    $this->url_analyzer = new UrlAnalyzer($this->element, $url_matchs);
44
+  }
45
+  
41 46
   protected function getApiConnector()
42 47
   {
43 48
     return $this->api_connector;

+ 6 - 21
src/Muzich/CoreBundle/Factory/Elements/Youtube.php Visa fil

@@ -4,32 +4,17 @@ namespace Muzich\CoreBundle\Factory\Elements;
4 4
 
5 5
 use Muzich\CoreBundle\Factory\Elements\Youtubecom;
6 6
 use Muzich\CoreBundle\Entity\Element;
7
+use Muzich\CoreBundle\Factory\UrlMatchs;
8
+use Symfony\Component\DependencyInjection\Container;
9
+use Doctrine\ORM\EntityManager;
7 10
 
8
-/**
9
- * 
10
- *
11
- * @author bux
12
- */
13 11
 class Youtube extends Youtubecom
14 12
 {
15 13
   
16
-  public function retrieveDatas()
14
+  public function __construct(Element $element, Container $container, EntityManager $entity_manager)
17 15
   {
18
-    $url_clean = $this->getCleanedUrl();
19
-    $ref_id = null;
20
-    
21
-    if (preg_match("#\/([a-zA-Z0-9_]+)#", $url_clean, $chaines))
22
-    {
23
-      $ref_id = $chaines[1];
24
-    }
25
-    
26
-    $this->element->setData(Element::DATA_REF_ID, $ref_id);
27
-    
28
-    // Données API
29
-    if ($ref_id)
30
-    {
31
-      $this->proceedAPIDatas($ref_id);
32
-    }
16
+    parent::__construct($element, $container, $entity_manager);
17
+    $this->setUrlAnalyzer(UrlMatchs::$youtu);
33 18
   }
34 19
   
35 20
 }

+ 66 - 45
src/Muzich/CoreBundle/Factory/Elements/Youtubecom.php Visa fil

@@ -4,61 +4,82 @@ namespace Muzich\CoreBundle\Factory\Elements;
4 4
 
5 5
 use Muzich\CoreBundle\Factory\ElementFactory;
6 6
 use Muzich\CoreBundle\Entity\Element;
7
+use Muzich\CoreBundle\Factory\UrlMatchs;
8
+use Symfony\Component\DependencyInjection\Container;
9
+use Doctrine\ORM\EntityManager;
7 10
 
8
-/**
9
- * 
10
- *
11
- * @author bux
12
- */
13 11
 class Youtubecom extends ElementFactory
14 12
 {
15 13
   
16
-  protected function proceedAPIDatas($ref_id)
14
+  public function __construct(Element $element, Container $container, EntityManager $entity_manager)
17 15
   {
18
-    $video_data_dom = new \DOMDocument;
19
-    try {
20
-      $video_data_dom->load("http://gdata.youtube.com/feeds/api/videos/". $ref_id);
21
-
22
-      if ($video_data_dom->getElementsByTagName("title"))
23
-      {
24
-        if ($video_data_dom->getElementsByTagName("title")->item(0))
25
-        {
26
-          if (($title = $video_data_dom->getElementsByTagName("title")->item(0)->nodeValue))
27
-          {
28
-            $this->element->setData(Element::DATA_TITLE, $title);
29
-          }
30
-        }
31
-      }
32
-    }
33
-    catch (\ErrorException $e)
34
-    {
35
-      // Api injoignable
36
-    }
16
+    $this->url_matchs = UrlMatchs::$youtube;
17
+    parent::__construct($element, $container, $entity_manager);
37 18
   }
38 19
   
39
-  public function retrieveDatas()
20
+  public function proceedDatas()
40 21
   {
41
-    $url_clean = $this->getCleanedUrl();
42
-    $ref_id = null;
43
-    
44
-    if (preg_match("#(v\/|watch\?v=)([\w\-]+)#", $url_clean, $chaines))
45
-    {
46
-      $ref_id = $chaines[2];
47
-    }
48
-    else if (preg_match("#(v=|watch\?v=)([\w\-]+)#", $url_clean, $chaines))
49
-    {
50
-      $ref_id = $chaines[2];
51
-    }
52
-    
53
-    $this->element->setData(Element::DATA_REF_ID, $ref_id);
22
+    $this->setElementDatasWithApi();
23
+    $this->proceedEmbedCode();
24
+    $this->proceedThumbnailUrl();
25
+  }
26
+  
27
+  protected function setElementDatasWithApi()
28
+  {
29
+    $response = $this->getApiConnector()->getResponseForUrl('https://www.googleapis.com/youtube/v3/videos?id='
30
+      .$this->url_analyzer->getRefId().'&key=AIzaSyC1DjeoUerFTUDdf2Rw8L8znEWbueGeykg&part=snippet,contentDetails,statistics,status');
54 31
     
55
-    // Données API TODO: REFACTORISER
56
-    if ($ref_id)
57
-    {
58
-      $this->proceedAPIDatas($ref_id);
59
-    }
32
+    $this->getApiConnector()->setElementDatasWithResponse($response, array(
33
+      Element::DATA_THUMB_URL  => array('items' => array(0 => array('thumbnails' => 'medium'))),
34
+      Element::DATA_TITLE      => array('items' => array(0 => array('snippet' => 'title'))),
35
+    ));
36
+    //
37
+    //die(var_dump($response->getContent()));
38
+    //
39
+    //$video_data_dom = new \DOMDocument;
40
+    //try {
41
+    //  $video_data_dom->load("http://gdata.youtube.com/feeds/api/videos/". $ref_id);
42
+    //
43
+    //  if ($video_data_dom->getElementsByTagName("title"))
44
+    //  {
45
+    //    if ($video_data_dom->getElementsByTagName("title")->item(0))
46
+    //    {
47
+    //      if (($title = $video_data_dom->getElementsByTagName("title")->item(0)->nodeValue))
48
+    //      {
49
+    //        $this->element->setData(Element::DATA_TITLE, $title);
50
+    //      }
51
+    //    }
52
+    //  }
53
+    //}
54
+    //catch (\ErrorException $e)
55
+    //{
56
+    //  // Api injoignable
57
+    //}
60 58
   }
61 59
   
60
+  //public function retrieveDatas()
61
+  //{
62
+  //  $url_clean = $this->getCleanedUrl();
63
+  //  $ref_id = null;
64
+  //  
65
+  //  if (preg_match("#(v\/|watch\?v=)([\w\-]+)#", $url_clean, $chaines))
66
+  //  {
67
+  //    $ref_id = $chaines[2];
68
+  //  }
69
+  //  else if (preg_match("#(v=|watch\?v=)([\w\-]+)#", $url_clean, $chaines))
70
+  //  {
71
+  //    $ref_id = $chaines[2];
72
+  //  }
73
+  //  
74
+  //  $this->element->setData(Element::DATA_REF_ID, $ref_id);
75
+  //  
76
+  //  // Données API TODO: REFACTORISER
77
+  //  if ($ref_id)
78
+  //  {
79
+  //    $this->proceedAPIDatas($ref_id);
80
+  //  }
81
+  //}
82
+  
62 83
   public function proceedEmbedCode()
63 84
   {
64 85
     if (($ref_id = $this->element->getData(Element::DATA_REF_ID)))
@@ -78,7 +99,7 @@ class Youtubecom extends ElementFactory
78 99
     if (($ref_id = $this->element->getData(Element::DATA_REF_ID)))
79 100
     {
80 101
       $this->element->setThumbnailUrl(
81
-        'http://img.youtube.com/vi/'.$ref_id.'/default.jpg'        
102
+        'http://img.youtube.com/vi/'.$ref_id.'/default.jpg'
82 103
       );
83 104
     }
84 105
   }

+ 15 - 0
src/Muzich/CoreBundle/Factory/UrlMatchs.php Visa fil

@@ -83,4 +83,19 @@ class UrlMatchs
83 83
       "#\/([0-9]+)#" => 1
84 84
     )
85 85
   );
86
+  
87
+  public static $youtu = array(
88
+    Element::TYPE_OTHER => array(
89
+      // http://youtu.be/2-5xt9MrI9w
90
+      "#\/([a-zA-Z0-9_]+)#" => 1
91
+    )
92
+  );
93
+  
94
+  public static $youtube = array(
95
+    Element::TYPE_OTHER => array(
96
+      // https://www.youtube.com/watch?v=2-5xt9MrI9w
97
+      "#(v\/|watch\?v=)([\w\-]+)#" => 2,
98
+      "#(v=|watch\?v=)([\w\-]+)#" => 2
99
+    )
100
+  );
86 101
 }

Filskillnaden har hållits tillbaka eftersom den är för stor
+ 488 - 580
src/Muzich/CoreBundle/Tests/ElementFactory/ElementFactoryTest.php