public function setTime(Registration $registration, $timestamp, $checkpoint, LoggerInterface $logger = null) { if (is_null($checkpoint) || false == trim($checkpoint)) { // PHP evaluates an empty string to false throw new \InvalidArgumentException('Checkpoint must not be empty!'); } elseif (is_null($timestamp) || false == trim($timestamp)) { // PHP evaluates an empty string to false throw new \InvalidArgumentException('Time must not be empty!'); } /** @var EntityManager $em */ $em = $this->getEntityManager(); $checkpoint = trim($checkpoint); // expected format of the timestamp is unix timestamp plus microseconds $dtime = \DateTime::createFromFormat("U.u", $timestamp); // check validity of the given timestamp // if parsing did not work, the method returns false (else object) if ($dtime === false || false === $dtime->getTimestamp()) { throw new \InvalidArgumentException('Time parameter is not a valid timestamp!'); } // check if the checkpoint does not exist for this registration /** @var Timing $t */ foreach ($registration->getTimings() as $t) { if ($t->getCheckpoint() == $checkpoint) { throw new \InvalidArgumentException('There is already a timing for this checkpoint!'); } } // validate if setting a checkpoint makes sense if ($checkpoint == Registration::CHECKPOINT_START) { if (!$registration->isCheckedIn()) { throw new \InvalidArgumentException($registration->getId() . ': ' . "Competitors on lane {$registration->getLane()} are not checked in for starting!"); } } else { // some other checkpoint after the starting line if (!$registration->isStarted()) { throw new \InvalidArgumentException('Competitors are not on track!'); } } // store time and checkpoint in database $timing = new Timing(); $timing->setRegistration($registration)->setTime($dtime)->setCheckpoint($checkpoint); $em->persist($timing); if (!is_null($logger)) { $logger->debug("stored {$registration->getId()} with timestamp {$timestamp} for checkpoint {$checkpoint}"); } if ($checkpoint == Registration::CHECKPOINT_START) { $registration->setStarted(); $em->persist($registration); } elseif ($checkpoint == Registration::CHECKPOINT_FINISH) { $registration->setFinished(); $em->persist($registration); } $em->flush(); }
private function canBeMovedDown(Registration $registration) { $result = false; if ($registration->isValidForRace()) { $maxStarters = $registration->getSection()->getRace()->getMaxStarterPerSection(); if ($registration->getLane() < $maxStarters) { $result = true; } else { // last lane or even higher number than allowed $next = $this->getNextPossibleSection($registration, function ($x, $y) { return $x > $y; }); if (!is_null($next)) { $result = true; } } } return $result; }