Commit 0dbb3a99 authored by Thomas Löffler's avatar Thomas Löffler
Browse files

Use given arguments of command and add strict types

parent d00ac976
......@@ -28,47 +28,22 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
/**
* @var array
*/
protected $hookObjects = array();
/**
* @var string
*/
public $importFile = '';
/**
* @var string
*/
public $dropboxToken = '';
/**
* @var integer
*/
public $membershipStoragePid = 0;
protected $hookObjects = [];
/**
* @var array
*/
protected $memberships = array();
protected $memberships = [];
/**
* Configure the command by defining the name, options and arguments
*/
protected function configure()
protected function configure(): void
{
$this->setDescription('Import members');
//$this->setHelp('Meaningful help text');
$this->addArgument(
'importFile',
InputArgument::REQUIRED
);
$this->addArgument(
'membershipStoragePid',
InputArgument::REQUIRED
);
$this->addArgument(
'dropboxToken',
InputArgument::REQUIRED
);
$this->addArgument('importFile', InputArgument::REQUIRED);
$this->addArgument('membershipStoragePid', InputArgument::REQUIRED);
$this->addArgument('dropboxToken', InputArgument::REQUIRED);
}
/**
......@@ -77,7 +52,7 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$io->title($this->getDescription());
......@@ -96,15 +71,15 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
}
// does the import file exist?
$importFile = $this->getImportFile();
$importFile = $this->getImportFile($input);
if (!GeneralUtility::isAbsPath($importFile)) {
$importFile = GeneralUtility::getFileAbsFileName($importFile);
}
if (!file_exists($importFile)) {
$logger->debug('No importfile', array('filename' => $importFile));
$logger->debug('No importfile', ['filename' => $importFile]);
return false;
return;
}
$this->initializeHookObjects();
......@@ -133,7 +108,7 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
$hidden = true;
}
$member = array(
$member = [
'name' => $fields[6],
'subscription_no' => $subscriptionNo,
'external_id' => (int)$fields[0],
......@@ -143,10 +118,10 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
'country' => $fields[13],
'end_date' => $endDate,
'endtime' => $endTime,
'hidden' => $hidden,
'hidden' => (int)$hidden,
'starttime' => 0,
'membership' => $membershipUid,
'pid' => $this->membershipStoragePid,
'pid' => $input->getArgument('membershipStoragePid'),
'crdate' => time(),
'tstamp' => time(),
'invoice_email' => $fields[84],
......@@ -154,7 +129,7 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
'url' => $fields[80],
'firstname' => $fields[82],
'lastname' => $fields[83]
);
];
$memberUid = $this->createOrUpdateMember($subscriptionNo, $member);
......@@ -168,48 +143,50 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
$io->writeln('Finished.');
}
protected function getDatabaseConnection(): ConnectionPool
{
return GeneralUtility::makeInstance(ConnectionPool::class);
}
/**
* Checks if the member with the given subscription number already exists in the database.
* If he exists, his data will be updated, otherwise a new record will be inserted.
* Gets importFile
*
* @param int $subscriptionNo
* @param array $memberData
* @return int The uid of the updated / inserted member.
* @return string
*/
protected function createOrUpdateMember($subscriptionNo, array $memberData)
public function getImportFile(InputInterface $input): string
{
$queryBuilder = $this->getDatabaseConnection()
->getQueryBuilderForTable('tx_t3omembership_domain_model_member');
$statement = $queryBuilder->select('uid')
->from('tx_t3omembership_domain_model_member')
->where(
$queryBuilder->expr()->eq('subscription_no',
$queryBuilder->createNamedParameter($subscriptionNo, \PDO::PARAM_INT))
)->execute();
$existingMember = $statement->fetch();
$dropbox = new Dropbox($input->getArgument('dropboxToken'));
$dropbox->files->download('/TYPO3 Backoffice/Member-Lists/memberlist.txt', $input->getArgument('importFile'));
if (!empty($existingMember['uid'])) {
$memberUid = $existingMember['uid'];
return $input->getArgument('importFile');
}
$this->getDatabaseConnection()
->getConnectionForTable('tx_t3omembership_domain_model_member')
->update(
'tx_t3omembership_domain_model_member',
$memberData,
['uid' => (int)$memberUid]
);
} else {
$datebaseConnection = $this->getDatabaseConnection()
->getConnectionForTable('tx_t3omembership_domain_model_member');
$datebaseConnection->insert(
'tx_t3omembership_domain_model_member',
$memberData
);
$memberUid = (int)$datebaseConnection->lastInsertId('tx_t3omembership_domain_model_member');
/**
* Instantiates all configured hook objects.
*/
protected function initializeHookObjects(): void
{
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3omembership']['importMemberTaksHooks'])) {
return;
}
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3omembership']['importMemberTaksHooks'] as $classData) {
$hookObject = GeneralUtility::makeInstance($classData);
if (!is_object($hookObject)) {
throw new \UnexpectedValueException( // @TODO Namespace?
'The hook object class ' . $classData . ' could not be instantiated.');
}
$this->hookObjects[] = $hookObject;
}
}
return (int)$memberUid;
protected function getMembershipUid(string $membershipName): ?int
{
$membershipName = trim(str_replace('Membership', '', $membershipName));
if (empty($this->memberships[$membershipName])) {
return null;
}
return $this->memberships[$membershipName];
}
/**
......@@ -218,7 +195,7 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
* @param string $endDate
* @return int
*/
protected function getMemberEndDate($endDate)
protected function getMemberEndDate(string $endDate): int
{
if (empty($endDate)) {
return 0;
......@@ -231,57 +208,33 @@ class ImportMembersCommand extends \Symfony\Component\Console\Command\Command
}
/**
* @param string $membershipName
* @return integer|null
*/
protected function getMembershipUid($membershipName)
{
$membershipName = trim(str_replace('Membership', '', $membershipName));
if (empty($this->memberships[$membershipName])) {
return null;
}
return $this->memberships[$membershipName];
}
/**
* Gets importFile
* Checks if the member with the given subscription number already exists in the database.
* If he exists, his data will be updated, otherwise a new record will be inserted.
*
* @return string
* @param int $subscriptionNo
* @param array $memberData
* @return int The uid of the updated / inserted member.
*/
public function getImportFile()
protected function createOrUpdateMember(int $subscriptionNo, array $memberData): int
{
$dropbox = new Dropbox($this->dropboxToken);
$dropbox->files->download('/TYPO3 Backoffice/Member-Lists/memberlist.txt', $this->importFile);
$queryBuilder = $this->getDatabaseConnection()->getQueryBuilderForTable('tx_t3omembership_domain_model_member');
return $this->importFile;
}
$statement = $queryBuilder->select('uid')->from('tx_t3omembership_domain_model_member')->where($queryBuilder->expr()
->eq('subscription_no', $queryBuilder->createNamedParameter($subscriptionNo, \PDO::PARAM_INT)))->execute();
$existingMember = $statement->fetch();
if (!empty($existingMember['uid'])) {
$memberUid = $existingMember['uid'];
/**
* Instantiates all configured hook objects.
*/
protected function initializeHookObjects()
{
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3omembership']['importMemberTaksHooks'])) {
return;
}
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3omembership']['importMemberTaksHooks'] as $classData) {
$hookObject = GeneralUtility::makeInstance($classData);
if (!is_object($hookObject)) {
throw new \UnexpectedValueException( // @TODO Namespace?
'The hook object class ' . $classData . ' could not be instantiated.'
);
}
$this->hookObjects[] = $hookObject;
$this->getDatabaseConnection()
->getConnectionForTable('tx_t3omembership_domain_model_member')
->update('tx_t3omembership_domain_model_member', $memberData, ['uid' => (int)$memberUid]);
} else {
$datebaseConnection = $this->getDatabaseConnection()->getConnectionForTable('tx_t3omembership_domain_model_member');
$datebaseConnection->insert('tx_t3omembership_domain_model_member', $memberData);
$memberUid = (int)$datebaseConnection->lastInsertId('tx_t3omembership_domain_model_member');
}
}
/**
* @return \TYPO3\CMS\Core\Database\ConnectionPool
*/
protected function getDatabaseConnection()
{
return GeneralUtility::makeInstance(ConnectionPool::class);
return (int)$memberUid;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment