<?php

namespace Drupal\restaurant\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\restaurant\App\Base;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;


abstract class MultipartForm extends AppForm implements Base
{

  /**
  * @var \Drupal\user\PrivateTempStoreFactory
  */
  protected $tempStoreFactory;

 /**
  * @var \Drupal\Core\Session\SessionManagerInterface
  */
  private $sessionManager;

 /**
  * @var \Drupal\Core\Session\AccountInterface
  */
  private $currentUser;

 /**
  * @var \Drupal\user\PrivateTempStore
  */
  protected $store;

  public function __construct($temp_store_factory, SessionManagerInterface $session_manager, AccountInterface $current_user)
  {
   $this->tempStoreFactory = $temp_store_factory;
   $this->sessionManager = $session_manager;
   $this->currentUser = $current_user;

   $this->store = $this->tempStoreFactory->get('multistep_data');
   parent::__construct();
 }

 /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container)
  {
    return new static(
      $container->get('tempstore.private'),
      $container->get('session_manager'),
      $container->get('current_user')
    );
  }

  public function buildForm(array $form, FormStateInterface $form_state)
  {
    if ($this->currentUser->isAnonymous() && !isset($_SESSION['multistep_form_holds_session'])) {
      $_SESSION['multistep_form_holds_session'] = true;
      $this->sessionManager->start();
    }
    return $form;
  }



}
