Kaynağa Gözat

Refactorisation de la factory de spotify

Sevajol Bastien 11 yıl önce
ebeveyn
işleme
68c8a4a6d6

+ 102 - 49
src/Muzich/CoreBundle/Factory/Elements/Spotifycom.php Dosyayı Görüntüle

@@ -4,6 +4,9 @@ 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 11
 /**
9 12
  * 
@@ -13,6 +16,18 @@ use Muzich\CoreBundle\Entity\Element;
13 16
 class Spotifycom extends ElementFactory
14 17
 {
15 18
   
19
+  public function __construct(Element $element, Container $container, EntityManager $entity_manager)
20
+  {
21
+    $this->url_matchs = UrlMatchs::$spotify;
22
+    parent::__construct($element, $container, $entity_manager);
23
+  }
24
+  
25
+  public function proceedDatas()
26
+  {
27
+    $this->setElementDatasWithApi();
28
+    $this->proceedEmbedCode();
29
+  }
30
+  
16 31
   /**
17 32
    * 
18 33
     track http url: http://open.spotify.com/track/7ylMdCOkqumPAwIMb6j2D5
@@ -59,67 +74,105 @@ class Spotifycom extends ElementFactory
59 74
     }
60 75
   }
61 76
   
62
-  protected function getDataFromApi($ref_id)
77
+  protected function setElementDatasWithApi()
63 78
   {
64
-    if ($this->element->getData(Element::DATA_TYPE) == 'track')
79
+    if ($this->url_analyzer->getType() == Element::TYPE_TRACK)
65 80
     {
66
-      $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:track:'.$ref_id);
81
+      $response = $this->getApiConnector()->getResponseForUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:track:'
82
+        .$this->url_analyzer->getRefId()
83
+      );
84
+      $this->getApiConnector()->setElementDatasWithResponse($response, array(
85
+        Element::DATA_ARTIST   => array('track' => 'artist'),
86
+        Element::DATA_TITLE    => array('track' => 'name')
87
+      ));
67 88
       
68
-      if (array_key_exists('track', $data))
89
+      if (!$this->element->getData(Element::DATA_ARTIST))
69 90
       {
70
-        if (array_key_exists('available', $data['track']))
71
-        {
72
-          if ($data['track']['available'])
73
-          {
74
-            if (array_key_exists('artist', $data['track']))
75
-            {
76
-              $this->element->setData(Element::DATA_ARTIST, $data['track']['artist']);
77
-            }
78
-            if (array_key_exists('artists', $data['track']))
79
-            {
80
-              if (count($data['track']['artists']))
81
-              {
82
-                if (array_key_exists('name', $data['track']['artists'][0]))
83
-                {
84
-                  $this->element->setData(Element::DATA_ARTIST, $data['track']['artists'][0]['name']);
85
-                }
86
-              }
87
-            }
88
-            if (array_key_exists('name', $data['track']))
89
-            {
90
-              $this->element->setData(Element::DATA_TITLE, $data['track']['name']);
91
-            }
92
-          }
93
-        }
91
+        $this->getApiConnector()->setElementDatasWithResponse($response, array(
92
+          Element::DATA_ARTIST         => array('track' => array('artists' => array(0 => 'name'))),
93
+        ));
94 94
       }
95 95
     }
96
-    if ($this->element->getData(Element::DATA_TYPE) == 'album')
96
+    if ($this->url_analyzer->getType() == Element::TYPE_ALBUM)
97 97
     {
98
-      $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:album:'.$ref_id);
98
+      $response = $this->getApiConnector()->getResponseForUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:album:'
99
+        .$this->url_analyzer->getRefId()
100
+      );
101
+      $this->getApiConnector()->setElementDatasWithResponse($response, array(
102
+        Element::DATA_ARTIST   => array('album' => 'artist'),
103
+        Element::DATA_TITLE    => array('album' => 'name')
104
+      ));
99 105
       
100
-      if (array_key_exists('album', $data))
106
+      if (!$this->element->getData(Element::DATA_ARTIST))
101 107
       {
102
-        if (array_key_exists('artist', $data['album']))
103
-        {
104
-          $this->element->setData(Element::DATA_ARTIST, $data['album']['artist']);
105
-        }
106
-        if (array_key_exists('artists', $data['album']))
107
-        {
108
-          if (count($data['album']['artists']))
109
-          {
110
-            if (array_key_exists('name', $data['album']['artists'][0]))
111
-            {
112
-              $this->element->setData(Element::DATA_ARTIST, $data['album']['artists'][0]['name']);
113
-            }
114
-          }
115
-        }
116
-        if (array_key_exists('name', $data['album']))
117
-        {
118
-          $this->element->setData(Element::DATA_TITLE, $data['album']['name']);
119
-        }
108
+        $this->getApiConnector()->setElementDatasWithResponse($response, array(
109
+          Element::DATA_ARTIST         => array('album' => array('artists' => array(0 => 'name'))),
110
+        ));
120 111
       }
121 112
     }
122 113
     
114
+    
115
+    
116
+    
117
+    //if ($this->element->getData(Element::DATA_TYPE) == 'track')
118
+    //{
119
+    //  $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:track:'.$ref_id);
120
+    //  
121
+    //  if (array_key_exists('track', $data))
122
+    //  {
123
+    //    if (array_key_exists('available', $data['track']))
124
+    //    {
125
+    //      if ($data['track']['available'])
126
+    //      {
127
+    //        if (array_key_exists('artist', $data['track']))
128
+    //        {
129
+    //          $this->element->setData(Element::DATA_ARTIST, $data['track']['artist']);
130
+    //        }
131
+    //        if (array_key_exists('artists', $data['track']))
132
+    //        {
133
+    //          if (count($data['track']['artists']))
134
+    //          {
135
+    //            if (array_key_exists('name', $data['track']['artists'][0]))
136
+    //            {
137
+    //              $this->element->setData(Element::DATA_ARTIST, $data['track']['artists'][0]['name']);
138
+    //            }
139
+    //          }
140
+    //        }
141
+    //        if (array_key_exists('name', $data['track']))
142
+    //        {
143
+    //          $this->element->setData(Element::DATA_TITLE, $data['track']['name']);
144
+    //        }
145
+    //      }
146
+    //    }
147
+    //  }
148
+    //}
149
+    //if ($this->element->getData(Element::DATA_TYPE) == 'album')
150
+    //{
151
+    //  $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:album:'.$ref_id);
152
+    //  
153
+    //  if (array_key_exists('album', $data))
154
+    //  {
155
+    //    if (array_key_exists('artist', $data['album']))
156
+    //    {
157
+    //      $this->element->setData(Element::DATA_ARTIST, $data['album']['artist']);
158
+    //    }
159
+    //    if (array_key_exists('artists', $data['album']))
160
+    //    {
161
+    //      if (count($data['album']['artists']))
162
+    //      {
163
+    //        if (array_key_exists('name', $data['album']['artists'][0]))
164
+    //        {
165
+    //          $this->element->setData(Element::DATA_ARTIST, $data['album']['artists'][0]['name']);
166
+    //        }
167
+    //      }
168
+    //    }
169
+    //    if (array_key_exists('name', $data['album']))
170
+    //    {
171
+    //      $this->element->setData(Element::DATA_TITLE, $data['album']['name']);
172
+    //    }
173
+    //  }
174
+    //}
175
+    
123 176
   }
124 177
   
125 178
   public function proceedEmbedCode()

+ 15 - 0
src/Muzich/CoreBundle/Factory/UrlMatchs.php Dosyayı Görüntüle

@@ -61,4 +61,19 @@ class UrlMatchs
61 61
       "#^\/track\/([0-9]+)#" => 1
62 62
     )
63 63
   );
64
+  
65
+  public static $spotify = array(
66
+    Element::TYPE_TRACK => array(
67
+      // http://open.spotify.com/track/7ylMdCOkqumPAwIMb6j2D5
68
+      "#^\/track\/([a-zA-Z0-9]+)#" => 1
69
+    ),
70
+    Element::TYPE_ALBUM => array(
71
+      // http://open.spotify.com/album/1VAB3Xn92dPKPWzocgQqkh
72
+      "#^\/album\/([a-zA-Z0-9]+)#" => 1
73
+    ),
74
+    Element::TYPE_PLAYLIST => array(
75
+      // http://open.spotify.com/user/bux/playlist/2kNeCiQaATbUi3lixhNwco
76
+      "#^\/user\/([a-zA-Z0-9_-]+)\/playlist\/([a-zA-Z0-9]+)#" => 2
77
+    )
78
+  );
64 79
 }

+ 25 - 0
src/Muzich/CoreBundle/Tests/ElementFactory/ElementFactoryTest.php Dosyayı Görüntüle

@@ -573,6 +573,31 @@ class ElementFactoryTest extends UnitTest
573 573
     //  $bux, 
574 574
     //  'http://vimeo.com/43258820'
575 575
     //));
576
+    //
577
+    ///*
578
+    // * Spotify
579
+    // *
580
+    // */
581
+    //
582
+    //$this->assertEquals(array(
583
+    //  'data_ref_id' => '1Uz3BDNxgLI0S6ACV7yXlT',
584
+    //  'data_title'  => 'Narkotek Old School Tracks',
585
+    //  'data_artist' => 'Guigoo Narkotek',
586
+    //  'data_type'   => 'album'
587
+    //),$this->proceed_element_datas_api(
588
+    //  $bux, 
589
+    //  'http://open.spotify.com/album/1Uz3BDNxgLI0S6ACV7yXlT'
590
+    //));
591
+    //
592
+    //$this->assertEquals(array(
593
+    //  'data_ref_id' => '3d5FWJe19DkUJaO2wDEQHY',
594
+    //  'data_title'  => 'Outta space',
595
+    //  'data_artist' => 'Guigoo Narkotek',
596
+    //  'data_type'   => 'track'
597
+    //),$this->proceed_element_datas_api(
598
+    //  $bux, 
599
+    //  'http://open.spotify.com/track/3d5FWJe19DkUJaO2wDEQHY'
600
+    //));
576 601
     
577 602
   }
578 603