我想在库存对象有效期结束前4周向所有用户发送电子邮件。我的问题是我不知道如何在ValidityReminderCommand类中运行服务。
这是我的ValidityReminderCommand:
namespace App\Command;
use App\Entity\Inventory;
use App\Repository\InventoryRepository;
use App\Service\ValidityEndHandler;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ValidityReminderCommand extends Command
{
protected static $defaultName = 'app:validity-reminder';
private $validityEndHandler;
public function __construct(ValidityEndHandler $validityEndHandler)
{
$this->validityEndHandler = $validityEndHandler;
parent::__construct();
}
protected function configure()
{
$this->setDescription('Sendet 4 Wochen vor Gültigkeitsende eine Mail an alle Admins');
}
protected function execute(InputInterface $input, OutputInterface $output, Inventory $inventory, \Swift_Mailer $mailer, InventoryRepository $repository)
{
$this->validityEndHandler->sendReminderMail($inventory, $mailer, $repository);
$output->writeln('Emails werden gesendet');
}
}
这是我的ValidityEndHandler即服务:
namespace App\Service;
use App\Entity\Inventory;
use App\Entity\User;
use App\Repository\InventoryRepository;
class ValidityEndHandler
{
public function sendReminderMail (Inventory $inventory, \Swift_Mailer $mailer, InventoryRepository $repository)
{
$itemsToRemind = $repository
->createQueryBuilder('i')
->where('i.validityEnd - 2419200 <= :now')
->setParameter('now', new \DateTime('now'))
->where('i.remindedAt is null')->getQuery()->execute();
foreach ($itemsToRemind as $itemToRemind) {
$endDate = $itemToRemind->$inventory->getValidityEnd();
$fourweeks = 2419200;
$endDateTimestamp = strtotime($endDate);
$difference = $endDateTimestamp - $fourweeks;
$now = time();
$title = $inventory->getTitle();
$admins = $repository
->createQueryBuilder('q')
->where('q.roles like :adminrole')
->setParameter('adminrole', '%ROLE_ADMIN%')
->getQuery()
->execute();
if ($difference == $now) {
/** @var User $admin */
foreach ($admins as $admin) {
$message = (new \Swift_Message('Eine Lizenz läuft in 4 Wochen ab.'))
->setFrom('noreply@validityEndLBX.com')
->setTo($admin->getEmail())
->setBody(
"Das Inventarobjekt . $title . läuft in 4 Wochen ab",
'text/html'
);
$mailer->send($message);
}
}
}
}
}
这是我在控制台中尝试得到的一些错误消息:
Warning: Declaration of App\Command\ValidityReminderCommand::execute(Symfony\Component\Console\Input\InputInterface $input, Symfony
\Component\Console\Output\OutputInterface $output, App\Entity\Inventory $inventory, Swift_Mailer $mailer, App\Repository\InventoryR
epository $repository) should be compatible with Symfony\Component\Console\Command\Command::execute(Symfony\Component\Console\Input
\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output)
答案 0 :(得分:4)
问题是您在ValidityReminderCommand中对execute方法的声明与Command的声明不兼容。
除了前两个参数外,您不应传递任何内容:InputInterface $ input,OutputInterface $ output
将其他参数传递给构造函数,就像对ValidityHandler参数所做的那样
ob_clean();