Commit bcb6661c authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[TASK] Replace command controller with symfony commands

parent ae2b3f6d
......@@ -29,53 +29,42 @@ namespace SGalinski\SgMail\Command;
use SGalinski\SgMail\Domain\Model\Mail;
use SGalinski\SgMail\Domain\Repository\MailRepository;
use SGalinski\SgMail\Service\PlaintextService;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
use Symfony\Component\Console\Command\Command;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
/**
* Command controller for the mailing feature
*/
class SendMailCommandController extends CommandController {
class SendMailCommandController extends Command {
/**
* @var MailRepository
* Configure command
*/
protected $mailRepository;
/**
* Inject the MailRepository
*
* @param MailRepository $mailRepository
*/
public function injectMailRepository(MailRepository $mailRepository) {
$this->mailRepository = $mailRepository;
}
/**
* @var PersistenceManager
*/
protected $persistenceManager;
/**
* Inject the PersistenceManager
*
* @param PersistenceManager $persistenceManager
*/
public function injectPersistenceManager(PersistenceManager $persistenceManager) {
$this->persistenceManager = $persistenceManager;
public function configure() {
$this->setDescription('Executes the sending of mails with a maximum of the given sentCount.')
->addArgument('sendCount', InputArgument::OPTIONAL, 'Send count', 50);
}
/**
* Executes the sending of mails with a maximum of the given sentCount.
* Execute the command
*
* @param int $sendCount
* @return void
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws \TYPO3\CMS\Extbase\Object\Exception
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/
public function runSendMailsCommand($sendCount = 50) {
$mailsToSend = $this->mailRepository->findMailsToSend($sendCount);
public function execute(InputInterface $input, OutputInterface $output) {
$sendCount = $input->getArgument('sendCount');
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$mailRepository = $objectManager->get(MailRepository::class);
$mailsToSend = $mailRepository->findMailsToSend($sendCount);
foreach ($mailsToSend as $mailToSend) {
/** @var Mail $mailToSend */
......@@ -96,9 +85,9 @@ class SendMailCommandController extends CommandController {
if (empty($fromAddress) || empty($toAddresses)) {
continue;
}
$this->mailRepository->update($mailToSend);
$mailRepository->update($mailToSend);
$mailMessage = $this->objectManager->get(MailMessage::class);
$mailMessage = GeneralUtility::makeInstance(MailMessage::class);
$mailMessage->setFrom($fromAddress, $mailToSend->getFromName());
$mailMessage->setTo($toAddresses);
$mailMessage->setSubject($mailSubject);
......@@ -152,6 +141,7 @@ class SendMailCommandController extends CommandController {
}
// Important for command controllers that change data
$this->persistenceManager->persistAll();
$objectManager->get(PersistenceManager::class)->persistAll();
return 0;
}
}
<?php
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace SGalinski\SgMail\Updates;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
use TYPO3\CMS\Scheduler\Scheduler;
use TYPO3\CMS\Scheduler\Task\ExecuteSchedulableCommandTask;
/**
* Class MigrateSchedulerTasks
*
* @package SGalinski\SgMail\Updates
*/
class MigrateSchedulerTasks implements UpgradeWizardInterface {
/**
* Identifier of the upgrade
*/
const IDENTIFIER = 'tx_sgmail_migrateschedulertasks';
/**
* @inheritDoc
*/
public function getIdentifier(): string {
return self::IDENTIFIER;
}
/**
* @inheritDoc
*/
public function getTitle(): string {
return 'Migrate sg_mail scheduler tasks to symfony commands API';
}
/**
* @inheritDoc
*/
public function getDescription(): string {
return 'This upgrade migrates all sg_mail scheduler tasks, created with the old CommandController API to the new Symfony Commands API';
}
/**
* @inheritDoc
*/
public function executeUpdate(): bool {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'tx_scheduler_task'
);
$tasks = $queryBuilder->select('serialized_task_object')
->from('tx_scheduler_task')
->where(
$queryBuilder->expr()->eq('disable', 0)
)->execute()->fetchAll();
foreach ($tasks as $_task) {
$task = unserialize($_task['serialized_task_object'], [\__PHP_Incomplete_Class::class]);
$taskVars = $this->cleanArrayKeys((array)$task);
$identifier = $taskVars['*commandIdentifier'];
if (
$identifier === 'sg_mail:sendmail:runsendmails'
) {
$this->replaceTask($taskVars);
}
}
return TRUE;
}
/**
* Replace the given extbase task implementation with the new Symfony Commands API one
*
* @param array $task
*/
protected function replaceTask(array $task) {
$commandTask = GeneralUtility::makeInstance(ExecuteSchedulableCommandTask::class);
switch ($task['*commandIdentifier']) {
case 'sg_mail:sendmail:runsendmails':
$commandTask->setCommandIdentifier('sg_mail:sendMail');
break;
}
if (is_array($task['*arguments'])) {
$commandTask->setArguments($task['*arguments']);
}
if (is_array($task['*defaults'])) {
foreach ($task['*defaults'] as $key => $default) {
$commandTask->addDefaultValue($key, $default);
}
}
$commandTask->setTaskGroup($task['*taskGroup']);
$commandTask->setExecution($task['*execution']);
$commandTask->setExecutionTime($task['*executionTime']);
$commandTask->setRunOnNextCronJob($task['*runOnNextCronJob']);
$commandTask->setTaskUid($task['*taskUid']);
GeneralUtility::makeInstance(Scheduler::class)->saveTask($commandTask);
}
/**
* @inheritDoc
*/
public function updateNecessary(): bool {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'tx_scheduler_task'
);
$tasks = $queryBuilder->select('serialized_task_object')
->from('tx_scheduler_task')
->where(
$queryBuilder->expr()->eq('disable', 0)
)->execute()->fetchAll();
foreach ($tasks as $_task) {
$task = unserialize($_task['serialized_task_object'], [\__PHP_Incomplete_Class::class]);
$taskVars = $this->cleanArrayKeys((array)$task);
$identifier = $taskVars['*commandIdentifier'];
if (
$identifier === 'sg_mail:sendmail:runsendmails'
) {
return TRUE;
}
}
return FALSE;
}
/**
* Cleans array keys from their hidden \0
*
* @param array $array
* @return array
*/
protected function cleanArrayKeys(array $array) {
$newArray = [];
foreach($array as $key => $value) {
$newArray[str_replace("\0", '', $key)] = $value;
}
return $newArray;
}
/**
* @inheritDoc
*/
public function getPrerequisites(): array {
return [
DatabaseUpdatedPrerequisite::class
];
}
}
<?php
return [
'sg_mail:sendMail' => [
'class' => \SGalinski\SgMail\Command\SendMailCommandController::class
]
];
......@@ -25,16 +25,12 @@
call_user_func(
function ($extKey) {
// register command controllers
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] =
\SGalinski\SgMail\Command\SendMailCommandController::class;
// add upgrade wizard for moving all db entries to their respected siteroot
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\SGalinski\SgMail\Updates\UpdatePidToSiteRoot::IDENTIFIER] = \SGalinski\SgMail\Updates\UpdatePidToSiteRoot::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\SGalinski\SgMail\Updates\UpdateSendingTimes::IDENTIFIER] = \SGalinski\SgMail\Updates\UpdateSendingTimes::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\SGalinski\SgMail\Updates\UpdateLanguages::IDENTIFIER] = \SGalinski\SgMail\Updates\UpdateLanguages::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\SGalinski\SgMail\Updates\UpdateGermanAsDefault::IDENTIFIER] = \SGalinski\SgMail\Updates\UpdateGermanAsDefault::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\SGalinski\SgMail\Updates\MigrateSchedulerTasks::IDENTIFIER] = \SGalinski\SgMail\Updates\MigrateSchedulerTasks::class;
if (TYPO3_MODE === 'BE') {
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(
......
Supports Markdown
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