[英]How to inject a service with mailer and datetime in a Symfony Command?
I would like to send emails to all users 4 weeks before the end of validity of the inventory object. 我想在库存对象有效期结束前4周向所有用户发送电子邮件。 My problem is that I do not know how to run my service in the ValidityReminderCommand class. 我的问题是我不知道如何在ValidityReminderCommand类中运行我的服务。
Here is my 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');
}
}
And here is my ValidityEndHandler as a Service: 这是我的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);
}
}
}
}
}
This is some error message I get when I try it in the console: 这是在控制台中尝试时收到的一些错误消息:
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)
The problem is your declaration of execute method in ValidityReminderCommand is not compatible with the declaration of Command. 问题是您在ValidityReminderCommand中对execute方法的声明与Command的声明不兼容。
You should not pass anything but the first two argument : InputInterface $input, OutputInterface $output 除了前两个参数外,您不应传递任何内容:InputInterface $ input,OutputInterface $ output
Pass the other arguments trough the constructor like you did for your ValidityHandler argument 将其他参数传递给构造函数,就像对ValidityHandler参数所做的那样
public function __construct(ValidityEndHandler $validityEndHandler, InventoryRepository $repository)
{
$this->validityEndHandler = $validityEndHandler;
$this->repository = $repository;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$inventory = $this->repository->yourRequest();
// pass the mailer service you need directly to ValidityHandler and send only $inventory as a parameter
$this->validityEndHandler->sendReminderMail($inventory);
$output->writeln('Emails werden gesendet');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.