Soundcloudcom.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Muzich\CoreBundle\Factory\Elements;
  3. use Muzich\CoreBundle\Factory\ElementFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
  6. use Muzich\CoreBundle\Factory\UrlMatchs;
  7. use Symfony\Component\DependencyInjection\Container;
  8. use Doctrine\ORM\EntityManager;
  9. class Soundcloudcom extends ElementFactory
  10. {
  11. public function __construct(Element $element, Container $container, EntityManager $entity_manager)
  12. {
  13. $this->url_matchs = UrlMatchs::$soundcloud;
  14. parent::__construct($element, $container, $entity_manager);
  15. }
  16. public function proceedDatas()
  17. {
  18. $this->cleanUrl();
  19. $this->setElementDatasWithApi();
  20. $this->proceedThumbnailUrl();
  21. $this->proceedEmbedCode();
  22. }
  23. protected function cleanUrl()
  24. {
  25. if (strpos($this->element->getUrl(), '#') !== false)
  26. {
  27. $this->element->setUrl(substr($this->element->getUrl(), 0, strpos($this->element->getUrl(), '#')));
  28. }
  29. }
  30. protected function setElementDatasWithApi()
  31. {
  32. if (($response = $this->getApiDatasResponse()))
  33. {
  34. if ($response->get('sharing') == 'public')
  35. {
  36. $this->setElementSharingData($response);
  37. $this->setTagsData($response);
  38. }
  39. if ($response->get('embeddable_by') != 'none')
  40. {
  41. $this->setElementEmbeddableData($response);
  42. }
  43. }
  44. }
  45. protected function getApiDatasResponse()
  46. {
  47. if (($response = $this->getApiConnector()->getResponseForUrl('https://api.soundcloud.com.bux.fr/resolve.json?url='.$this->element->getUrl().'&client_id=39946ea18e3d78d64c0ac95a025794e1')))
  48. {
  49. if ($response->haveNot('errors') && $response->have('location'))
  50. {
  51. return $this->getApiConnector()->getResponseForUrl($response->get('location'));
  52. }
  53. }
  54. return null;
  55. }
  56. protected function setElementSharingData(ApiResponse $response)
  57. {
  58. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  59. Element::DATA_NORMALIZED_URL => 'uri',
  60. Element::DATA_TYPE => 'kind',
  61. Element::DATA_DOWNLOAD => 'downloadable',
  62. Element::DATA_DOWNLOAD_URL => 'download_url',
  63. Element::DATA_TITLE => 'title',
  64. Element::DATA_ARTIST => array('user' => 'username')
  65. ));
  66. $this->setThumbnailData($response);
  67. }
  68. protected function setThumbnailData(ApiResponse $response)
  69. {
  70. if ($response->have('artwork_url'))
  71. {
  72. $this->element->setData(Element::DATA_THUMB_URL, $response->get('artwork_url'));
  73. }
  74. elseif ($response->have(array('user' => 'avatar_url')))
  75. {
  76. $this->element->setData(Element::DATA_THUMB_URL, $response->get(array('user' => 'avatar_url')));
  77. }
  78. }
  79. protected function setTagsData(ApiResponse $response)
  80. {
  81. $tags_string = $response->get('genre').' '.$response->get('tag_list').' '.str_replace(' ', '-', $response->get('genre'));
  82. $this->setDataTagsForElement($tags_string, array($response->get('genre')));
  83. }
  84. protected function setElementEmbeddableData($response)
  85. {
  86. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  87. Element::DATA_REF_ID => 'id'
  88. ));
  89. }
  90. public function proceedThumbnailUrl()
  91. {
  92. if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
  93. {
  94. $this->element->setThumbnailUrl($thumb);
  95. }
  96. }
  97. public function proceedEmbedCode()
  98. {
  99. if (($ref_id = $this->element->getData(Element::DATA_REF_ID))
  100. && ($this->element->getData(Element::DATA_TYPE) == 'track' || $this->element->getData(Element::DATA_TYPE) == 'playlist' ))
  101. {
  102. $ref_id = $this->element->getUrl();
  103. $embed_id = md5($ref_id);
  104. $height = $this->container->getParameter('soundcloud_player_height');
  105. $this->element->setEmbed(
  106. '<object height="'.$height.'" width="100%" id="embed_'.$embed_id.'" '
  107. .'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
  108. <param name="movie" value="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;'
  109. .'enable_api=true&amp;object_id=embed_'.$embed_id.'"></param>
  110. <param name="allowscriptaccess" value="always"></param>
  111. <embed allowscriptaccess="always" height="'.$height.'" '
  112. .'src="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;enable_api=true'
  113. .'&amp;object_id=embed_'.$embed_id.'" type="application/x-shockwave-flash" '
  114. .'width="100%" name="embed_'.$embed_id.'"></embed>
  115. </object>'
  116. );
  117. }
  118. }
  119. }