InactiveUsersEmailNotification.php
7 months ago
InactiveUsersNotificationProvider.php
7 months ago
UserEmailNotification.php
7 months ago
UserNotification.php
7 months ago
UserNotificationInterface.php
7 months ago
UserNotificationProvider.php
7 months ago
UserNotificationProviderInterface.php
4 months ago
UserNotifierTask.php
7 months ago
UserNotifierTask.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Plugins\UsersManager\UserNotifications; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | use Piwik\Date; |
| 14 | use Piwik\Log\LoggerInterface; |
| 15 | use Piwik\Option; |
| 16 | use Piwik\Plugin\Manager as PluginManager; |
| 17 | use Piwik\Scheduler\Schedule\Monthly; |
| 18 | use Piwik\Scheduler\Task; |
| 19 | /** |
| 20 | * Send user notifications for each provider |
| 21 | */ |
| 22 | class UserNotifierTask extends Task |
| 23 | { |
| 24 | public const LAST_RUN_TIME_OPTION_NAME = 'UserNotifier.lastRunTime'; |
| 25 | public function __construct() |
| 26 | { |
| 27 | $monthlyOnFirst = new Monthly(); |
| 28 | $monthlyOnFirst->setDay(1); |
| 29 | parent::__construct($this, 'dispatchNotifications', null, $monthlyOnFirst); |
| 30 | } |
| 31 | /** |
| 32 | * Get a list of providers (class names) that may provide user notifications to be dispatched |
| 33 | * |
| 34 | * @return array |
| 35 | */ |
| 36 | private function getUserNotificationProviderClasses() : array |
| 37 | { |
| 38 | return PluginManager::getInstance()->findMultipleComponents('UserNotifications', \Piwik\Plugins\UsersManager\UserNotifications\UserNotificationProviderInterface::class); |
| 39 | } |
| 40 | /** |
| 41 | * Dispatch notifications for each provider and its users |
| 42 | */ |
| 43 | public function dispatchNotifications() |
| 44 | { |
| 45 | $container = StaticContainer::getContainer(); |
| 46 | $logger = $container->get(LoggerInterface::class); |
| 47 | try { |
| 48 | Option::set(self::LAST_RUN_TIME_OPTION_NAME, (string) Date::factory('today')->getTimestamp()); |
| 49 | $notificationsToDispatchCount = 0; |
| 50 | $notificationsDispatchedCount = 0; |
| 51 | foreach ($this->getUserNotificationProviderClasses() as $providerClass) { |
| 52 | /** @var UserNotificationProviderInterface $provider */ |
| 53 | $provider = $container->get($providerClass); |
| 54 | $providerUserNotificationsForDispatch = $provider->getUserNotificationsForDispatch(); |
| 55 | $notificationsToDispatchCount += count($providerUserNotificationsForDispatch); |
| 56 | foreach ($providerUserNotificationsForDispatch as $userNotification) { |
| 57 | $dispatched = $userNotification->dispatch(); |
| 58 | if ($dispatched) { |
| 59 | $provider->setUserNotificationDispatched($userNotification->getUsers()); |
| 60 | $notificationsDispatchedCount++; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | if ($notificationsToDispatchCount) { |
| 65 | $logger->info("Number of user notifications dispatched: {number} of {total}.", ['number' => $notificationsDispatchedCount, 'total' => $notificationsToDispatchCount]); |
| 66 | } else { |
| 67 | $logger->info("No user notifications to dispatch, task rescheduled."); |
| 68 | } |
| 69 | } catch (Exception $ex) { |
| 70 | $container->get(LoggerInterface::class)->error($ex); |
| 71 | throw $ex; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |