public function storeAction(Request $request)
 {
     $this->assertUserRights(UserRole::ROLE_ADMIN);
     $em = $this->getDoctrine()->getManager();
     // begin handle integration config
     $currentConfig = $this->getDoctrine()->getRepository('KoalamonIntegrationBundle:IntegrationConfig')->findOneBy(['project' => $this->getProject(), 'integration' => $this->getIntegrationIdentifier()]);
     $newConfig = IntegrationConfig::createFromRequest($request);
     $newConfig->setIntegration($this->getIntegrationIdentifier());
     $newConfig->setProject($this->getProject());
     if ($currentConfig) {
         if ($newConfig->equals($currentConfig)) {
             $globalConfigChanged = false;
         } else {
             $globalConfigChanged = true;
             $currentConfig->setOptions($newConfig->getOptions());
             $currentConfig->setStatus($newConfig->getStatus());
             $em->persist($currentConfig);
         }
     } else {
         $globalConfigChanged = true;
         $currentConfig = $newConfig;
         $em->persist($currentConfig);
     }
     // end handle integration config
     // begin handle integration systems
     $integrationSystems = $this->getDoctrine()->getRepository('KoalamonIntegrationBundle:IntegrationSystem')->findBy(['project' => $this->getProject(), 'integration' => $this->getIntegrationIdentifier()]);
     /** @var IntegrationSystem[] $integrationSystems */
     $oldIntegrationSystemArray = array();
     /** @var IntegrationSystem[] $oldIntegrationSystemArray */
     foreach ($integrationSystems as $integrationSystem) {
         if ($integrationSystem->getSystem()->isActive()) {
             $oldIntegrationSystemArray[$integrationSystem->getSystem()->getId()] = $integrationSystem;
         }
     }
     foreach ((array) $request->get('systems') as $systemId => $system) {
         if (array_key_exists('enabled', $system)) {
             $newIntegrationSystem = $this->createIntegrationSystem($systemId, $system, $currentConfig);
             // if old configuration for system exists
             if (array_key_exists($systemId, $oldIntegrationSystemArray)) {
                 // if old and new are equal set change flag to false
                 if ($newIntegrationSystem->equals($oldIntegrationSystemArray[$systemId])) {
                     $changed = false;
                     unset($oldIntegrationSystemArray[$systemId]);
                 } else {
                     $changed = true;
                 }
             } else {
                 $changed = true;
             }
             if ($changed || $globalConfigChanged && $newIntegrationSystem->getInherit()) {
                 if ($changed) {
                     $em->persist($newIntegrationSystem);
                 }
                 $this->dispatchEvent($newIntegrationSystem);
             }
         }
     }
     foreach ($oldIntegrationSystemArray as $oldIntegrationSystem) {
         $em->remove($oldIntegrationSystem);
     }
     $em->flush();
     return new JsonResponse(['status' => 'success', 'message' => 'Configuration stored.']);
 }
 public function equals(IntegrationConfig $integrationSystem)
 {
     if ($this->getOptions() != $integrationSystem->getOptions()) {
         return false;
     }
     if ($this->getIntegration() != $integrationSystem->getIntegration()) {
         return false;
     }
     return true;
 }