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(); }