Bonjour,
je me permets de déterrer ce post puisque je cherche également à envoyer un email depuis une ligne de commande. Ca coince lors du MailerFactory.
J'ai une Class Notification qui extends ContainerAwareCommand sur laquelle j'ai rajouté la fonction init() de Roadster.
Une Class DocumentManager qui dispatche vers les events JokerKit::DOCUMENT_EXPIRED ou JokerKit::DOCUMENT_TO_BE_EXPIRED
Visiblement, il faut utiliser un dispatcher spécifique lorsqu'on travaille depuis la ligne de commande (ContainerAwareEventDispatcher)
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
class DocumentManager
{
public static function alertDocumentsToBeExpired(ContainerAwareEventDispatcher $dispatcher)
{
$dispatcher->dispatch(JokerKit::DOCUMENT_EXPIRED, $alertEvent);
OU
$dispatcher->dispatch(JokerKit::DOCUMENT_TO_BE_EXPIRED, $alertEvent);
}
}
Enfin, mon écouteur d'évènements dont le constructeur qui ne fonctionne pas :
namespace JokerKit\EventListeners;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use JokerKit\JokerKit;
class SendAdminEmail implements EventSubscriberInterface
{
/** @var MailerFactory */
protected $mailer;
public function __construct(MailerFactory $mailer)
{
$this->mailer = $mailer;
}
public static function getSubscribedEvents()
{
return array(
JokerKit::DOCUMENT_TO_BE_EXPIRED => array("sendWarningAdminEmail", 128),
JokerKit::DOCUMENT_EXPIRED => array("sendErrorAdminEmail", 128)
);
}
/**
* Send email to notify administrators that documents will expires soon.
*
* @param DocumentEvent $event
* @throws \Exception
*/
public function sendWarningAdminEmail(DocumentEvent $event)
{
$documentTitle = $event->getTitle();
// Send warning email
}
/**
* Send email to notify administrators that documents have been expired.
*
* @param DocumentEvent $event
* @throws \Exception
*/
public function sendErrorAdminEmail(DocumentEvent $event)
{
$documentTitle = $event->getTitle();
// Send error email
}
}
?>
Dans le fichier config du module, j'ai bien renseigné le service Mailer pour mon écouteur :
<services>
<service id="jokerkit.document.expire.listener" class="JokerKit\EventListeners\SendAdminEmail">
<argument type="service" id="mailer"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
J'obtiens cette erreur :
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to JokerKit\EventListeners\SendAdminEmail::__construct() must be an instance of JokerKit\EventListeners\MailerFactory, instance of Thelia\Mailer\MailerFactory given, called in /var/www/vhosts/demo.joker-courses.com/httpdocs/cache/dev/CoreDevDebugProjectContainer.php on line 966 in /var/www/vhosts/demo.joker-courses.com/httpdocs/local/modules/JokerKit/EventListeners/SendAdminEmail.php:14
Stack trace:
#0 /var/www/vhosts/demo.joker-courses.com/httpdocs/cache/dev/CoreDevDebugProjectContainer.php(966): JokerKit\EventListeners\SendAdminEmail->__construct(Object(Thelia\Mailer\MailerFactory))
#1 /var/www/vhosts/demo.joker-courses.com/httpdocs/core/vendor/symfony/dependency-injection/Container.php(312): CoreDevDebugProjectContainer->getJokerkit_Document_Expire_ListenerService()
#2 /var/www/vhosts/demo.joker-courses.com/httpdocs/core/vendor/symfony/event-dispatcher/ContainerAwareEventDispatcher.php(183): Symfony\Component\Depe in /var/www/vhosts/demo.joker-courses.com/httpdocs/local/modules/JokerKit/EventListeners/SendAdminEmail.php on line 14
Une aide serait fort généreuse.