|
<?php |
|
/** |
|
* @throws Exception |
|
*/ |
|
public function __invoke() : void |
|
{ |
|
// get parsing urls |
|
$batchSize = 25; |
|
/** @var ParseUrl $url */ |
|
foreach ($urls as $url) { |
|
$this->parseUrlContent($url); |
|
if (0 === $this->totalParsed % $batchSize) { |
|
$this->parseUrlRepository->flushWithClear(); |
|
} |
|
} |
|
$this->parseUrlRepository->flushWithClear(); |
|
} |
|
|
|
/** |
|
* @param ParseUrl $url |
|
* |
|
* @throws EntityIsNotAnimeTagException |
|
* @throws Exception |
|
*/ |
|
private function parseUrlContent(ParseUrl $url) : void |
|
{ |
|
$tagsCollection = $this->createTags($this->parseHtmlPage($url)); |
|
$this->createEpisodeWithDetails($anime, $tagsCollection, $parsedPageData); |
|
$this->totalParsed++; |
|
} |
|
|
|
/** |
|
* @param Anime $anime |
|
* @param AnimeTagCollection $animeTagCollection |
|
* @param array $parsedPageData |
|
* |
|
* @throws EntityIsNotAnimeTagException |
|
* @throws Exception |
|
*/ |
|
private function createEpisodeWithDetails( |
|
Anime $anime, |
|
AnimeTagCollection $animeTagCollection, |
|
array $parsedPageData |
|
) : void { |
|
// collect data from parsedPageData, required data to create entities |
|
try { |
|
$episode = $this->episodeRepository->single($episodeFilter); |
|
} catch (EpisodeNotFoundException $exception) { |
|
/** @var Episode $lastEpisode */ |
|
$episode = $this->episodeFactory->create('some_data'); |
|
if (false !== $this->getPersistedEntityFromUnitOfWork(Episode::class, 'name', $episode->name()->value())) { |
|
$episode = $this->getPersistedEntityFromUnitOfWork(Episode::class, 'slug', $episode->name()->value()); |
|
} |
|
} |
|
|
|
foreach ($animeTagCollection as $tag) { |
|
$details->addTag($tag); |
|
} |
|
$this->episodeRepository->persist($episode); |
|
} |
|
|
|
/** |
|
* @param array $parsedPageData |
|
* |
|
* @return AnimeTagCollection |
|
* @throws EntityIsNotAnimeTagException |
|
* @throws Exception |
|
*/ |
|
private function createTags(array $parsedPageData) : AnimeTagCollection |
|
{ |
|
$tags = ['some_tags', 1,2,3,4]; |
|
$tagsCollection = new AnimeTagCollection(); |
|
foreach ($tags as $tagData) { |
|
try { |
|
$tag = $this->animeTagRepository->single($tagFilter); |
|
} catch (AnimeTagNotFoundException $exception) { |
|
$tag = $this->animeTagFactory->create(\ucfirst($tagName)); |
|
if (false !== $this->getPersistedEntityFromUnitOfWork(AnimeTag::class, 'slug', $slug)) { |
|
$tag = $this->getPersistedEntityFromUnitOfWork(AnimeTag::class, 'slug', $slug); |
|
} |
|
$this->animeTagRepository->persist($tag); |
|
} |
|
$tagsCollection->add($tag); |
|
} |
|
|
|
return $tagsCollection; |
|
} |
|
|
|
/** |
|
* @param string $class |
|
* @param string $methodName |
|
* @param string $value |
|
* |
|
* @return mixed |
|
*/ |
|
private function getPersistedEntityFromUnitOfWork(string $class, string $methodName, string $value) |
|
{ |
|
foreach ($this->entityManager->getUnitOfWork()->getScheduledEntityInsertions() as $scheduledEntityInsertion) { |
|
if ( |
|
$class !== \get_class($scheduledEntityInsertion) || |
|
$value !== $scheduledEntityInsertion->$methodName()->value() |
|
) { |
|
continue; |
|
} |
|
|
|
return $scheduledEntityInsertion; |
|
} |
|
|
|
return false; |
|
} |