<?php
namespace ProjectBiz\DatabaseBundle\Database;
use Doctrine\Persistence\ManagerRegistry;
use ProjectBiz\PortalBundle\Exceptions\PortalException;
use ProjectBiz\DatabaseBundle\Service\RepositoryConfigurationManager;
use ProjectBiz\UserBundle\Service\SecurityContextWrapper;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Container;
class GenericRepositoryFactory implements GenericRepositoryFactoryInterface
{
const msgMissingTable = 'Die Tabelle »%table%« existiert nicht.';
protected $doctrine;
protected $securityContextWrapper;
protected $tableHelper;
protected $logger;
protected $configManager;
protected $dateTimeFactory;
protected $serviceContainer;
protected $repoFactory;
protected $dateFormat;
protected $dateTimeFormat;
/**
* Construct.
*
* @param ManagerRegistry $doctrine
* @param SecurityContextWrapper $securityContextWrapper
* @param TableHelper $tableHelper
* @param LoggerInterface $logger
* @param RepositoryConfigurationManager $configManager
*/
public function __construct(
ManagerRegistry $doctrine,
SecurityContextWrapper $securityContextWrapper,
TableHelper $tableHelper,
LoggerInterface $logger,
RepositoryConfigurationManager $configManager,
string $prefix,
DateTimeFactory $dateTimeFactory,
Container $serviceContainer,
$dateFormat,
$dateTimeFormat,
$schemaCache,
$requestStack
) {
$this->doctrine = $doctrine->getManager();
$this->securityContextWrapper = $securityContextWrapper;
$this->tableHelper = $tableHelper;
$this->logger = $logger;
$this->configManager = $configManager;
$this->prefix = $prefix;
$this->dateTimeFactory = $dateTimeFactory;
$this->serviceContainer = $serviceContainer;
$this->dateFormat = $dateFormat;
$this->dateTimeFormat = $dateTimeFormat;
$this->schemaCache = $schemaCache;
$this->requestStack = $requestStack;
}
/**
* @inheritdoc
*/
public function createGenericRepository($tablename, $options = []) : GenericRepository
{
// Get a table descriptor. It is just used to determine the MAIN tablename
$tableDesciptor = $this->configManager->lookupDescriptor($tablename);
if (!$tableDesciptor) {
if (!in_array($tablename, $this->tableHelper->getTables())) {
throw new PortalException(self::msgMissingTable, ['%table%' => $tablename], PortalException::MISSING_TABLE);
}
$tableDesciptor = new TableDescriptor($tablename, $tablename);
}
// Let the config manager modify the options / supply defaults
$options = $this->configManager->buildOptions($tablename, $options);
// Create the repository options - use MAIN tablename
$repositoryOptions = new GenericRepositoryOptions(
$this->tableHelper->getTableInfo($tableDesciptor->getMainTablename()),
$options
);
// Create the repository
$repo = new GenericRepository(
$this->doctrine->getConnection(),
$this->securityContextWrapper,
$this->tableHelper->getTableInfo($tablename),
$repositoryOptions,
$this->prefix,
$this->tableHelper,
$this->logger,
$this->dateTimeFactory,
$this->serviceContainer->get('projectbiz.mailer.mail_notifier'),
$this,
$this->dateFormat,
$this->dateTimeFormat,
$this->schemaCache,
$this->requestStack,
$this->serviceContainer
);
// Let the configuration manager setup event handler for the repository
return $this->configManager->afterCreate($tablename, $repo);
}
}