You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

114 lines
3.1 KiB

  1. <?php
  2. namespace App\Service;
  3. /*
  4. * Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. use App\Entity\Entry;
  22. use App\Entity\Feed;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use SimplePie;
  25. use SimplePie_Category;
  26. use SimplePie_Item;
  27. use Symfony\Component\String\Slugger\SluggerInterface;
  28. class FeedEntriesImporter
  29. {
  30. /**
  31. * @var FeedParserFactory
  32. */
  33. private $parserFactory;
  34. /**
  35. * @var EntityManager
  36. */
  37. private $entityManager;
  38. /**
  39. * @var SluggerInterface
  40. */
  41. private $slugger;
  42. public function __construct(
  43. FeedParserFactory $parserFactory,
  44. EntityManagerInterface $entityManager,
  45. SluggerInterface $slugger
  46. ) {
  47. $this->parserFactory = $parserFactory;
  48. $this->entityManager = $entityManager;
  49. $this->slugger = $slugger;
  50. }
  51. public function importEntries(Feed $feed): void
  52. {
  53. $parser = $this->parserFactory->getParser();
  54. $parser->set_feed_url($feed->getUrl());
  55. $parser->init();
  56. // TODO: Send event
  57. // dump($feed->getUrl());
  58. foreach ($parser->get_items() as $item) {
  59. // TODO: Send event
  60. // TODO: Check publishing date and skip if to old
  61. $this->importEntry($item, $feed);
  62. }
  63. $this->entityManager->flush();
  64. }
  65. private function importEntry(SimplePie_Item $item, Feed $feed): void
  66. {
  67. $entry = new Entry(
  68. $feed,
  69. $item->get_id(),
  70. $this->getDateTime($item->get_gmdate()),
  71. $item->get_title(),
  72. $this->slugger->slug($item->get_title()),
  73. $item->get_link() ?? '',
  74. $this->implodeCategories($item->get_categories()),
  75. $item->get_content(),
  76. );
  77. $this->entityManager->persist($entry);
  78. }
  79. private function implodeCategories(?array $categories = null): string
  80. {
  81. if ($categories === null) {
  82. return '';
  83. }
  84. return implode(',', array_map(function (SimplePie_Category $category) {
  85. return $category->get_term();
  86. }, $categories));
  87. }
  88. private function getDateTime($dateTime): \DateTimeImmutable
  89. {
  90. $dateTime = \DateTimeImmutable::createFromFormat(
  91. 'd M Y, h:i a',
  92. $dateTime,
  93. new \DateTimeZone('GMT')
  94. );
  95. return $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  96. }
  97. }