Skip to content

Commit

Permalink
Merge branch 'second-half-feature'
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Jul 25, 2020
2 parents 26c0219 + 5dc45be commit 96dd282
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 59 deletions.
7 changes: 5 additions & 2 deletions src/Application/Handler/CreateMatchesForSeasonHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use HexagonalPlayground\Application\Permission\IsAdmin;
use HexagonalPlayground\Application\Repository\SeasonRepositoryInterface;
use HexagonalPlayground\Application\Security\AuthContext;
use HexagonalPlayground\Domain\MatchFactory;
use HexagonalPlayground\Domain\MatchDayGenerator;
use HexagonalPlayground\Domain\Season;

class CreateMatchesForSeasonHandler implements AuthAwareHandler
Expand Down Expand Up @@ -37,7 +37,10 @@ public function __invoke(CreateMatchesForSeasonCommand $command, AuthContext $au
/** @var Season $season */
$season = $this->seasonRepository->find($command->getSeasonId());
$season->clearMatchDays();
(new MatchFactory())->createMatchDaysForSeason($season, $command->getMatchDaysDates());

$generator = new MatchDayGenerator();
$generator->generateMatchDaysForSeason($season, $command->getMatchDaysDates());

$this->seasonRepository->save($season);
}
}
20 changes: 5 additions & 15 deletions src/Domain/MatchDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ public function clearMatches(): void
$this->matches->clear();
}

/**
* @return Competition
*/
public function getCompetition(): Competition
{
return $this->season ?? $this->tournament;
}

/**
* @param DateTimeImmutable $startDate
* @param DateTimeImmutable $endDate
Expand Down Expand Up @@ -138,9 +130,8 @@ public function getEndDate(): DateTimeImmutable
*/
public function addResult(string $homeTeamId, string $guestTeamId, MatchResult $matchResult): void
{
$competition = $this->getCompetition();
if ($competition instanceof Season) {
$competition->addResult($homeTeamId, $guestTeamId, $matchResult);
if ($this->season !== null) {
$this->season->getRanking()->addResult($homeTeamId, $guestTeamId, $matchResult);
}
}

Expand All @@ -151,9 +142,8 @@ public function addResult(string $homeTeamId, string $guestTeamId, MatchResult $
*/
public function revertResult(string $homeTeamId, string $guestTeamId, MatchResult $matchResult): void
{
$competition = $this->getCompetition();
if ($competition instanceof Season) {
$competition->revertResult($homeTeamId, $guestTeamId, $matchResult);
if ($this->season !== null) {
$this->season->getRanking()->revertResult($homeTeamId, $guestTeamId, $matchResult);
}
}

Expand Down Expand Up @@ -181,4 +171,4 @@ private function setCompetition(Competition $competition): void
$this->tournament = $competition;
}
}
}
}
47 changes: 36 additions & 11 deletions src/Domain/MatchFactory.php → src/Domain/MatchDayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use HexagonalPlayground\Domain\Util\Assert;

