Skip to content

Commit

Permalink
Add MAILTO configuration to receive cron job output (#10)
Browse files Browse the repository at this point in the history
* Add emailto to the extension

* Add emailto to the global configuration

* Test configuration

* Add EMAILTO instruction to the cron template

* Update dump file test

* Update documentation

* Rename emailto to mailto

* getMailto can return null
  • Loading branch information
flohw authored May 12, 2023
1 parent 1349e14 commit bf50ce3
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cron/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function getCrons(): array
return $this->crons;
}

public function getMailto(): ?string
{
return $this->globalConfig['mailto'];
}

public function getUser(): string
{
return $this->globalConfig['user'];
Expand Down Expand Up @@ -94,6 +99,7 @@ private function init(): void
'php_version' => $this->cronConfig['php_version'],
'env' => $this->env,
'output_path' => $this->cronConfig['output_path'],
'mailto' => $this->cronConfig['mailto'],
];
}

Expand Down
1 change: 1 addition & 0 deletions Cron/DumpFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private function render(): string
'absolute_path' => $this->configuration->getAbsolutePath(),
'php_version' => $this->configuration->getPhpVersion(),
'env' => $this->env,
'mailto' => $this->configuration->getMailto(),
]);
}
}
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->scalarNode('mailto')->defaultNull()->end()
->arrayNode('env_available')
->requiresAtLeastOneElement()
->prototype('scalar')
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Configure your crons per env in your `config/packages/lexik_cron_file_generator.

``` yaml
lexik_cron_file_generator:
mailto: ~ # Send your command output to the list of emails
env_available: # declare your available environements
- staging # example: staging and prod
- prod
Expand Down
3 changes: 3 additions & 0 deletions Resources/views/template.txt.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{% if mailto %}
MAILTO="{{ mailto }}"
{% endif %}
{% for cron in crons %}
# {{ cron.name }}

Expand Down
2 changes: 2 additions & 0 deletions Tests/Cron/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testInit()
$this->assertEquals('php7.3', $configuration->getPhpVersion());
$this->assertEquals('staging', $configuration->getEnv());
$this->assertEquals('path/to/cron_file', $configuration->getOutpath());
$this->assertSame('noreply@domail.tld', $configuration->getMailto());

/** @var Cron $cron */
foreach ($configuration->getCrons() as $cron) {
Expand Down Expand Up @@ -121,6 +122,7 @@ public function testWithBadCronEnv()
private function getFullConfig()
{
return [
'mailto' => 'noreply@domail.tld',
'env_available' => [
'staging', 'prod',
],
Expand Down
2 changes: 2 additions & 0 deletions Tests/Cron/DumpFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testDumpFile()
$templating->expects($this->any())->method('render')->willReturn('content');

$configuration = new Configuration([
'mailto' => 'noreply@email.tld',
'env_available' => [
'staging', 'prod',
],
Expand Down Expand Up @@ -71,6 +72,7 @@ public function testEmptyCron()
$templating->expects($this->never())->method('render');

$configuration = new Configuration([
'mailto' => null,
'env_available' => [
'staging',
],
Expand Down

0 comments on commit bf50ce3

Please sign in to comment.