<?php
namespace App\Package\Admin\Tools\AdminState;
use App\Package\Admin\Main\EntityInterface\{ AdminModuleInterface, AdminActionInterface };
use App\Package\Core\EntityInterface\SimpleEntityInterface;
use App\Package\Toolkit\ApplicationMode\ApplicationMode,
App\Package\Admin\Tools\AdminUtil\AdminUtil,
App\Package\Admin\Tools\ObjectUtil\ObjectUtil,
App\Package\Admin\Tools\UrlUtil\UrlUtil,
App\Package\Toolkit\Exception\NotFoundException\NotFoundException;
/**
* AdminState
*
* Holds:
* - current module
* - current action
* - current object
* - current object id
* - current object parent id
* - current object parent is self
* - current query string
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright Openform
* @since 03.2019
*/
class AdminState
{
/**
* @var AdminModuleInterface
*/
protected $currentModule;
/**
* @var AdminActionInterface
*/
protected $currentAction;
/**
* @var string
*/
protected $currentObjectPackage;
/**
* @var string
*/
protected $currentObjectEntityName;
/**
* @var SimpleEntityInterface
*/
protected $currentObject;
/**
* @var int
*/
protected $currentObjectId;
/**
* @var int
*/
protected $currentObjectParentId;
/**
* @var boolean
*/
protected $currentObjectParentIsSelf;
/**
* @var mixed[]
*/
protected $currentQueryArgs;
/**
* @param ApplicationMode $applicationMode
* @param AdminUtil $adminUtil
* @param ObjectUtil $objectUtil
* @param UrlUtil $urlUtil
*
* @throws NotFoundException if currentObjectId is not null and currentObject is null
* (object does not exist)
*/
public function __construct(
ApplicationMode $applicationMode,
AdminUtil $adminUtil,
ObjectUtil $objectUtil,
UrlUtil $urlUtil
) {
if ($applicationMode->getCurrentMode() === 'admin') {
$this->currentModule = $adminUtil->fetchCurrentAdminModule();
$this->currentAction = $adminUtil->fetchCurrentAdminAction();
$this->currentObjectPackage =
$this->currentModule ?
(
$this->currentModule->getEntityName() && $this->currentModule->getEntityPackage() ?
$this->currentModule->getEntityPackage() :
$this->currentModule->getPackage()
) :
null;
$this->currentObjectEntityName =
$this->currentModule ?
(
$this->currentModule->getEntityName() && $this->currentModule->getEntityPackage() ?
$this->currentModule->getEntityName() :
$this->currentModule->getName()
) :
null;
$this->currentObjectId = $objectUtil->getCurrentObjectId();
$this->currentObject =
$this->currentModule && $this->currentObjectId ?
(
$this->currentModule->getEntityName() && $this->currentModule->getEntityPackage() ?
$objectUtil->fetchObjectByPackageColonEntityName(
$this->currentObjectPackage . ':' . $this->currentObjectEntityName,
$this->currentObjectId
) :
$objectUtil->fetchObject($this->currentModule, $this->currentObjectId)
) :
null;
if ($this->currentObjectId && ! $this->currentObject) {
throw new NotFoundException(
' >> 404 Object, package: ' . $this->currentObjectPackage . ', '
. ' entity: ' . $this->currentObjectEntityName . ', id: ' . $this->currentObjectId
);
}
$this->currentQueryArgs = $urlUtil->getQueryArgs();
$this->currentObjectParentId =
array_key_exists('pid', $this->currentQueryArgs) ?
$this->currentQueryArgs[ 'pid' ] :
null;
$this->currentObjectParentIsSelf =
array_key_exists('pself', $this->currentQueryArgs) ?
true :
false;
}
}
/**
* Gets current admin module
*
* @return AdminModuleInterface|null
*/
public function getCurrentModule() : ? AdminModuleInterface
{
return $this->currentModule;
}
/**
* Gets current admin action
*
* @return AdminActionInterface|null
*/
public function getCurrentAction() : ? AdminActionInterface
{
return $this->currentAction;
}
/**
* Gets current object package name
*
* @return string|null
*/
public function getCurrentObjectPackage() : ? string
{
return $this->currentObjectPackage;
}
/**
* Gets current object entity name
*
* @return string|null
*/
public function getCurrentObjectEntityName() : ? string
{
return $this->currentObjectEntityName;
}
/**
* Gets current object id
*
* @return integer|null
*/
public function getCurrentObjectId() : ? int
{
return $this->currentObjectId;
}
/**
* Gets current object
*
* @return SimpleEntityInterface|null
*/
public function getCurrentObject() : ? SimpleEntityInterface
{
return $this->currentObject;
}
/**
* Gets current object parent id
*
* @return integer|null
*/
public function getCurrentObjectParentId() : ? int
{
return $this->currentObjectParentId;
}
/**
* Gets current object parent is self
*
* @return boolean
*/
public function getCurrentObjectParentIsSelf() : bool
{
return $this->currentObjectParentIsSelf;
}
/**
* Gets current query args array
*
* @return array|null
*/
public function getCurrentQueryArgs() : ? array
{
return $this->currentQueryArgs;
}
}