public function testEvent() { /* @var $item \PHPUnit_Framework_MockObject_MockObject|Item */ $item = $this->getMock('\\AnimeDb\\Bundle\\CatalogBundle\\Entity\\Item'); /* @var $filler1 \PHPUnit_Framework_MockObject_MockObject|FillerInterface */ $filler1 = $this->getMock('\\AnimeDb\\Bundle\\CatalogBundle\\Plugin\\Fill\\Filler\\FillerInterface'); /* @var $filler2 \PHPUnit_Framework_MockObject_MockObject|FillerInterface */ $filler2 = $this->getMock('\\AnimeDb\\Bundle\\CatalogBundle\\Plugin\\Fill\\Filler\\FillerInterface'); $event = new AddNewItem($item, $filler1); $this->assertEquals($item, $event->getItem()); $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $event->getFillers()); $this->assertEquals([$filler1], $event->getFillers()->toArray()); $event->addFiller($filler2); $this->assertEquals([$filler1, $filler2], $event->getFillers()->toArray()); }
/** * @param AddNewItem $event */ public function onAddNewItem(AddNewItem $event) { $item = $event->getItem(); if (!$event->getFillers()->contains($this->filler) && ($url = $this->refiller->getSourceForFill($item)) && preg_match(Filler::REG_ITEM_ID, $url, $match)) { try { // get data $body = $this->browser->get('anime', ['aid' => $match['id']]); } catch (\Exception $e) { return; } // fill item if (!$item->getDateEnd()) { $item->setDateEnd(new \DateTime($body->filter('enddate')->text())); } if (!$item->getDatePremiere()) { $item->setDatePremiere(new \DateTime($body->filter('startdate')->text())); } if (!$item->getEpisodes()) { $this->filler->setEpisodes($item, $body); } if (!$item->getEpisodesNumber()) { $item->setEpisodesNumber($body->filter('episodecount')->text()); } if (!$item->getSummary()) { $item->setSummary($this->cleaner->clean($body->filter('description')->text())); } if (!$item->getType()) { $this->filler->setType($item, $body); } if (!$item->getCover()) { $this->filler->setCover($item, $body, $match['id']); } $this->filler->setGenres($item, $body); // copy main and other names $new_item = $this->filler->setNames(new Item(), $body); // set main name in top of names list $new_names = $new_item->getNames()->toArray(); array_unshift($new_names, (new Name())->setName($new_item->getName())); /* @var $new_name Name */ foreach ($new_names as $new_name) { $item->addName($new_name->setItem(null)); } $event->addFiller($this->filler); // resend event $this->dispatcher->dispatch(StoreEvents::ADD_NEW_ITEM, clone $event); $event->stopPropagation(); } }
/** * @param AddNewItem $event */ public function onAddNewItem(AddNewItem $event) { $item = $event->getItem(); if (!$event->getFillers()->contains($this->filler) && ($url = $this->refiller->getSourceForFill($item))) { // get data preg_match(Filler::REG_ITEM_ID, $url, $match); $path = str_replace('#ID#', $match['id'], Filler::FILL_URL); $body = $this->browser->get($path); // fill item if (!$item->getDateEnd() && $body['released_on']) { $item->setDateEnd(new \DateTime($body['released_on'])); } if (!$item->getDatePremiere()) { $item->setDatePremiere(new \DateTime($body['aired_on'])); } if (!$item->getDuration()) { $item->setDuration($body['duration']); } if (!$item->getEpisodesNumber()) { $ep_num = $body['episodes_aired'] ? $body['episodes_aired'] : $body['episodes']; $item->setEpisodesNumber($ep_num . ($body['ongoing'] ? '+' : '')); } if (!$item->getSummary()) { $item->setSummary($body['description']); } // set complex data if (!$item->getStudio()) { $this->filler->setStudio($item, $body); } if (!$item->getType()) { $this->filler->setType($item, $body); } if (!$item->getCover()) { $this->filler->setCover($item, $body); } $this->filler->setGenres($item, $body); $this->filler->setSources($item, $body); $this->filler->setNames($item, $body); $event->addFiller($this->filler); // resend event $this->dispatcher->dispatch(StoreEvents::ADD_NEW_ITEM, clone $event); $event->stopPropagation(); } }
/** * @param AddNewItem $event */ public function onAddNewItemPersistIt(AddNewItem $event) { $this->em->persist($event->getItem()); $this->em->flush(); }