ElementManager.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Muzich\CoreBundle\ElementFactory;
  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\ElementFactory\Site\YoutubecomFactory;
  8. use Muzich\CoreBundle\ElementFactory\Site\YoutubeFactory;
  9. use Muzich\CoreBundle\ElementFactory\Site\DailymotioncomFactory;
  10. use Muzich\CoreBundle\ElementFactory\Site\JamendocomFactory;
  11. use Muzich\CoreBundle\ElementFactory\Site\SoundcloudcomFactory;
  12. use Muzich\CoreBundle\ElementFactory\Site\DeezercomFactory;
  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)
  63. {
  64. $this->element->setOwner($owner);
  65. $this->element->setTagsWithIds($this->em, json_decode($this->element->getTags()));
  66. $this->determineType();
  67. $this->proceedExtraFields();
  68. }
  69. protected function setGroup()
  70. {
  71. if ($this->element->getGroup())
  72. {
  73. $group = $this->em->getRepository('MuzichCoreBundle:Group')->findOneById($this->element->getGroup());
  74. $this->element->setGroup($group);
  75. }
  76. else
  77. {
  78. $this->element->setGroup(null);
  79. }
  80. }
  81. /**
  82. * Determine le type d'objet auquel on a affaire.
  83. */
  84. protected function determineType()
  85. {
  86. // On ne prend pas de risque avec le www, on l'enlève
  87. $url = str_replace('www.', '', $this->element->getUrl());
  88. preg_match("/^(http:\/\/)?([^\/]+)/i", $url, $chaines);
  89. $type = 'unknow';
  90. if (array_key_exists(2, $chaines))
  91. {
  92. $host = $chaines[2];
  93. // Repérer les derniers segments
  94. preg_match("/[^\.\/]+\.[^\.\/]+$/",$host,$chaines);
  95. if (array_key_exists(0, $chaines))
  96. {
  97. $type = $chaines[0];
  98. }
  99. }
  100. $this->element->setType($type);
  101. }
  102. /**
  103. * Construction des autres champs tel que embed.
  104. *
  105. *
  106. */
  107. public function proceedExtraFields()
  108. {
  109. // Instanciation d'un objet factory correspondant au type, par exemple
  110. // YoutubeFactory, qui répondant a une implementation retournera ces infos.
  111. if (in_array($this->element->getType(), $this->factories))
  112. {
  113. $site_factory = $this->getFactory();
  114. $this->element->setEmbed($site_factory->getEmbedCode());
  115. $this->element->setThumbnailUrl($site_factory->getThumbnailUrl());
  116. }
  117. }
  118. protected function getFactory()
  119. {
  120. // $factory_name = ucfirst(str_replace('.', '', $this->element->getType())).'Factory';
  121. // return new $factory_name($this->element, $this->container);
  122. switch ($this->element->getType())
  123. {
  124. case 'youtube.com':
  125. return new YoutubecomFactory($this->element, $this->container);
  126. break;
  127. case 'youtu.be':
  128. return new YoutubeFactory($this->element, $this->container);
  129. break;
  130. case 'soundcloud.com':
  131. return new SoundcloudcomFactory($this->element, $this->container);
  132. break;
  133. case 'jamendo.com':
  134. return new JamendocomFactory($this->element, $this->container);
  135. break;
  136. case 'dailymotion.com':
  137. return new DailymotioncomFactory($this->element, $this->container);
  138. break;
  139. case 'deezer.com':
  140. return new DeezercomFactory($this->element, $this->container);
  141. break;
  142. default:
  143. throw new \Exception("La Factory n'est pas prise en charge pour ce type.");
  144. break;
  145. }
  146. }
  147. }
  148. ?>