簡體   English   中英

如何在Symfony命令中使用mailer和datetime注入服務?

[英]How to inject a service with mailer and datetime in a Symfony Command?

我想在庫存對象有效期結束前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)

問題是您在ValidityReminderCommand中對execute方法的聲明與Command的聲明不兼容。

除了前兩個參數外,您不應傳遞任何內容:InputInterface $ input,OutputInterface $ output

將其他參數傳遞給構造函數,就像對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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM