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, $do_tags = true)
  65. {
  66. $this->element->setOwner($owner);
  67. if ($do_tags)
  68. {
  69. $this->element->setTagsWithIds($this->em, json_decode($this->element->getTags()));
  70. }
  71. $this->determineType();
  72. $this->proceedExtraFields();
  73. }
  74. protected function setGroup()
  75. {
  76. if ($this->element->getGroup())
  77. {
  78. $group = $this->em->getRepository('MuzichCoreBundle:Group')->findOneById($this->element->getGroup());
  79. $this->element->setGroup($group);
  80. }
  81. else
  82. {
  83. $this->element->setGroup(null);
  84. }
  85. }
  86. /**
  87. * Determine le type d'objet auquel on a affaire.
  88. */
  89. public function determineType()
  90. {
  91. // On ne prend pas de risque avec le www, on l'enlève
  92. $url = str_replace('www.', '', $this->element->getUrl());
  93. preg_match("/^(http:\/\/|https:\/\/)?([^\/]+)/i", $url, $chaines);
  94. $type = 'unknow';
  95. if (array_key_exists(2, $chaines))
  96. {
  97. $host = $chaines[2];
  98. // Repérer les derniers segments
  99. preg_match("/[^\.\/]+\.[^\.\/]+$/",$host,$chaines);
  100. if (array_key_exists(0, $chaines))
  101. {
  102. $type = $chaines[0];
  103. }
  104. }
  105. $this->element->setType($type);
  106. }
  107. /**
  108. * Construction des autres champs tel que embed.
  109. *
  110. *
  111. */
  112. public function proceedExtraFields()
  113. {
  114. // Instanciation d'un objet factory correspondant au type, par exemple
  115. // YoutubeFactory, qui répondant a une implementation retournera ces infos.
  116. if (in_array($this->element->getType(), $this->factories))
  117. {
  118. $site_factory = $this->getFactory();
  119. // On récupères les datas de l'élément
  120. $site_factory->retrieveDatas();
  121. // On procède a la construction de nos informations
  122. $site_factory->proceedEmbedCode();
  123. $site_factory->proceedThumbnailUrl();
  124. }
  125. }
  126. public function getFactory()
  127. {
  128. // $factory_name = ucfirst(str_replace('.', '', $this->element->getType())).'Factory';
  129. // return new $factory_name($this->element, $this->container);
  130. switch ($this->element->getType())
  131. {
  132. case 'youtube.com':
  133. return new Youtubecom($this->element, $this->container, $this->em);
  134. break;
  135. case 'youtu.be':
  136. return new Youtube($this->element, $this->container, $this->em);
  137. break;
  138. case 'soundcloud.com':
  139. return new Soundcloudcom($this->element, $this->container, $this->em);
  140. break;
  141. case 'jamendo.com':
  142. return new Jamendocom($this->element, $this->container, $this->em);
  143. break;
  144. case 'dailymotion.com':
  145. return new Dailymotioncom($this->element, $this->container, $this->em);
  146. break;
  147. case 'deezer.com':
  148. return new Deezercom($this->element, $this->container, $this->em);
  149. break;
  150. case 'vimeo.com':
  151. return new Vimeocom($this->element, $this->container, $this->em);
  152. break;
  153. case 'spotify.com':
  154. return new Spotifycom($this->element, $this->container, $this->em);
  155. break;
  156. default:
  157. throw new \Exception("La Factory n'est pas prise en charge pour ce type.");
  158. break;
  159. }
  160. }
  161. public function regenerate()
  162. {
  163. $this->determineType();
  164. $this->proceedExtraFields();
  165. }
  166. }
  167. ?>