简体   繁体   中英

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. My problem is that I do not know how to run my service in the ValidityReminderCommand class.

Here is my 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:

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.

You should not pass anything but the first two argument : InputInterface $input, OutputInterface $output

Pass the other arguments trough the constructor like you did for your ValidityHandler argument

    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');

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM