ElementManager.php 4.1KB

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