ElementManager.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\YoutubeFactory;
  8. /**
  9. *
  10. *
  11. * @author bux
  12. */
  13. class ElementManager
  14. {
  15. protected $types = array(
  16. 'youtube.com',
  17. 'soundcloud.com',
  18. 'son2teuf.org',
  19. 'jamendo.com'
  20. );
  21. protected $em;
  22. protected $element;
  23. protected $container;
  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. public function __construct(Element $element, EntityManager $em, Container $container)
  39. {
  40. $this->element = $element;
  41. $this->em = $em;
  42. $this->container = $container;
  43. $evm = new \Doctrine\Common\EventManager();
  44. $timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
  45. $evm->addEventSubscriber($timestampableListener);
  46. $this->em->getEventManager()->addEventSubscriber($timestampableListener);
  47. }
  48. /**
  49. * Procédure chargé de construire le contenu de l'élément.
  50. * nom, Code d'embed, [...]
  51. *
  52. * @param Element $element
  53. * @param array $params
  54. * @param User $owner
  55. * @return Element
  56. */
  57. public function proceedFill($params, User $owner)
  58. {
  59. $this->element->setName($params['name']);
  60. $this->element->setUrl($params['url']);
  61. $this->element->setOwner($owner);
  62. $this->element->setTagsWithIds($this->em, $params['tags']);
  63. $this->determineType();
  64. $this->proceedExtraFields();
  65. }
  66. /**
  67. * Determine le type d'objet auquel on a affaire.
  68. */
  69. protected function determineType()
  70. {
  71. preg_match("/^(http:\/\/)?([^\/]+)/i", $this->element->getUrl(), $chaines);
  72. $host = $chaines[2];
  73. // Repérer les derniers segments
  74. preg_match("/[^\.\/]+\.[^\.\/]+$/",$host,$chaines);
  75. $type = null;
  76. if (in_array($chaines[0], $this->types))
  77. {
  78. $type = $this->em->getRepository('MuzichCoreBundle:ElementType')->find($chaines[0]);
  79. }
  80. $this->element->setType($type);
  81. }
  82. /**
  83. * Construction des autres champs tel que embed.
  84. *
  85. *
  86. */
  87. protected function proceedExtraFields()
  88. {
  89. // Instanciation d'un objet factory correspondant au type, par exemple
  90. // YoutubeFactory, qui répondant a une implementation retournera ces infos.
  91. if ($this->element->getType())
  92. {
  93. $site_factory = $this->getFactory();
  94. $this->element->setEmbed($site_factory->getEmbedCode());
  95. }
  96. }
  97. protected function getFactory()
  98. {
  99. switch ($this->element->getType()->getId())
  100. {
  101. case 'youtube.com':
  102. return new YoutubeFactory($this->element, $this->container);
  103. break;
  104. case 'soundcloud.com':
  105. return new SoundCloudFactory($this->element, $this->container);
  106. break;
  107. case 'son2teuf.org':
  108. return new Son2TeufFactory($this->element, $this->container);
  109. break;
  110. case 'jamendo.com':
  111. return new JamendoFactory($this->element, $this->container);
  112. break;
  113. default:
  114. throw new Exception("La Factory n'est pas connu pour ce type.");
  115. break;
  116. }
  117. }
  118. }
  119. ?>