Mixcloudcom.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 Mixcloudcom extends ElementFactory
  10. {
  11. public function __construct(Element $element, Container $container, EntityManager $entity_manager)
  12. {
  13. $this->url_matchs = UrlMatchs::$mixcloud;
  14. parent::__construct($element, $container, $entity_manager);
  15. }
  16. public function proceedDatas()
  17. {
  18. $this->setElementDatasWithApi();
  19. $this->proceedThumbnailUrl();
  20. $this->proceedEmbedCode();
  21. }
  22. protected function setElementDatasWithApi()
  23. {
  24. if (($response = $this->getApiDatasResponse()))
  25. {
  26. $this->setElementSharingData($response);
  27. $this->setTagsData($response);
  28. }
  29. }
  30. protected function getApiDatasResponse()
  31. {
  32. if (($response = $this->getApiConnector()->getResponseForUrl('http://api.mixcloud.com'.$this->getCleanedUrl())))
  33. if ($response->haveNot('error'))
  34. return $response;
  35. return null;
  36. }
  37. protected function setElementSharingData(ApiResponse $response)
  38. {
  39. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  40. Element::DATA_TITLE => 'name',
  41. Element::DATA_NORMALIZED_URL => 'url',
  42. Element::DATA_REF_ID => 'key',
  43. Element::DATA_TYPE => Element::TYPE_TRACK,
  44. Element::DATA_ARTIST => array('user' => 'name'),
  45. Element::DATA_THUMB_URL => array('pictures' => 'medium')
  46. ));
  47. }
  48. protected function setTagsData(ApiResponse $response)
  49. {
  50. $tags = $response->get('tags');
  51. $tags_array = array();
  52. if (count($tags))
  53. {
  54. foreach ($tags as $tag_data)
  55. {
  56. $tags_array[] = $tag_data['name'];
  57. }
  58. }
  59. $this->setDataTagsForElement(implode(' ', $tags_array));
  60. }
  61. public function proceedEmbedCode()
  62. {
  63. if (($response = $this->getApiConnector()->getResponseForUrl(
  64. 'http://api.mixcloud.com'.$this->element->getData(Element::DATA_REF_ID).'embed-json/?width=100%'
  65. )))
  66. {
  67. if ($response->get('html'))
  68. $this->element->setEmbed($response->get('html'));
  69. }
  70. }
  71. }