ElementFactory.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Muzich\CoreBundle\Factory;
  3. use Muzich\CoreBundle\Entity\Element;
  4. use Symfony\Component\DependencyInjection\Container;
  5. use Doctrine\ORM\EntityManager;
  6. use Muzich\CoreBundle\lib\Api\Connector as ApiConnector;
  7. use Muzich\CoreBundle\lib\Element\UrlAnalyzer;
  8. /**
  9. *
  10. * @author bux
  11. */
  12. abstract class ElementFactory
  13. {
  14. protected $element;
  15. protected $container;
  16. protected $entity_manager;
  17. protected $api_connector;
  18. protected $url_analyzer;
  19. protected $url_matchs = array();
  20. /**
  21. *
  22. * @param Element $element
  23. * @param Container $container
  24. */
  25. public function __construct(Element $element, Container $container, EntityManager $entity_manager)
  26. {
  27. $this->element = $element;
  28. $this->container = $container;
  29. $this->entity_manager = $entity_manager;
  30. $this->api_connector = new ApiConnector($element);
  31. $this->url_analyzer = new UrlAnalyzer($element, $this->url_matchs);
  32. }
  33. protected function getApiConnector()
  34. {
  35. return $this->api_connector;
  36. }
  37. public function setUrlDatas()
  38. {
  39. if ($this->url_analyzer->haveMatch())
  40. {
  41. $this->element->setData(Element::DATA_TYPE , $this->url_analyzer->getType());
  42. $this->element->setData(Element::DATA_REF_ID, $this->url_analyzer->getRefId());
  43. }
  44. }
  45. /**
  46. * Retourne l'url relative dans le site
  47. *
  48. * @return string
  49. */
  50. protected function getCleanedUrl($decode = false, $force_base_url = null)
  51. {
  52. // Procèdures de nettoyages après constat d'erreurs
  53. $url = $this->element->getUrl();
  54. if ($decode)
  55. {
  56. $url = urldecode($url);
  57. }
  58. $base_url = $this->element->getType();
  59. if ($force_base_url)
  60. {
  61. $base_url = $force_base_url;
  62. }
  63. $url = str_replace('www.', '', $url);
  64. $url = str_replace('http://'.$base_url, '', $url);
  65. $url = str_replace('https://'.$base_url, '', $url);
  66. return $url;
  67. }
  68. public function retrieveDatas()
  69. {
  70. $this->element->setDatas(array());
  71. }
  72. public function proceedEmbedCode()
  73. {
  74. $this->element->setEmbed(null);
  75. }
  76. public function proceedThumbnailUrl()
  77. {
  78. if (($thumb_url = $this->element->getData(Element::DATA_THUMB_URL)))
  79. {
  80. $this->element->setThumbnailUrl($thumb_url);
  81. }
  82. }
  83. protected function getJsonDataFromApiWithUrl($url)
  84. {
  85. $api_url = curl_init($url);
  86. $options = array(
  87. CURLOPT_RETURNTRANSFER => true,
  88. CURLOPT_HTTPHEADER => array('Content-type: application/json')
  89. );
  90. curl_setopt_array($api_url, $options);
  91. return json_decode(curl_exec($api_url), true);
  92. }
  93. }
  94. ?>