<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Oeffentlich\PasswortAendern\SendePasswortAendernAnfordern;

use Kufer\Web\KuferTools\Oeffentlich\PasswortAnfordern\Utils\HandlePasswortAnfordernEingaben as HandlePasswortAnfordern;

class SendePasswortAendernAnfordernController extends \Kufer\Web\KuferTools\Basis\Controller\Controller
{
  protected function getUseCase(): \Kufer\Web\KuferTools\Basis\UseCase\UseCase
  {
    return new SendePasswortAendernAnfordernUseCase(
      $this->getRequestObj()
    );
  }

  protected function getRequestObj()
  {
    $filter = \FilterFactory::getFilterClass();
    $captcha = \Kufer\Web\Classes\Captchas\CaptchaFactory::getCaptcha4KuferTools();

    $request = new SendePasswortAendernAnfordernRequest();
    $request->login = $filter->filterLogin(\PostOperations::getVarValue('login'));
    
    
    $request->captchaOk = $captcha->isValid(null, true, 'tedit', true);

    $action = $this->getActionVal();

    return $this->handleXsrf($request, $action);
  }

  protected function getControllerToForward($pUseCaseResponse)
  {
    if (!empty($pUseCaseResponse->errorStatusCodes)
        && (!$pUseCaseResponse->ktPwVergessenVorRateAngriffSchuetzen
          || empty(array_intersect($this->insecureStatusCodes(), $pUseCaseResponse->errorStatusCodes)))) {
      return new \Kufer\Web\KuferTools\Oeffentlich\PasswortAendern\PasswortAendernAnfordern\PasswortAendernAnfordernErrorController($pUseCaseResponse->login, $pUseCaseResponse->errorStatusCodes);
    }

    return null;
  }

  protected function getPresenter($pUseCaseResponse)
  {
    return new SendePasswortAendernAnfordernPresenter($pUseCaseResponse);
  }

  private function insecureStatusCodes(): array
  {
    return array(
      HandlePasswortAnfordern::ERROR_LOGIN_NICHT_GEFUNDEN,
      HandlePasswortAnfordern::ERROR_LOGIN_OHNE_EMAIL,
      HandlePasswortAnfordern::ERROR_FEHLER_BEIM_DBLOPTIN_EMAIL_VERSAND,
      HandlePasswortAnfordern::ERROR_PASSWORT_ANFORDERN_NICHT_ERLAUBT_FUER_LDAP_LOGIN
  );
  }
}