Procházet zdrojové kódy

Refactorisation du code de dialogue avec l'api. Code refactorisé pour soundcloud pour le moment.

Sevajol Bastien před 11 roky
rodič
revize
499796aa57

+ 13 - 5
src/Muzich/CoreBundle/Factory/ElementFactory.php Zobrazit soubor

@@ -4,8 +4,8 @@ namespace Muzich\CoreBundle\Factory;
4 4
 
5 5
 use Muzich\CoreBundle\Entity\Element;
6 6
 use Symfony\Component\DependencyInjection\Container;
7
-use \Exception;
8 7
 use Doctrine\ORM\EntityManager;
8
+use Muzich\CoreBundle\lib\Api\Connector as ApiConnector;
9 9
 
10 10
 /**
11 11
  *
@@ -14,13 +14,10 @@ use Doctrine\ORM\EntityManager;
14 14
 abstract class ElementFactory
15 15
 {
16 16
   
17
-  /**
18
-   *
19
-   * @var Element 
20
-   */
21 17
   protected $element;
22 18
   protected $container;
23 19
   protected $entity_manager;
20
+  protected $api_connector;
24 21
   
25 22
   /**
26 23
    *
@@ -32,6 +29,12 @@ abstract class ElementFactory
32 29
     $this->element   = $element;
33 30
     $this->container = $container;
34 31
     $this->entity_manager = $entity_manager;
32
+    $this->api_connector = new ApiConnector($element);
33
+  }
34
+  
35
+  protected function getApiConnector()
36
+  {
37
+    return $this->api_connector;
35 38
   }
36 39
   
37 40
   /**
@@ -91,6 +94,11 @@ abstract class ElementFactory
91 94
     return json_decode(curl_exec($api_url), true);
92 95
   }
93 96
   
97
+  protected function configureApiConnector()
98
+  {
99
+    
100
+  }
101
+  
94 102
 }
95 103
 
96 104
 ?>

+ 234 - 192
src/Muzich/CoreBundle/Factory/Elements/Soundcloudcom.php Zobrazit soubor

@@ -5,6 +5,7 @@ namespace Muzich\CoreBundle\Factory\Elements;
5 5
 use Muzich\CoreBundle\Factory\ElementFactory;
6 6
 use Muzich\CoreBundle\Entity\Element;
7 7
 use Muzich\CoreBundle\Util\TagLike;
8
+use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
8 9
 
9 10
 /**
10 11
  * 
@@ -12,8 +13,7 @@ use Muzich\CoreBundle\Util\TagLike;
12 13
  * @author bux
13 14
  */
14 15
 class Soundcloudcom extends ElementFactory
