<?php
namespace App\Package\Admin\Tools\AdminUtil;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\{ Request, RequestStack };
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Package\Toolkit\ConfigurationBag\ConfigurationBag;
use App\Package\Admin\Main\EntityInterface\{ AdminInterface, AdminModuleInterface, AdminActionInterface };
use App\Package\Admin\Tools\AdminUtil\{ AdminUtilEntityClassTrait, AdminUtilRepositoryTrait };
/**
* AdminUtil
*
* Utilities related to admin package
* - getters for repositories
* - getters for class names
* - fetchers of module/action from database by field "name"
* - fetchers of current module/action from database using moduleSlug/actionSlug from url
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright Openform
* @since 03.2019
*/
class AdminUtil
{
use AdminUtilEntityClassTrait,
AdminUtilRepositoryTrait;
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var Request
*/
protected $request;
/**
* @var AdminInterface
*/
protected $admin;
/**
* @var ConfigurationBag
*/
protected $configBag;
/**
* @param EntityManagerInterface $em
* @param RequestStack $requestStack
* @param TokenStorageInterface $tokenStorage
* @param ConfigurationBag $configBag
*/
public function __construct(
EntityManagerInterface $em,
RequestStack $requestStack,
TokenStorageInterface $tokenStorage,
ConfigurationBag $configBag
) {
$this->em = $em;
$this->admin =
$tokenStorage->getToken() ?
$tokenStorage->getToken()->getUser() :
null;
$this->request = $requestStack->getMainRequest();
$this->configBag = $configBag;
}
/**
* Clears entity manager
*
* @return AdminUtil
*/
public function clearEm() : AdminUtil
{
$this->em->clear();
return $this;
}
/**
* Fetches from database admin module by name
*
* @param string $name
*
* @return AdminModuleInterface|null
*/
public function fetchAdminModule(string $name) : ? AdminModuleInterface
{
return
$this->getAdminModuleRepository()
->findOneByName($name);
}
/**
* Fetches from database current admin module
*
* @return AdminModuleInterface|null
*/
public function fetchCurrentAdminModule() : ? AdminModuleInterface
{
if (! $this->request) {
return null;
}
$moduleRepository = $this->getAdminModuleRepository();
return
$this->request->get('moduleSlug') ?
$moduleRepository->getVisibleBySlug(
$this->request->get('moduleSlug'),
$this->request->getLocale()
) :
(
strpos($this->request->get('_route'), 'admin_adminModule_home') !== false ?
$moduleRepository->findOneByName(
$this->configBag->get('admin:general:module_home')
) :
null
);
}
/**
* Fetches admin action by name
*
* @param string $name
*
* @return AdminActionInterface|null
*/
public function fetchAdminAction(string $name) : ? AdminActionInterface
{
return
$this->getAdminActionRepository()
->findOneByName($name);
}
/**
* Fetches current admin action
*
* @return AdminActionInterface|null
*/
public function fetchCurrentAdminAction() : ? AdminActionInterface
{
if (! $this->request) {
return null;
}
$actionRepository = $this->getAdminActionRepository();
return
$this->request->get('actionSlug') ?
$actionRepository->getVisibleBySlug(
$this->request->get('actionSlug'),
$this->request->getLocale()
) :
(
(
strpos($this->request->get('_route'), 'admin_adminModule_action') !== false ||
strpos($this->request->get('_route'), 'admin_adminModule_home') !== false
) ?
$actionRepository->findOneByName(
$this->configBag->get('admin:general:action_index')
) :
null
);
}
}