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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
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!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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