public function schedule()
 {
     $licenseRepo = $this->em->getRepository('AppBundle:License');
     $drillSchemaEventsRepo = $this->em->getRepository('AppBundle:DrillSchemaEvent');
     $licensesWithoutSchema = $licenseRepo->findWithoutRegisteredSchema();
     foreach ($licensesWithoutSchema as $license) {
         $drillSchemaEvents = $drillSchemaEventsRepo->findByAddonKey($license->getAddonKey());
         if (empty($drillSchemaEvents)) {
             continue;
         }
         $drillRegisteredSchema = new DrillRegisteredSchema();
         $drillRegisteredSchema->setLicenseId($license->getLicenseId());
         $drillRegisteredSchema->setAddonKey($license->getAddonKey());
         $this->em->persist($drillRegisteredSchema);
         foreach ($drillSchemaEvents as $drillSchemaEvent) {
             $sendDate = $this->calculateSendDate($drillSchemaEvent, $license);
             $today = new \DateTime();
             // prevent creating events from past
             if ($sendDate < $today->modify('-2 days')) {
                 continue;
             }
             $drillRegisteredEvent = new DrillRegisteredEvent();
             $drillRegisteredEvent->setDrillRegisteredSchema($drillRegisteredSchema);
             $drillRegisteredEvent->setDrillSchemaEvent($drillSchemaEvent);
             // Calculate
             $drillRegisteredEvent->setSendDate($sendDate);
             $drillRegisteredEvent->setStatus('new');
             $this->em->persist($drillRegisteredEvent);
         }
     }
     $this->em->flush();
 }
 /**
  * @Route("/cancel/{id}", name="drill_cancel")
  */
 public function cancelAction(DrillRegisteredSchema $drill)
 {
     $em = $this->getDoctrine()->getManager();
     foreach ($drill->getDrillRegisteredEvents() as $event) {
         if ('new' == $event->getStatus()) {
             $event->setStatus('canceled');
             $em->persist($event);
         }
     }
     $em->flush();
     return $this->redirectToRoute('license_detail', ['licenseId' => $drill->getLicenseId()]);
 }