ElementManager.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Muzich\CoreBundle\Managers;
  3. use Muzich\CoreBundle\Entity\Element;
  4. use Muzich\CoreBundle\Entity\User;
  5. use Doctrine\ORM\EntityManager;
  6. use Symfony\Component\DependencyInjection\Container;
  7. use Muzich\CoreBundle\Factory\Elements\Youtubecom;
  8. use Muzich\CoreBundle\Factory\Elements\Youtube;
  9. use Muzich\CoreBundle\Factory\Elements\Dailymotioncom;
  10. use Muzich\CoreBundle\Factory\Elements\Jamendocom;
  11. use Muzich\CoreBundle\Factory\Elements\Soundcloudcom;
  12. use Muzich\CoreBundle\Factory\Elements\Deezercom;
  13. /**
  14. *
  15. *
  16. * @author bux
  17. */
  18. class ElementManager
  19. {
  20. protected $em;
  21. protected $element;
  22. protected $container;
  23. protected $factories;
  24. /**
  25. * Procédure chargé de retourner des information destiné au
  26. * formulaire d'ajout d'un element.
  27. *
  28. * @param string $url
  29. * @return array
  30. */
  31. public static function collect($url)
  32. {
  33. return array(
  34. 'name' => null,
  35. 'tags' => array()
  36. );
  37. }
  38. /**
  39. *
  40. * @param Element $element
  41. * @param EntityManager $em
  42. * @param Container $container
  43. */
  44. public function __construct(Element $element, EntityManager $em, Container $container)
  45. {
  46. $this->element = $element;
  47. $this->em = $em;
  48. $this->container = $container;
  49. $this->factories = $this->container->getParameter('factories');
  50. $evm = new \Doctrine\Common\EventManager();
  51. $timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
  52. $evm->addEventSubscriber($timestampableListener);
  53. $this->em->getEventManager()->addEventSubscriber($timestampableListener);
  54. }
  55. /**
  56. * Procédure chargé de construire le contenu de l'élément.
  57. * nom, Code d'embed, [...]
  58. *
  59. * @param array $params
  60. * @param User $owner
  61. */
  62. public function proceedFill(User $owner, $do_tags = true)
  63. {
  64. $this->element->setOwner($owner);
  65. if ($do_tags)
  66. {
  67. $this->element->setTagsWithIds($this->em, json_decode($this->element->getTags()));
  68. }
  69. $this->determineType();
  70. $this->proceedExtraFields();
  71. }
  72. protected function setGroup()
  73. {
  74. if ($this->element->getGroup())
  75. {
  76. $group = $this->em->getRepository('MuzichCoreBundle:Group')->findOneById($this->element->getGroup());
  77. $this->element->setGroup($group);
  78. }
  79. else
  80. {
  81. $this->element->setGroup(null);
  82. }
  83. }
  84. /**
  85. * Determine le type d'objet auquel on a affaire.
  86. */
  87. public function determineType()
  88. {
  89. // On ne prend pas de risque avec le www, on l'enlève
  90. $url = str_replace('www.', '', $this->element->getUrl());
  91. preg_match("/^(http:\/\/|https:\/\/)?([^\/]+)/i", $url, $chaines);
  92. $type = 'unknow';
  93. if (array_key_exists(2, $chaines))
  94. {
  95. $host = $chaines[2];
  96. // Repérer les derniers segments
  97. preg_match("/[^\.\/]+\.[^\.\/]+$/",$host,$chaines);
  98. if (array_key_exists(0, $chaines))
  99. {
  100. $type = $chaines[0];
  101. }
  102. }
  103. $this->element->setType($type);
  104. }
  105. /**
  106. * Construction des autres champs tel que embed.
  107. *
  108. *
  109. */
  110. public function proceedExtraFields()
  111. {
  112. // Instanciation d'un objet factory correspondant au type, par exemple
  113. // YoutubeFactory, qui répondant a une implementation retournera ces infos.
  114. if (in_array($this->element->getType(), $this->factories))
  115. {
  116. $site_factory = $this->getFactory();
  117. // On récupères les datas de l'élément
  118. $site_factory->retrieveDatas();
  119. // On procède a la construction de nos informations
  120. $site_factory->proceedEmbedCode();
  121. $site_factory->proceedThumbnailUrl();
  122. }
  123. }
  124. public function getFactory()
  125. {
  126. // $factory_name = ucfirst(str_replace('.', '', $this->element->getType())).'Factory';
  127. // return new $factory_name($this->element, $this->container);
  128. switch ($this->element->getType())
  129. {
  130. case 'youtube.com':
  131. return new Youtubecom($this->element, $this->container);
  132. break;
  133. case 'youtu.be':
  134. return new Youtube($this->element, $this->container);
  135. break;
  136. case 'soundcloud.com':
  137. return new Soundcloudcom($this->element, $this->container);
  138. break;
  139. case 'jamendo.com':
  140. return new Jamendocom($this->element, $this->container);
  141. break;
  142. case 'dailymotion.com':
  143. return new Dailymotioncom($this->element, $this->container);
  144. break;
  145. case 'deezer.com':
  146. return new Deezercom($this->element, $this->container);
  147. break;
  148. default:
  149. throw new \Exception("La Factory n'est pas prise en charge pour ce type.");
  150. break;
  151. }
  152. }
  153. }
  154. ?>