<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\KuferTools\Oeffentlich\DoLogout;

class DoLogoutUseCase extends \Kufer\KuferTools\Basis\UseCase\UseCase
{
  
  private $loginApi;

  public function __construct($request, $loginApi)
  {
    parent::__construct($request, new DoLogoutResponse());
    $this->loginApi = $loginApi;
  }

  public function hasAccess($pToolSectionId = null)
  {
    return true;
  }

  public function action()
  {
    $success = $this->loginApi->doWtLogout();

    return $this->getResponse($success);
  }

  protected function getResponse($logoutSucess)
  {
    $this->response->logoutSuccess = $logoutSucess;
    $this->setzeMenupunkte();
    $this->response->mitCaptcha = $this->mitCaptcha();

    return $this->response;
  }

  private function setzeMenupunkte()
  {
    $menuBar = new \MenuBar();
    $menuBar->setMenuBarFromSettings(array(), 'LOGIN', null, true, true);

    $this->response->menuPunkte = $menuBar->getMenuPunkte();
  }

  private function mitCaptcha()
  {
    $tool0settings = \ToolSettingsAPI::getToolSettings(0);
    if ($tool0settings->captchaLoginFehlerhaft) {
      $dbwLoginVersuch = new \DBWorkToolsLoginError(1, true);

      return $dbwLoginVersuch->useCaptcha4CurrentIp();
    } else {
      return false;
    }
  }
}

class DoLogoutRequest
{
}

class DoLogoutResponse extends \Kufer\KuferTools\Basis\UseCase\BasisResponse
{
  public $logoutSuccess;
  public $mitCaptcha;
}