-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reset last run state when enabling email sending (#96)
* Refactored last run setting and checking into a service * Reset last run state when enabling email sending #94
- Loading branch information
1 parent
8279993
commit a57c30f
Showing
8 changed files
with
244 additions
and
17 deletions.
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
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
74 changes: 74 additions & 0 deletions
74
modules/localgov_workflows_notifications/src/NotificationTimer.php
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,74 @@ | ||
<?php | ||
|
||
namespace Drupal\localgov_workflows_notifications; | ||
|
||
use Drupal\Component\Datetime\TimeInterface; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Config\ImmutableConfig; | ||
use Drupal\Core\State\StateInterface; | ||
|
||
/** | ||
* Handle timing when to send notifications. | ||
*/ | ||
class NotificationTimer implements NotificationTimerInterface { | ||
|
||
/** | ||
* Module settings. | ||
* | ||
* @var \Drupal\Core\Config\ImmutableConfig | ||
*/ | ||
protected ImmutableConfig $settings; | ||
|
||
/** | ||
* The state service. | ||
* | ||
* @var \Drupal\Core\State\StateInterface | ||
*/ | ||
protected StateInterface $state; | ||
|
||
/** | ||
* The time service. | ||
* | ||
* @var \Drupal\Component\Datetime\TimeInterface | ||
*/ | ||
protected TimeInterface $time; | ||
|
||
/** | ||
* Constructs a NotificationTimer object. | ||
*/ | ||
public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, TimeInterface $time) { | ||
$this->settings = $config_factory->get('localgov_workflows_notifications.settings'); | ||
$this->state = $state; | ||
$this->time = $time; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getLastRun(): ?int { | ||
return $this->state->get(self::LAST_RUN); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function update(): void { | ||
$request_time = $this->time->getRequestTime(); | ||
$this->state->set(self::LAST_RUN, $request_time); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function trigger(): bool { | ||
$request_time = $this->time->getRequestTime(); | ||
$last_run = $this->state->get(self::LAST_RUN, $request_time); | ||
$next_run = $last_run + 86400 * $this->settings->get('email_frequency'); | ||
if ($request_time >= $next_run) { | ||
return TRUE; | ||
} | ||
|
||
return FALSE; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
modules/localgov_workflows_notifications/src/NotificationTimerInterface.php
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,35 @@ | ||
<?php | ||
|
||
namespace Drupal\localgov_workflows_notifications; | ||
|
||
/** | ||
* Interface for handling notification timing. | ||
*/ | ||
interface NotificationTimerInterface { | ||
|
||
/** | ||
* Last notification state variable name. | ||
* | ||
* @var string | ||
*/ | ||
const LAST_RUN = 'localgov_workflows_notifications.last_email_run'; | ||
|
||
/** | ||
* Get the last time notifications were triggered. | ||
* | ||
* @return int|null | ||
* Timestamp of last notification. | ||
*/ | ||
public function getLastRun(): ?int; | ||
|
||
/** | ||
* Update the notification timer. | ||
*/ | ||
public function update(): void; | ||
|
||
/** | ||
* Is it time to trigger notification sending? | ||
*/ | ||
public function trigger(): bool; | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...ocalgov_workflows_notifications/tests/src/Functional/WorkflowNotificationSettingsTest.php
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,72 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\localgov_workflows_notifications\Functional; | ||
|
||
use Drupal\Core\State\StateInterface; | ||
use Drupal\localgov_workflows_notifications\NotificationTimerInterface; | ||
use Drupal\Tests\BrowserTestBase; | ||
|
||
/** | ||
* Test settings form. | ||
*/ | ||
class WorkflowNotificationSettingsTest extends BrowserTestBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $profile = 'testing'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $defaultTheme = 'stark'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'localgov_workflows_notifications', | ||
]; | ||
|
||
/** | ||
* The state service. | ||
* | ||
* @var \Drupal\Core\State\StateInterface | ||
*/ | ||
protected StateInterface $state; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->drupalLogin($this->rootUser); | ||
} | ||
|
||
/** | ||
* Test the Localgov Workflows Notifications settings form. | ||
*/ | ||
public function testSettingsForm() { | ||
$last_run = \Drupal::state()->get(NotificationTimerInterface::LAST_RUN); | ||
|
||
// Test settings get saved. | ||
$this->drupalGet('/admin/config/workflow/localgov-workflows-notifications'); | ||
$this->submitForm([ | ||
'email_enabled' => FALSE, | ||
'email_frequency' => 2, | ||
], 'Save configuration'); | ||
$settings = \Drupal::config('localgov_workflows_notifications.settings'); | ||
$this->assertFalse($settings->get('email_enabled')); | ||
$this->assertEquals(2, $settings->get('email_frequency')); | ||
|
||
// Test notification timer gets reset when email notifications are enabled. | ||
$this->drupalGet('/admin/config/workflow/localgov-workflows-notifications'); | ||
$this->submitForm([ | ||
'email_enabled' => TRUE, | ||
], 'Save configuration'); | ||
$new_last_run = \Drupal::state()->get(NotificationTimerInterface::LAST_RUN); | ||
$this->assertGreaterThan($last_run, $new_last_run); | ||
} | ||
|
||
} |
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