public function setForLine(Line $line, $endpoint, $times, $key = null) { $qb = $this->getQueryBuilder(self::SUBSCRIPTION_ENTITY); $lineEntity = $this->entityManager->find('TubeService:Line', $line->getId()); // start a transaction $this->entityManager->getConnection()->beginTransaction(); try { // set all previous subscriptions for this line and endpoint to inactive $q = $qb->update()->set(self::TBL . '.is_active', '0')->where(self::TBL . '.line = ?1', self::TBL . '.endpoint = ?2')->setParameter(1, $line->getId()->getId())->setParameter(2, $endpoint)->getQuery(); $q->execute(); // create the domain entities for each time block foreach ($times as $day => $groups) { foreach ($groups as $group) { $subscription = new Subscription(); $subscription->setLine($lineEntity); $subscription->setDay($day); $subscription->setEndpoint($endpoint); $subscription->setPublicKey($key); $subscription->setStartHour($group->start); $subscription->setEndHour($group->end); $this->entityManager->persist($subscription); } } // commit the transaction $this->entityManager->flush(); $this->entityManager->getConnection()->commit(); return true; } catch (Exception $e) { $this->entityManager->getConnection()->rollback(); throw $e; } }
public function getDomainModel(\TubeService\Data\Database\Entity\Subscription $item) : Subscription { $id = new ID($item->getID()); $line = null; $lineEntity = $item->getLine(); if ($lineEntity) { $line = $this->mapperFactory->getDomainModel($lineEntity); } $subscription = new Subscription($id, $item->getCreatedAt(), $item->getUpdatedAt(), $item->getEndpoint(), $item->getDay(), $item->getStartHour(), $item->getEndHour(), $item->getIsActive(), $item->getPublicKey(), $line); return $subscription; }