15
-{
16
-  
16
+{  
17 17
   /**
18 18
    * ??SET = http://soundcloud.com/matas/sets/library-project
19 19
    * ??    = http://soundcloud.com/matas/anadrakonic-waltz
@@ -41,214 +41,256 @@ class Soundcloudcom extends ElementFactory
41 41
       $match = false;
42 42
     }
43 43
     
44
-    
45
-    //$this->element->setData(Element::DATA_REF_ID, $this->element->getUrl());
46
-    
47
-    // récupération de données avec l'API
44
+    /////////////
48 45
     if ($match)
49 46
     {
50
-      // La première étape consiste a résoudre l'url
51
-      $ch = curl_init('http://api.soundcloud.com/resolve.json?url='.$this->element->getUrl().'&client_id=39946ea18e3d78d64c0ac95a025794e1');
52
-
53
-      $options = array(
54
-        CURLOPT_RETURNTRANSFER => true,
55
-        CURLOPT_HTTPHEADER => array('Content-type: application/json')
56
-      );
57
-
58
-      curl_setopt_array( $ch, $options );
59
-      $result = json_decode(curl_exec($ch));
60
-
61
-      if (isset($result->errors))
62
-      {
63
-        if (count($result->errors))
64
-        {
65
-          return;  
66
-        }
67
-      }
68
-      
69
-      if (!isset($result->location))
47
+      $this->setElementDatasWithApi();
48
+    }
49
+    
50
+    ////$this->element->setData(Element::DATA_REF_ID, $this->element->getUrl());
51
+    //
52
+    //// récupération de données avec l'API
53
+    //if ($match)
54
+    //{
55
+    //  
56
+    //  
57
+    //  
58
+    //  
59
+    //  
60
+    //  // La première étape consiste a résoudre l'url
61
+    //  $ch = curl_init('http://api.soundcloud.com/resolve.json?url='.$this->element->getUrl().'&client_id=39946ea18e3d78d64c0ac95a025794e1');
62
+    //
63
+    //  $options = array(
64
+    //    CURLOPT_RETURNTRANSFER => true,
65
+    //    CURLOPT_HTTPHEADER => array('Content-type: application/json')
66
+    //  );
67
+    //
68
+    //  curl_setopt_array( $ch, $options );
69
+    //  $result = json_decode(curl_exec($ch));
70
+    //
71
+    //  if (isset($result->errors))
72
+    //  {
73
+    //    if (count($result->errors))
74
+    //    {
75
+    //      return;  
76
+    //    }
77
+    //  }
78
+    //  
79
+    //  if (!isset($result->location))
80
+    //  {
81
+    //    return;
82
+    //  }
83
+    //  
84
+    //  if (!$result->location)
85
+    //  {
86
+    //    return;
87
+    //  }
88
+    //  
89
+    //  $getjsonurl = $result->location;
90
+    //  // On a maintenant la bonne url pour demander les infos
91
+    //  $ch = curl_init($getjsonurl);
92
+    //  curl_setopt_array($ch, $options);
93
+    //  $result = json_decode(curl_exec($ch), true);
94
+    //  
95
+    //  // En premier lieux il nous faut être sur d'avoir le droit d'utiliser le lecteur exportable
96
+    //  $sharing = false;
97
+    //  if (array_key_exists('sharing', $result) && array_key_exists('embeddable_by', $result))
98
+    //  {
99
+    //    if ($result['sharing'] == 'public' && ($result['embeddable_by'] == 'all' || $result['embeddable_by'] == 'me'))
100
+    //    {
101
+    //      $sharing = true;
102
+    //    }
103
+    //  }
104
+    //  
105
+    //  if ($sharing)
106
+    //  {
107
+    //    if (array_key_exists('id', $result) )
108
+    //    {
109
+    //      $this->element->setData(Element::DATA_REF_ID, $result['id']);
110
+    //    }
111
+    //    
112
+    //    if (array_key_exists('uri', $result) )
113
+    //    {
114
+    //      $this->element->setData(Element::DATA_NORMALIZED_URL, $result['uri']);
115
+    //    }
116
+    //
117
+    //    if (array_key_exists('artwork_url', $result) )
118
+    //    {
119
+    //      if ($result['artwork_url'])
120
+    //      {
121
+    //        $this->element->setData(Element::DATA_THUMB_URL, $result['artwork_url']);
122
+    //      }
123
+    //      else
124
+    //      {
125
+    //        if (array_key_exists('user', $result) )
126
+    //        {
127
+    //          if (array_key_exists('avatar_url', $result['user']) )
128
+    //          {
129
+    //            if ($result['user']['avatar_url'])
130
+    //            {
131
+    //              $this->element->setData(Element::DATA_THUMB_URL, $result['user']['avatar_url']);
132
+    //            }
133
+    //          }
134
+    //        }
135
+    //      }
136
+    //    }
137
+    //    
138
+    //    if (array_key_exists('kind', $result) )
139
+    //    {
140
+    //      $this->element->setData(Element::DATA_TYPE, $result['kind']);
141
+    //    }
142
+    //
143
+    //    if (array_key_exists('downloadable', $result) )
144
+    //    {
145
+    //      $this->element->setData(Element::DATA_DOWNLOAD, $result['downloadable']);
146
+    //      // FIXME
147
+    //      $this->element->setData(Element::DATA_DOWNLOAD_URL, $this->element->getUrl().'/download');
148
+    //    }
149
+    //
150
+    //    if (array_key_exists('title', $result) )
151
+    //    {
152
+    //      $this->element->setData(Element::DATA_TITLE, $result['title']);
153
+    //    }
154
+    //
155
+    //    if (array_key_exists('user', $result) )
156
+    //    {
157
+    //      $this->element->setData(Element::DATA_ARTIST, $result['user']['username']);
158
+    //    }
159
+    //    
160
+    //    $genres = '';
161
+    //    if (array_key_exists('genre', $result) )
162
+    //    {
163
+    //      if (strlen($result['genre']))
164
+    //      {
165
+    //        $genres = $result['genre'];
166
+    //      }
167
+    //    }
168
+    //    
169
+    //    $tags_list = '';
170
+    //    if (array_key_exists('tag_list', $result) )
171
+    //    {
172
+    //      if (strlen($result['tag_list']))
173
+    //      {
174
+    //        $tags_list = $result['tag_list'];
175
+    //      }
176
+    //    }
177
+    //    
178
+    //    $tags_string = $genres.' '.$tags_list.' '.str_replace(' ', '-', $genres);
179
+    //    $tags_like = array();
180
+    //    if (strlen($tags_string))
181
+    //    {
182
+    //      $tag_like = new TagLike($this->entity_manager);
183
+    //      foreach (explode(' ', $tags_string) as $word)
184
+    //      {
185
+    //        $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
186
+    //        if (count($similar_tags))
187
+    //        {
188
+    //          if ($similar_tags['same_found'])
189
+    //          {
190
+    //            $tags_like[] = $similar_tags['tags'][0]['name'];
191
+    //          }
192
+    //        }
193
+    //      }
194
+    //      $tags_like[] = $genres;
195
+    //      if (count($tags_like))
196
+    //      {
197
+    //        $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
198
+    //      }
199
+    //    }
200
+    //    
201
+    //  }
202
+    //}
203
+  }
204
+  
205
+  protected function setElementDatasWithApi()
206
+  {
207
+    if (($response = $this->getApiDatasResponse()))
208
+    {
209
+      if ($response->get('sharing') == 'public')
70 210
       {
71
-        return;
211
+        $this->setElementSharingData($response);
212
+        $this->setTagsData($response);
72 213
       }
73 214
       
74
-      if (!$result->location)
215
+      if ($response->get('embeddable_by') == 'all')
75 216
       {
76
-        return;
217
+        $this->setElementEmbeddableData($response);
77 218
       }
78
-      
79
-      $getjsonurl = $result->location;
80
-      // On a maintenant la bonne url pour demander les infos
81
-      $ch = curl_init($getjsonurl);
82
-      curl_setopt_array($ch, $options);
83
-      $result = json_decode(curl_exec($ch), true);
84
-
85
-      /*
86
-       * array
87
-          'kind' => string 'track' (length=5)
88
-          'id' => int 57452080
89
-          'created_at' => string '2012/08/24 20:39:44 +0000' (length=25)
90
-          'user_id' => int 11235441
91
-          'duration' => int 4206558
92
-          'commentable' => boolean true
93
-          'state' => string 'finished' (length=8)
94
-          'original_content_size' => int 168196212
95
-          'sharing' => string 'public' (length=6)
96
-          'tag_list' => string '' (length=0)
97
-          'permalink' => string 'mechanika-crew-andrew-dj-set' (length=28)
98
-          'streamable' => boolean true
99
-          'embeddable_by' => string 'all' (length=3)
100
-          'downloadable' => boolean true
101
-          'purchase_url' => null
102
-          'label_id' => null
103
-          'purchase_title' => null
104
-          'genre' => string '' (length=0)
105
-          'title' => string 'MECHANIKA CREW / ANDREW dj set 24.08.12' (length=39)
106
-          'description' => string '' (length=0)
107
-          'label_name' => string '' (length=0)
108
-          'release' => string '' (length=0)
109
-          'track_type' => string '' (length=0)
110
-          'key_signature' => string '' (length=0)
111
-          'isrc' => string '' (length=0)
112
-          'video_url' => null
113
-          'bpm' => null
114
-          'release_year' => null
115
-          'release_month' => null
116
-          'release_day' => null
117
-          'original_format' => string 'mp3' (length=3)
118
-          'license' => string 'all-rights-reserved' (length=19)
119
-          'uri' => string 'http://api.soundcloud.com/tracks/57452080' (length=41)
120
-          'user' => 
121
-            array
122
-              'id' => int 11235441
123
-              'kind' => string 'user' (length=4)
124
-              'permalink' => string 'user4818423' (length=11)
125
-              'username' => string 'Andrea Andrew mechanika' (length=23)
126
-              'uri' => string 'http://api.soundcloud.com/users/11235441' (length=40)
127
-              'permalink_url' => string 'http://soundcloud.com/user4818423' (length=33)
128
-              'avatar_url' => string 'http://i1.sndcdn.com/avatars-000023343399-cp1lvg-large.jpg?04ad178' (length=66)
129
-          'permalink_url' => string 'http://soundcloud.com/user4818423/mechanika-crew-andrew-dj-set' (length=62)
130
-          'artwork_url' => string 'http://i1.sndcdn.com/artworks-000029057120-6fz4k4-large.jpg?04ad178' (length=67)
131
-          'waveform_url' => string 'http://w1.sndcdn.com/udItSnzA5J22_m.png' (length=39)
132
-          'stream_url' => string 'http://api.soundcloud.com/tracks/57452080/stream' (length=48)
133
-          'download_url' => string 'http://api.soundcloud.com/tracks/57452080/download' (length=50)
134
-          'playback_count' => int 502
135
-          'download_count' => int 85
136
-          'favoritings_count' => int 12
137
-          'comment_count' => int 13
138
-          'attachments_uri' => string 'http://api.soundcloud.com/tracks/57452080/attachments' (length=53)
139
-
140
-       */
141
-      
142
-      // En premier lieux il nous faut être sur d'avoir le droit d'utiliser le lecteur exportable
143
-      $sharing = false;
144
-      if (array_key_exists('sharing', $result) && array_key_exists('embeddable_by', $result))
219
+    }
220
+  }
221
+  
222
+  protected function getApiDatasResponse()
223
+  {
224
+    if (($response = $this->getApiConnector()->getResponseForUrl('http://api.soundcloud.com/resolve.json?url='.$this->element->getUrl().'&client_id=39946ea18e3d78d64c0ac95a025794e1')))
225
+    {
226
+      if ($response->haveNot('errors') && $response->have('location'))
145 227
       {
146
-        if ($result['sharing'] == 'public' && ($result['embeddable_by'] == 'all' || $result['embeddable_by'] == 'me'))
147
-        {
148
-          $sharing = true;
149
-        }
228
+        return $this->getApiConnector()->getResponseForUrl($response->get('location'));
150 229
       }
151
-      
152
-      if ($sharing)
230
+    }
231
+    
232
+    return null;
233
+  }
234
+  
235
+  protected function setElementSharingData(ApiResponse $response)
236
+  {
237
+    $this->getApiConnector()->setElementDatasWithResponse($response, array(
238
+      Element::DATA_NORMALIZED_URL => 'uri',
239
+      Element::DATA_TYPE           => 'kind',
240
+      Element::DATA_DOWNLOAD       => 'downloadable',
241
+      Element::DATA_DOWNLOAD_URL   => 'download_url',
242
+      Element::DATA_TITLE          => 'title',
243
+      Element::DATA_ARTIST         => array('user' => 'username')
244
+    ));
245
+    
246
+    $this->setThumbnailData($response);
247
+  }
248
+  
249
+  protected function setThumbnailData(ApiResponse $response)
250
+  {
251
+    if ($response->have('artwork_url'))
252
+    {
253
+      $this->element->setData(Element::DATA_THUMB_URL, $response->get('artwork_url'));
254
+    }
255
+    elseif ($response->have(array('user' => 'avatar_url')))
256
+    {
257
+      $this->element->setData(Element::DATA_THUMB_URL, $response->get(array('user' => 'avatar_url')));
258
+    }
259
+  }
260
+  
261
+  protected function setTagsData(ApiResponse $response)
262
+  {
263
+    $tags_string = $response->get('genre').' '.$response->get('tag_list').' '.str_replace(' ', '-', $response->get('genre'));
264
+    $tags_like = array();
265
+    if (strlen($tags_string))
266
+    {
267
+      $tag_like = new TagLike($this->entity_manager);
268
+      foreach (explode(' ', $tags_string) as $word)
153 269
       {
154
-        if (array_key_exists('id', $result) )
155
-        {
156
-          $this->element->setData(Element::DATA_REF_ID, $result['id']);
157
-        }
158
-        
159
-        if (array_key_exists('uri', $result) )
160
-        {
161
-          $this->element->setData(Element::DATA_NORMALIZED_URL, $result['uri']);
162
-        }
163
-
164
-        if (array_key_exists('artwork_url', $result) )
165
-        {
166
-          if ($result['artwork_url'])
167
-          {
168
-            $this->element->setData(Element::DATA_THUMB_URL, $result['artwork_url']);
169
-          }
170
-          else
171
-          {
172
-            if (array_key_exists('user', $result) )
173
-            {
174
-              if (array_key_exists('avatar_url', $result['user']) )
175
-              {
176
-                if ($result['user']['avatar_url'])
177
-                {
178
-                  $this->element->setData(Element::DATA_THUMB_URL, $result['user']['avatar_url']);
179
-                }
180
-              }
181
-            }
182
-          }
183
-        }
184
-        
185
-        if (array_key_exists('kind', $result) )
186
-        {
187
-          $this->element->setData(Element::DATA_TYPE, $result['kind']);
188
-        }
189
-
190
-        if (array_key_exists('downloadable', $result) )
270
+        $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
271
+        if (count($similar_tags))
191 272
         {
192
-          $this->element->setData(Element::DATA_DOWNLOAD, $result['downloadable']);
193
-          // FIXME
194
-          $this->element->setData(Element::DATA_DOWNLOAD_URL, $this->element->getUrl().'/download');
195
-        }
196
-
197
-        if (array_key_exists('title', $result) )
198
-        {
199
-          $this->element->setData(Element::DATA_TITLE, $result['title']);
200
-        }
201
-
202
-        if (array_key_exists('user', $result) )
203
-        {
204
-          $this->element->setData(Element::DATA_ARTIST, $result['user']['username']);
205
-        }
206
-        
207
-        $genres = '';
208
-        if (array_key_exists('genre', $result) )
209
-        {
210
-          if (strlen($result['genre']))
211
-          {
212
-            $genres = $result['genre'];
213
-          }
214
-        }
215
-        
216
-        $tags_list = '';
217
-        if (array_key_exists('tag_list', $result) )
218
-        {
219
-          if (strlen($result['tag_list']))
273
+          if ($similar_tags['same_found'])
220 274
           {
221
-            $tags_list = $result['tag_list'];
275
+            $tags_like[] = $similar_tags['tags'][0]['name'];
222 276
           }
223 277
         }
224
-        
225
-        $tags_string = $genres.' '.$tags_list.' '.str_replace(' ', '-', $genres);
226
-        $tags_like = array();
227
-        if (strlen($tags_string))
228
-        {
229
-          $tag_like = new TagLike($this->entity_manager);
230
-          foreach (explode(' ', $tags_string) as $word)
231
-          {
232
-            $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
233
-            if (count($similar_tags))
234
-            {
235
-              if ($similar_tags['same_found'])
236
-              {
237
-                $tags_like[] = $similar_tags['tags'][0]['name'];
238
-              }
239
-            }
240
-          }
241
-          $tags_like[] = $genres;
242
-          if (count($tags_like))
243
-          {
244
-            $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
245
-          }
246
-        }
247
-        
278
+      }
279
+      $tags_like[] = $response->get('genre');
280
+      if (count($tags_like))
281
+      {
282
+        $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
248 283
       }
249 284
     }
250 285
   }
