ElementManager.php 5.0KB

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