Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony Mailer Support #219

Merged
merged 1 commit into from Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Expand Up @@ -35,14 +35,23 @@ jobs:
# Test against latest Symfony 4.3 stable
- php: 7.3
env: SYMFONY_REQUIRE="4.3.*"
install:
- composer require --dev symfony/mailer --no-update
- travis_retry composer update -n --prefer-dist --prefer-stable

# Test against latest Symfony 4.4 stable
- php: 7.3
env: SYMFONY_REQUIRE="4.4.*"
install:
- composer require --dev symfony/mailer --no-update
- travis_retry composer update -n --prefer-dist --prefer-stable

# Test against latest Symfony 5.0 stable
- php: 7.3
env: SYMFONY_REQUIRE="5.0.*"
install:
- composer require --dev symfony/mailer --no-update
- travis_retry composer update -n --prefer-dist --prefer-stable

notifications:
slack: liip:3QOs1QKt3aCFxpJvRzpJCbVZ
32 changes: 25 additions & 7 deletions DependencyInjection/LiipMonitorExtension.php
Expand Up @@ -9,14 +9,18 @@
use Doctrine\Migrations\MigrationException;
use Liip\MonitorBundle\DoctrineMigrations\Configuration as LiipMigrationConfiguration;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;

class LiipMonitorExtension extends Extension implements CompilerPassInterface
{
Expand Down Expand Up @@ -67,13 +71,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('controller.xml');
}

if ($config['mailer']['enabled']) {
$loader->load('swift_mailer.xml');

foreach ($config['mailer'] as $key => $value) {
$container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
}
}
$this->configureMailer($container, $loader, $config);

$container->setParameter(sprintf('%s.default_group', $this->getAlias()), $config['default_group']);

Expand Down Expand Up @@ -255,6 +253,26 @@ private function configureDoctrineMigrationsCheck(ContainerBuilder $container, a
}
}

private function configureMailer(ContainerBuilder $container, LoaderInterface $loader, array $config)
{
if (false === $config['mailer']['enabled']) {
return;
}

try {
$mailerDefinition = $container->findDefinition('mailer');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Baachi , @kbond

I am unable to make the bundle work when I enable the mailer configuration.

I am using Symfony 4.4 (symfony/mailer 4.4, symfony/mailgun-mailer 4.4), LiipMonitorBundle 2.11.1

Regarding the Symfony documentation

In the load() method, all services and parameters related to this extension will be loaded. This method doesn't get the actual container instance, but a copy. This container only has the parameters from the actual container. After loading the services and parameters, the copy will be merged into the actual container, to ensure all services and parameters are also added to the actual container.

The container copy won't never have any mailer definition, so the exception is always raise.

Does it need a CompilerPass to load the mailer configuration instead?

Thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @ixarlie, it looks like we should just be setting the config parameters in the extension and like you suggest, have a compiler pass to create the service.

@Baachi, are you able to take a look?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @kbond , @Baachi finally I've opened a new pull request addressing this issue. #227

} catch (ServiceNotFoundException $e) {
throw new \InvalidArgumentException('To enable mail reporting you have to install the "swiftmailer/swiftmailer" or "symfony/mailer".');
}

$filename = \Swift_Mailer::class !== $mailerDefinition->getClass() ? 'symfony_mailer.xml' : 'swift_mailer.xml';
$loader->load($filename);

foreach ($config['mailer'] as $key => $value) {
$container->setParameter(sprintf('%s.mailer.%s', $this->getAlias(), $key), $value);
}
}

/**
* Return key-value array with migration version as key and class as a value defined in config file
*
Expand Down
97 changes: 97 additions & 0 deletions Helper/AbstractMailReporter.php
@@ -0,0 +1,97 @@
<?php

namespace Liip\MonitorBundle\Helper;

use ArrayObject;
use ZendDiagnostics\Check\CheckInterface;
use ZendDiagnostics\Result\Collection as ResultsCollection;
use ZendDiagnostics\Result\ResultInterface;
use ZendDiagnostics\Runner\Reporter\ReporterInterface;

abstract class AbstractMailReporter implements ReporterInterface
{
/**
* @var array|string
*/
protected $recipients;
/**
* @var string
*/
protected $subject;
/**
* @var string
*/
protected $sender;
/**
* @var bool
*/
protected $sendOnWarning;

/**
* @param string|array $recipients
* @param string $sender
* @param string $subject
* @param bool $sendOnWarning
*/
public function __construct($recipients, $sender, $subject, $sendOnWarning = true)
{
$this->recipients = $recipients;
$this->sender = $sender;
$this->subject = $subject;
$this->sendOnWarning = $sendOnWarning;
}

/**
* {@inheritdoc}
*/
public function onStart(ArrayObject $checks, $runnerConfig)
{
}

/**
* {@inheritdoc}
*/
public function onBeforeRun(CheckInterface $check, $checkAlias = null)
{
}