251 286
   
287
+  protected function setElementEmbeddableData($response)
288
+  {
289
+    $this->getApiConnector()->setElementDatasWithResponse($response, array(
290
+      Element::DATA_REF_ID => 'id'
291
+    ));
292
+  }
293
+  
252 294
   public function proceedEmbedCode()
253 295
   {
254 296
     if (($ref_id = $this->element->getData(Element::DATA_REF_ID)) 

+ 126 - 0
src/Muzich/CoreBundle/Tests/Api/ResponseTest.php Zobrazit soubor

@@ -0,0 +1,126 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Api;
4
+
5
+use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
6
+
7
+class ResponseTest extends \PHPUnit_Framework_TestCase
8
+{
9
+  
10
+  protected $response_content = array(
11
+    'id'     => 1,
12
+    'title'  => '',
13
+    'xtitle'  => '   ',
14
+    'tags'   => array(),
15
+    'url'    => null,
16
+    'count'  => 0,
17
+    'xcount' => '0',
18
+    'user' => array(
19
+      'username' => 'bob',
20
+      'email'    => 'bob@mail.com',
21
+      'address'  => array(
22
+        'town'        => 'Paris',
23
+        'country'     => 'France',
24
+        'departement' => ''
25
+      )
26
+    )
27
+  );
28
+  
29
+  public function testHaving()
30
+  {
31
+    $response = new ApiResponse($this->response_content);
32
+    
33
+    $this->assertTrue($response->have('id'));
34
+    $this->assertTrue($response->have('user'));
35
+    $this->assertFalse($response->have('toto'));
36
+    $this->assertFalse($response->have('title'));
37
+    $this->assertFalse($response->have('xtitle'));
38
+    $this->assertFalse($response->have('url'));
39
+    $this->assertTrue($response->have('count'));
40
+    $this->assertTrue($response->have('xcount'));
41
+    
42
+    $this->assertFalse($response->have('toto', false));
43
+    $this->assertTrue($response->have('title', false));
44
+    $this->assertTrue($response->have('xtitle', false));
45
+    $this->assertTrue($response->have('tags', false));
46
+    
47
+    $this->assertTrue($response->have(array('user' => 'username')));
48
+    $this->assertTrue($response->have(array('user' => 'address')));
49
+    $this->assertFalse($response->have(array('user' => 'toto')));
50
+    $this->assertTrue($response->have(array('user' => array('address' => 'town'))));
51
+    $this->assertFalse($response->have(array('user' => array('address' => 'toto'))));
52
+    $this->assertFalse($response->have(array('user' => array('address' => 'departement'))));
53
+    
54
+    $this->assertFalse($response->have(array('user' => array('address' => 'toto'))));
55
+    $this->assertTrue($response->have(array('user' => array('address' => 'departement')), false));
56
+  }
57
+  
58
+  public function testGet()
59
+  {
60
+    $response = new ApiResponse($this->response_content);
61
+    
62
+    $this->assertEquals(1, $response->get('id'));
63
+    $this->assertEquals(array(
64
+      'username' => 'bob',
65
+      'email'    => 'bob@mail.com',
66
+      'address'  => array(
67
+        'town'        => 'Paris',
68
+        'country'     => 'France',
69
+        'departement' => ''
70
+      )
71
+    ), $response->get('user'));
72
+    $this->assertEquals(null, $response->get('toto'));
73
+    $this->assertEquals(null, $response->get('title'));
74
+    $this->assertEquals(null, $response->get('xtitle'));
75
+    $this->assertEquals(null, $response->get('tags'));
76
+    $this->assertEquals(null, $response->get('url'));
77
+    $this->assertEquals(0, $response->get('count'));
78
+    $this->assertEquals('0', $response->get('xcount'));
79
+    
80
+    $this->assertEquals('bob', $response->get(array('user' => 'username')));
81
+    $this->assertEquals(array(
82
+      'town'        => 'Paris',
83
+      'country'     => 'France',
84
+      'departement' => ''
85
+    ), $response->get(array('user' => 'address')));
86
+    $this->assertEquals(null, $response->get(array('user' => 'toto')));
87
+    $this->assertEquals('Paris', $response->get(array('user' => array('address' => 'town'))));
88
+    $this->assertEquals(null, $response->get(array('user' => array('address' => 'toto'))));
89
+    $this->assertEquals(null, $response->get(array('user' => array('address' => 'departement'))));
90
+  }
91
+  
92
+  public function testGetNotStrict()
93
+  {
94
+    $response = new ApiResponse($this->response_content);
95
+    
96
+    $this->assertEquals(1, $response->get('id', false));
97
+    $this->assertEquals(array(
98
+      'username' => 'bob',
99
+      'email'    => 'bob@mail.com',
100
+      'address'  => array(
101
+        'town'        => 'Paris',
102
+        'country'     => 'France',
103
+        'departement' => ''
104
+      )
105
+    ), $response->get('user', false));
106
+    $this->assertEquals(null, $response->get('toto', false));
107
+    $this->assertEquals('', $response->get('title', false));
108
+    $this->assertEquals('   ', $response->get('xtitle', false));
109
+    $this->assertEquals(array(), $response->get('tags', false));
110
+    $this->assertEquals(null, $response->get('url', false));
111
+    $this->assertEquals(0, $response->get('count', false));
112
+    $this->assertEquals('0', $response->get('xcount', false));
113
+    
114
+    $this->assertEquals('bob', $response->get(array('user' => 'username'), false));
115
+    $this->assertEquals(array(
116
+      'town'        => 'Paris',
117
+      'country'     => 'France',
118
+      'departement' => ''
119
+    ), $response->get(array('user' => 'address'), false));
120
+    $this->assertEquals(null, $response->get(array('user' => 'toto'), false));
121
+    $this->assertEquals('Paris', $response->get(array('user' => array('address' => 'town'), false)));
122
+    $this->assertEquals(null, $response->get(array('user' => array('address' => 'toto'), false)));
123
+    $this->assertEquals('', $response->get(array('user' => array('address' => 'departement'), false)));
124
+  }
125
+  
126
+}

+ 7 - 7
src/Muzich/CoreBundle/Tests/ElementFactory/ElementFactoryTest.php Zobrazit soubor

@@ -371,7 +371,7 @@ class ElementFactoryTest extends UnitTest
371 371
       //'data_thumb_url' => 'http://i1.sndcdn.com/artworks-000000514203-fsvbcj-large.jpg?51826bf',
372 372
       'data_type' => 'playlist',
373 373
       'data_download' => null,
374
-      'data_download_url' => 'http://soundcloud.com/matas/sets/library-project/download',
374
+      'data_download_url' => null,
375 375
       'data_artist' => 'matas',
376 376
       'data_normalized_url' => 'http://api.soundcloud.com/playlists/3770',
377 377
       'data_tags' => array(0 => '')
@@ -394,8 +394,8 @@ class ElementFactoryTest extends UnitTest
394 394
       'data_title' => 'White Stripes Vs Led Zeppelin - Icky Kinky Love (Rock Mashup) DJ Zebra',
395 395
       //'data_thumb_url' => 'http://i1.sndcdn.com/artworks-000000514203-fsvbcj-large.jpg?51826bf',
396 396
       'data_type' => 'track',
397
-      'data_download' => false,
398
-      'data_download_url' => 'https://soundcloud.com/mixessss3/white-stripes-vs-led-zeppelin-icky-kinky-love-rock-mashup-dj-zebra/download',
397
+      'data_download' => null,
398
+      'data_download_url' => null,
399 399
       'data_artist' => 'Mixes and Mashups #3',
400 400
       'data_normalized_url' => 'http://api.soundcloud.com/tracks/2215186',
401 401
       'data_tags' => array(0 => 'Rock', 1 => 'rock  ')
@@ -419,8 +419,8 @@ class ElementFactoryTest extends UnitTest
419 419
       'data_title' => 'Above Hyperion (redux)',
420 420
       //'data_thumb_url' => 'http://i1.sndcdn.com/artworks-000001536693-gb1n5v-large.jpg?51826bf',
421 421
       'data_type' => 'track',
422
-      'data_download' => false,
423
-      'data_download_url' => 'http://soundcloud.com/matas/above-hyperion-redux/download',
422
+      'data_download' => null,
423
+      'data_download_url' => null,
424 424
       'data_artist' => 'matas',
425 425
       'data_tags' => array(
426 426
         0 => 'Spacestep'
@@ -465,7 +465,7 @@ class ElementFactoryTest extends UnitTest
465 465
       'data_ref_id' => '120468',
466 466
       'data_title' => '6:00 AM',
467 467
       'data_type' => 'album',
468
-      'data_thumb_url' => 'http://imgjam.com/albums/s120/120468/covers/1.100.jpg',
468
+      'data_thumb_url' => 'https://imgjam.com/albums/s120/120468/covers/1.100.jpg',
469 469
       'data_artist' => 'Azyd Azylum',
470 470
       'data_tags' => array(
471 471
         0 => 'Metal',
@@ -486,7 +486,7 @@ class ElementFactoryTest extends UnitTest
486 486
       'data_ref_id' => '207079',
487 487
       'data_title' => 'Insanity',
488 488
       'data_type' => 'track',
489
-      'data_thumb_url' => 'http://imgjam.com/albums/s30/30661/covers/1.100.jpg',
489
+      'data_thumb_url' => 'https://imgjam.com/albums/s30/30661/covers/1.100.jpg',
490 490
       'data_artist' => 'Ptit lutin',
491 491
       'data_tags' => array(
492 492
         0 => 'Techno',

+ 38 - 0
src/Muzich/CoreBundle/lib/Api/Connector.php Zobrazit soubor

@@ -0,0 +1,38 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib\Api;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+
7
+class Connector
8
+{
9
+  
10
+  private $element;
11
+  
12
+  public function __construct(Element $element)
13
+  {
14
+    $this->element = $element;
15
+  }
16
+  
17
+  public function getResponseForUrl($url)
18
+  {
19
+    $api_url = curl_init($url);
20
+    
21
+    $options = array(
22
+      CURLOPT_RETURNTRANSFER => true,
23
+      CURLOPT_HTTPHEADER => array('Content-type: application/json')
24
+    );
25
+      
26
+    curl_setopt_array($api_url, $options);
27
+    return new Response(json_decode(curl_exec($api_url), true));
28
+  }
29
+  
30
+  public function setElementDatasWithResponse(Response $response, $parameters)
31
+  {
32
+    foreach ($parameters as $data_id => $searched)
33
+    {
34
+      $this->element->setData($data_id, $response->get($searched));
35
+    }
36
+  }
37
+  
38
+}

+ 85 - 0
src/Muzich/CoreBundle/lib/Api/Response.php Zobrazit soubor

@@ -0,0 +1,85 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib\Api;
4
+
5
+class Response
6
+{
7
+  
8
+  protected $content = array();
9
+  
10
+  public function __construct($content)
11
+  {
12
+    $this->content = $content;
13
+  }
14
+  
15
+  public function haveNot($searched)
16
+  {
17
+    return !$this->have($searched);
18
+  }
19
+  
20
+  public function have($searched, $not_empty = true, $content = null)
21
+  {
22
+    if (!$content)
23
+      $content = $this->content;
24
+    
25
+    if (is_array($searched))
26
+    {
27
+      foreach ($searched as $searched_key => $searched_subvalue)
28
+      {
29
+        return $this->have($searched_subvalue, $not_empty, $this->get($searched_key, false, $content));
30
+      }
31
+    }
32
+    
33
+    if (array_key_exists($searched, $content))
34
+    {
35
+      if ($not_empty)
36
+      {
37
+        if ((is_null($content[$searched]) || !count($content[$searched]) || !$content[$searched]) && ($content[$searched] !== 0 && $content[$searched] !== '0'))
38
+        {
39
+          return false;
40
+        }
41
+        if (is_string($content[$searched]))
42
+        {
43
+          if (trim($content[$searched]) == '')
44
+          {
45
+            return false;
46
+          }
47
+        }
48
+      }
49
+      
50
+      return true;
51
+    }
52
+    
53
+    return false;
54
+  }
55
+  
56
+  public function get($searched, $return_null_if_empty = true, $content = null)
57
+  {
58
+    if (!$content)
59
+      $content = $this->content;
60
+    
61
+    if (is_array($searched))
62
+    {
63
+      foreach ($searched as $searched_key => $searched_subvalue)
64
+      {
65
+        if ($this->have($searched_key, true, $content))
66
+        {
67
+          return $this->get($searched_subvalue, $return_null_if_empty, $this->get($searched_key, $return_null_if_empty, $content));
68
+        }
69
+        else
70
+        {
71
+          
72
+          return null;
73
+        }
74
+      }
75
+    }
76
+    
77
+    if ($this->have($searched, $return_null_if_empty, $content))
78
+    {
79
+      return $content[$searched];
80
+    }
81
+    
82
+    return null;
83
+  }
84
+  
85
+}