Skip to content

Commit

Permalink
Handle duplicate wikis on event creation
Browse files Browse the repository at this point in the history
  • Loading branch information
MusikAnimal committed Apr 30, 2018
1 parent 00bc3fb commit 2f4f161
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/AppBundle/Controller/EventController.php
Expand Up @@ -319,7 +319,9 @@ function ($wikiObjects) {
}, $wikis);
},
function ($wikis) use ($event, $eventWikiRepo) {
return $this->normalizeEventWikis($wikis, $event, $eventWikiRepo);
return array_filter(
$this->normalizeEventWikis($wikis, $event, $eventWikiRepo)
);
}
);
}
Expand All @@ -337,6 +339,11 @@ private function normalizeEventWikis($wikis, Event $event, EventWikiRepository $
{
return array_map(function ($wiki) use ($event, $eventWikiRepo) {
$domain = $eventWikiRepo->getDomainFromEventWikiInput($wiki);

if ($event->hasWikiWithDomain($domain)) {
return null;
}

$eventWiki = $eventWikiRepo->findOneBy([
'event' => $event,
'domain' => $domain,
Expand Down
12 changes: 12 additions & 0 deletions src/AppBundle/Model/Event.php
Expand Up @@ -549,6 +549,18 @@ public function removeWiki(EventWiki $wiki)
$this->wikis->removeElement($wiki);
}

/**
* Does the Event have a wiki with the given domain?
* @param string $domain Must be the normalized form, e.g. en.wikipedia, *.wikipedia
* @return bool
*/
public function hasWikiWithDomain($domain)
{
return $this->wikis->filter(function ($wiki) use ($domain) {
return $domain === $wiki->getDomain();
})->count() > 0;
}

/***************
* WIKI FAMILY *
***************/
Expand Down
2 changes: 2 additions & 0 deletions tests/AppBundle/Model/EventTest.php
Expand Up @@ -191,6 +191,7 @@ public function testAddRemoveWiki()
$event->addWiki($wiki);

$this->assertEquals($wiki, $event->getWikis()[0]);
$this->assertTrue($event->hasWikiWithDomain('testwiki'));

// Try adding the same one, which shouldn't duplicate.
$event->addWiki($wiki);
Expand All @@ -199,6 +200,7 @@ public function testAddRemoveWiki()
// Removing the wiki.
$event->removeWiki($wiki);
$this->assertEquals(0, count($event->getWikis()));
$this->assertFalse($event->hasWikiWithDomain('testwiki'));

// Double-remove shouldn't error out.
$event->removeWiki($wiki);
Expand Down

0 comments on commit 2f4f161

Please sign in to comment.