public function insertUpdateProcessing($em, $passwordEncoder, $data, $id)
 {
     $update = !is_null($id);
     try {
         $em->beginTransaction();
         if ($update) {
             $user = $em->find('Model\\User', $id);
         } else {
             $user = new User();
         }
         $user->setUsername($data['username']);
         //Regenerate salt and update password only if a new password is provided
         if (isset($data['password']) && strlen($data['password']) > 8 && strlen($data['password']) < 1024) {
             //Prepare salt
             //                $salt = base64_encode(mcrypt_create_iv(ceil(0.75 * 32), MCRYPT_DEV_URANDOM));
             $salt = uniqid("", true);
             $user->setSalt($salt);
             //Encrypt password
             $password = $passwordEncoder->encodePassword($data['password'], $user->getSalt());
             $user->setPassword($password);
         }
         $user->setName($data['name']);
         $user->setSurname($data['surname']);
         $user->setEmail($data['email']);
         $user->setPhone($data['phone']);
         $user->setAddress($data['address']);
         $user->setCap($data['cap']);
         $user->setCity($data['city']);
         $user->setProvince($data['province']);
         $user->setCountry($data['country']);
         //Prepare roles string
         $roles = array();
         if (isset($data['roles'])) {
             foreach ($data['roles'] as $roleString => $val) {
                 $roles[] = new Role($roleString);
             }
         }
         $user->setRoles($roles);
         //Prepare accountExpiration DateTime object
         $accountExpiration = new \DateTime($data['accountExpiration'], new \DateTimeZone('Europe/Rome'));
         $user->setAccountExpiration($accountExpiration);
         $enabled = isset($data['enabled']);
         $user->setEnabled($enabled);
         //Prepare credentialsExpiration DateTime object
         $credentialsExpiration = new \DateTime($data['credentialsExpiration'], new \DateTimeZone('Europe/Rome'));
         $user->setCredentialsExpiration($credentialsExpiration);
         if ($update) {
             $em->merge($user);
         } else {
             $em->persist($user);
         }
         $em->flush();
         $em->commit();
     } catch (\Exception $e) {
         $em->rollback();
         throw $e;
     }
     return $user->getId();
 }