/**
  * @Route("/login", name="api_login")
  * @Method("POST")
  * @param Request $request
  * @return JsonResponse
  */
 public function loginAction(Request $request)
 {
     $userDTO = new ApiUserLogin();
     $form = $this->createForm(new LoginApiType(), $userDTO);
     $this->handleJsonForm($form, $request);
     $redmineClient = $this->get('redmine.guzzle_client');
     try {
         $redmineResponse = $redmineClient->redmineLogin($userDTO->getUsername(), $userDTO->getPassword());
         $q = json_decode($redmineResponse);
         /** @var EntityManager $em */
         $em = $this->getDoctrine()->getManager();
         $user = $em->getRepository("RedmineAppBundle:RedmineUser")->findOneBy(['redmineToken' => $q->user->api_key, 'redmineUserID' => $q->user->id]);
         if (!$user) {
             $passwordEncoder = $this->get('security.password_encoder');
             $user = new RedmineUser();
             $user->setUsername($q->user->login)->setEmail($q->user->mail)->setPassword($passwordEncoder->encodePassword($user, md5(uniqid())))->setName($q->user->firstname)->setSurname($q->user->lastname)->setRedmineUserID($q->user->id)->setRedmineToken($q->user->api_key);
             $settings = new Settings();
             $settings->setSms(false)->setPush(false)->setCheckFirst(Carbon::createFromTime(17, 45))->setCheckSecond(Carbon::createFromTime(20, 0))->setCheckThird(Carbon::createFromTime(9, 30))->setUser($user);
             $em->persist($user);
             $em->persist($settings);
             $em->flush();
         }
     } catch (\Exception $e) {
         return new JsonResponse(['message' => 'Redmine user: bad credentials'], 403);
     }
     $this->get('redmine.device.notification')->getDevice($user, $userDTO->getDeviceId(), $userDTO->getPushToken(), $userDTO->getPushPlatform());
     return new JsonResponse($user);
 }
 public function authUserFromRedmine(UsernamePasswordToken $token)
 {
     $pass = $token->getCredentials();
     $username = $token->getUser();
     try {
         $response = $this->client->redmineLogin($username, $pass);
         $q = json_decode($response);
         $em = $this->em;
         $user = $em->getRepository("RedmineAppBundle:RedmineUser")->findOneBy(['redmineToken' => $q->user->api_key, 'redmineUserID' => $q->user->id]);
         if ($user) {
             return $user->getUsername();
         } else {
             $user = new RedmineUser();
             $user->setUsername($q->user->login)->setEmail($q->user->mail)->setPassword($this->encoder->encodePassword($user, md5(uniqid())))->setName($q->user->firstname)->setSurname($q->user->lastname)->setRedmineUserID($q->user->id)->setRedmineToken($q->user->api_key);
             $settings = new Settings();
             $settings->setSms(false)->setPush(false)->setCheckFirst(Carbon::createFromTime(17, 45))->setCheckSecond(Carbon::createFromTime(20, 0))->setCheckThird(Carbon::createFromTime(9, 30))->setUser($user);
             $this->em->persist($user);
             $this->em->persist($settings);
             $this->em->flush();
             return $user->getUsername();
         }
     } catch (\Exception $e) {
         return null;
     }
 }
 public function sendNotify(Message $message, RedmineUser $user)
 {
     if ($user->getDevices()->count()) {
         /** @var Device $device */
         foreach ($user->getDevices() as $device) {
             if ($device->isEnabled()) {
                 $logMessage = 'platform not found';
                 $logCustom = 'custom';
                 if ($device->getPlatform() === self::PLATFORM_ANDROID) {
                     $logMessage = $message->getGcmData();
                     $logCustom = $message->getCustom();
                 }
                 if ($device->getPlatform() === self::PLATFORM_IOS) {
                     $logMessage = $message->getApnsData();
                     $logCustom = $message->getCustom();
                 }
                 if ($device->getPlatform() === self::PLATFORM_IOS_SB) {
                     $logMessage = $message->getApnsData();
                     $logCustom = $message->getCustom();
                 }
                 try {
                     $this->messages->send($message, $device->getArn());
                     $this->logger->info("\nSend message to: ", ['DEVICE' => $device->toLog(), 'MESSAGE' => $logMessage, 'CUSTOM' => $logCustom]);
                 } catch (\Exception $e) {
                     $device->setEnabled(false);
                     $this->em->flush();
                     $this->logger->critical("\n Device is disabled: ", ['DeviceArn: ' => $device->getArn()]);
                 }
             }
             if (!$device->isEnabled()) {
                 $newDevice = $this->getDevice($device->getUser(), $device->getDeviceId(), $device->getPushtoken(), $device->getPlatform());
                 $logMessage = 'platform not found';
                 $logCustom = 'custom';
                 if ($device->getPlatform() === self::PLATFORM_ANDROID) {
                     $logMessage = $message->getGcmData();
                     $logCustom = $message->getCustom();
                 }
                 if ($device->getPlatform() === self::PLATFORM_IOS) {
                     $logMessage = $message->getApnsData();
                     $logCustom = $message->getCustom();
                 }
                 if ($device->getPlatform() === self::PLATFORM_IOS_SB) {
                     $logMessage = $message->getApnsData();
                     $logCustom = $message->getCustom();
                 }
                 try {
                     $this->messages->send($message, $newDevice->getArn());
                     $this->logger->info("\nSend message to: ", ['DEVICE' => $device->toLog(), 'MESSAGE' => $logMessage, 'CUSTOM' => $logCustom]);
                 } catch (\Exception $e) {
                     $device->setEnabled(false);
                     $this->em->flush();
                     $this->logger->critical("\n Device is disabled: ", ['DeviceArn: ' => $device->getArn()]);
                 }
             }
         }
         //  end foreach devices
     }
 }
 private function sendSMS(RedmineUser $user, $message)
 {
     $clientSMS = new SMSClient($this->alfa_sms_ID, $this->alfa_sms_password, $this->alfa_sms_api_key);
     try {
         $clientSMS->sendSMS($this->alfa_sms_name, '+38' . $user->getSettings()->getPhone(), $message);
     } catch (\Exception $e) {
     }
 }