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

namespace Kufer\Web\KuferTools\Allgemein\Heartbeat\Heartbeat;

use Kufer\Web\KuferTools\Basis\UseCase\BasisResponse;
use Kufer\Web\KuferTools\Basis\UseCase\UseCase;

class HeartbeatUseCase extends UseCase
{
  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new HeartbeatResponse());
  }

  protected function initResponseBase($response)
  {
    $response->timestamp = \DateTimeOperations::getTimestampNow();
    $response->success = true;
  }

  public function action()
  {
    return $this->response;
  }

  public function hasAccess($pToolSectionId = null)
  {
    return !is_null($this->getUser());
  }

  protected function getUser(): ?\Benutzer
  {
    $result = \SessionOperations::getCurrentWebToolsUser();
    if (empty($result->id)) {
      return null;
    }

    return $result;
  }
}

class HeartbeatRequest
{
}

class HeartbeatResponse
{
  public $success;
  public $timestamp;
}