/**
* {@inheritdoc}
*/
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null)
{
}

/**
* {@inheritdoc}
*/
public function onStop(ResultsCollection $results)
{
}

/**
* {@inheritdoc}
*/
public function onFinish(ResultsCollection $results)
{
if ($results->getUnknownCount() > 0) {
$this->sendEmail($results);

return;
}

if ($results->getWarningCount() > 0 && $this->sendOnWarning) {
$this->sendEmail($results);

return;
}

if ($results->getFailureCount() > 0) {
$this->sendEmail($results);

return;
}
}

abstract protected function sendEmail(ResultsCollection $results);
}
67 changes: 4 additions & 63 deletions Helper/SwiftMailerReporter.php
Expand Up @@ -8,18 +8,13 @@
use ZendDiagnostics\Check\CheckInterface;
use ZendDiagnostics\Result\Collection as ResultsCollection;
use ZendDiagnostics\Result\ResultInterface;
use ZendDiagnostics\Runner\Reporter\ReporterInterface;

/**
* @author louis <louis@systemli.org>
*/
class SwiftMailerReporter implements ReporterInterface
class SwiftMailerReporter extends AbstractMailReporter
{
private $mailer;
private $recipients;
private $subject;
private $sender;
private $sendOnWarning;

/**
* @param Swift_Mailer $mailer
Expand All @@ -31,72 +26,18 @@ class SwiftMailerReporter implements ReporterInterface
public function __construct(Swift_Mailer $mailer, $recipients, $sender, $subject, $sendOnWarning = true)
{
$this->mailer = $mailer;
$this->recipients = $recipients;
$this->sender = $sender;
$this->subject = $subject;
$this->sendOnWarning = $sendOnWarning;
}

/**
* {@inheritdoc}
*/
public function onStart(ArrayObject $checks, $runnerConfig)
{
}

/**
* {@inheritdoc}
*/
public function onBeforeRun(CheckInterface $check, $checkAlias = null)
{
}

/**
* {@inheritdoc}
*/
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null)
{
}

/**
* {@inheritdoc}
*/
public function onStop(ResultsCollection $results)
{
}

/**
* {@inheritdoc}
*/
public function onFinish(ResultsCollection $results)
{
if ($results->getUnknownCount() > 0) {
$this->sendEmail($results);

return;
}

if ($results->getWarningCount() > 0 && $this->sendOnWarning) {
$this->sendEmail($results);

return;
}

if ($results->getFailureCount() > 0) {
$this->sendEmail($results);

return;
}
parent::__construct($recipients, $sender, $subject, $sendOnWarning);
}

private function sendEmail(ResultsCollection $results)
protected function sendEmail(ResultsCollection $results)
{
$body = '';

foreach ($results as $check) {
/* @var $check CheckInterface */
/* @var $result ResultInterface */
$result = isset($results[$check]) ? $results[$check] : null;
$result = $results[$check] ?? null;

if ($result instanceof ResultInterface) {
$body .= sprintf("Check: %s\n", $check->getLabel());
Expand Down
48 changes: 48 additions & 0 deletions Helper/SymfonyMailerReporter.php
@@ -0,0 +1,48 @@
<?php

namespace Liip\MonitorBundle\Helper;

use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use ZendDiagnostics\Check\CheckInterface;
use ZendDiagnostics\Result\Collection as ResultsCollection;
use ZendDiagnostics\Result\ResultInterface;

class SymfonyMailerReporter extends AbstractMailReporter
{
/**
* @var MailerInterface
*/
private $mailer;

public function __construct(MailerInterface $mailer, array $recipients, string $sender, string $subject, bool $sendOnWarning = true)
{
$this->mailer = $mailer;

parent::__construct($recipients, $sender, $subject, $sendOnWarning);
}

protected function sendEmail(ResultsCollection $results): void
{
$body = '';

foreach ($results as $check) {
/* @var $check CheckInterface */
/* @var $result ResultInterface */
$result = $results[$check] ?? null;

if ($result instanceof ResultInterface) {
$body .= sprintf('[%s] %s', $check->getLabel(), $result->getMessage());
}
}


$message = (new Email())
->subject($this->subject)
->from($this->sender)
->to(...$this->recipients)
->text($body);

$this->mailer->send($message);
}
}
17 changes: 17 additions & 0 deletions Resources/config/symfony_mailer.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="liip_monitor.reporter.symfony_mailer" class="Liip\MonitorBundle\Helper\SymfonyMailerReporter">
<tag name="liip_monitor.additional_reporter" alias="symfony_mailer" />
<argument type="service" id="mailer" />
<argument>%liip_monitor.mailer.recipient%</argument>
<argument>%liip_monitor.mailer.sender%</argument>
<argument>%liip_monitor.mailer.subject%</argument>
<argument>%liip_monitor.mailer.send_on_warning%</argument>
</service>
</services>
</container>