/**
* A factory which constructs Match objects and implements a match day generation algorithm
* Generates MatchDays and Matches for a Season
*/
class MatchFactory
class MatchDayGenerator
{
/**
* Create all matches for a given season
* Generates all MatchDays for a Season
*
* @param Season $season
* @param array|DatePeriod[] $matchDayDates
*/
public function createMatchDaysForSeason(Season $season, array $matchDayDates): void
public function generateMatchDaysForSeason(Season $season, array $matchDayDates): void
{
$teams = array_values($season->getTeams());
Assert::true(count($teams) >= 2, 'Cannot create matches for season with less than 2 teams');
Expand All @@ -26,20 +26,45 @@ public function createMatchDaysForSeason(Season $season, array $matchDayDates):
$teams[] = null;
}
shuffle($teams);
$expectedMatchDayCount = count($teams) - 1;

Assert::true(count($matchDayDates) === $expectedMatchDayCount, sprintf(
'Count of MatchDay dates does not match. Expected %d. Got %d',
$expectedMatchDayCount,
count($matchDayDates)
));
$matchDaysPerHalf = count($teams) - 1;
$possibleMatchDayCounts = [$matchDaysPerHalf, $matchDaysPerHalf * 2];

Assert::oneOf(
count($matchDayDates),
$possibleMatchDayCounts,
'Count of MatchDay dates does not match. Expected: [%s]. Got: %s',
);

/** @var DatePeriod[] $secondHalfMatchDayDates */
$secondHalfMatchDayDates = [];
if (count($matchDayDates) > $matchDaysPerHalf) {
$secondHalfMatchDayDates = array_splice($matchDayDates, $matchDaysPerHalf, $matchDaysPerHalf);
}

$matchDayNumber = 1;
foreach ($matchDayDates as $datePeriod) {
$matchDay = $season->createMatchDay(null, $matchDayNumber, $datePeriod->getStartDate(), $datePeriod->getEndDate());
$this->generateMatchesForMatchDay($matchDay, $teams);
$matchDayNumber++;
}

if (!empty($secondHalfMatchDayDates)) {
$i = 0;
foreach ($season->getMatchDays() as $firstHalfMatchDay) {
$secondHalfMatchDay = $season->createMatchDay(
null,
$matchDayNumber,
$secondHalfMatchDayDates[$i]->getStartDate(),
$secondHalfMatchDayDates[$i]->getEndDate()
);
foreach ($firstHalfMatchDay->getMatches() as $match) {
$secondHalfMatchDay->createMatch(null, $match->getGuestTeam(), $match->getHomeTeam());
}
$i++;
$matchDayNumber++;
}
}
}

/**
Expand Down Expand Up @@ -84,4 +109,4 @@ private function generateMatchesForMatchDay(MatchDay $matchDay, array $teams) :
$matchDay->createMatch(null, $homeTeam, $guestTeam);
}
}
}
}
29 changes: 0 additions & 29 deletions src/Domain/Season.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use HexagonalPlayground\Domain\Event\Event;
use HexagonalPlayground\Domain\Event\Publisher;
use HexagonalPlayground\Domain\Util\Assert;
use HexagonalPlayground\Domain\Value\MatchResult;

class Season extends Competition
{
Expand Down Expand Up @@ -117,14 +116,6 @@ private function hasStarted() : bool
return ($this->ranking !== null);
}

/**
* @return bool
*/
public function hasEnded() : bool
{
return $this->state === self::STATE_ENDED;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -203,26 +194,6 @@ public function getMatchDays(): array
return $this->matchDays->toArray();
}

/**
* @param string $homeTeamId
* @param string $guestTeamId
* @param MatchResult $matchResult
*/
public function addResult(string $homeTeamId, string $guestTeamId, MatchResult $matchResult): void
{
$this->getRanking()->addResult($homeTeamId, $guestTeamId, $matchResult);
}

/**
* @param string $homeTeamId
* @param string $guestTeamId
* @param MatchResult $matchResult
*/
public function revertResult(string $homeTeamId, string $guestTeamId, MatchResult $matchResult): void
{
$this->getRanking()->revertResult($homeTeamId, $guestTeamId, $matchResult);
}

private function updateMatchDayCount(): void
{
$this->matchDayCount = $this->matchDays->count();
Expand Down
4 changes: 2 additions & 2 deletions tests/GraphQL/SeasonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testSeasonCanBeStarted(string $seasonId): string
$this->client->addTeamToSeason($seasonId, $teamId);
}

$dates = self::createMatchDayDates(count(self::$teamIds) - 1);
$dates = self::createMatchDayDates(2 * (count(self::$teamIds) - 1));
$this->client->createMatchesForSeason($seasonId, $dates);

$events = EventCapturer::getInstance()->capture(function() use ($seasonId) {
Expand Down Expand Up @@ -380,4 +380,4 @@ private function getNonParticipatingTeamIds(stdClass $match): array
$match->guest_team->id
]);
}
}
}

0 comments on commit 96dd282

Please sign in to comment.