Interfaces
1 year ago
Transporter
1 year ago
Notifications.php
1 year ago
NotificationsProvider.php
1 year ago
Notifications.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Notifications; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Notifications\NotificationsProvider; |
| 7 | |
| 8 | class Notifications |
| 9 | { |
| 10 | /** |
| 11 | * @var string |
| 12 | */ |
| 13 | const OPTION_BACKUP_SCHEDULE_REPORT_EMAIL = 'wpstg_backup_schedules_report_email'; |
| 14 | |
| 15 | /** |
| 16 | * @var bool |
| 17 | */ |
| 18 | const DISABLE_FOOTER_MESSAGE = false; |
| 19 | |
| 20 | /** |
| 21 | * @var bool |
| 22 | */ |
| 23 | const ENABLE_FOOTER_MESSAGE = true; |
| 24 | |
| 25 | /** |
| 26 | * @var object |
| 27 | */ |
| 28 | private $transporter; |
| 29 | |
| 30 | /** |
| 31 | * @var NotificationsProvider |
| 32 | */ |
| 33 | private $notificationProvider; |
| 34 | |
| 35 | /** |
| 36 | * @param NotificationsProvider $notificationProvider |
| 37 | */ |
| 38 | public function __construct(NotificationsProvider $notificationProvider) |
| 39 | { |
| 40 | $this->notificationProvider = $notificationProvider; |
| 41 | $this->getTransporter(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return void |
| 46 | */ |
| 47 | private function getTransporter() |
| 48 | { |
| 49 | $providers = $this->notificationProvider->getProviders(); |
| 50 | |
| 51 | $this->transporter = new \stdClass(); |
| 52 | foreach ($providers as $provider) { |
| 53 | $providerName = lcfirst(basename(str_replace('\\', '/', $provider))); |
| 54 | $this->transporter->{$providerName} = WPStaging::make($provider); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param string $to Recipient |
| 60 | * @param string $subject Subject |
| 61 | * @param string $message Content |
| 62 | * @param string $from (Optional) Sender |
| 63 | * @param array $attachments (Optional) Attachments |
| 64 | * @param bool $isAddFooterMessage (Optional) Enable footer message |
| 65 | * @return bool |
| 66 | */ |
| 67 | public function sendEmail(string $to, string $subject, string $message, string $from = '', array $attachments = [], bool $isAddFooterMessage = self::ENABLE_FOOTER_MESSAGE): bool |
| 68 | { |
| 69 | if (empty($this->transporter->emailNotification) || !is_object($this->transporter->emailNotification)) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | $this->transporter->emailNotification->setSender($from) |
| 74 | ->setRecipient($to) |
| 75 | ->setSubject($subject) |
| 76 | ->setAttachment($attachments) |
| 77 | ->setIsAddFooterMessage($isAddFooterMessage); |
| 78 | |
| 79 | return $this->transporter->emailNotification->send($message); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param string $webhook Slack Webhook |
| 84 | * @param string $title title |
| 85 | * @param string $message Content |
| 86 | * @param bool $isAddFooterMessage (Optional) Enable footer message |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function sendSlack(string $webhook, string $title, string $message, bool $isAddFooterMessage = self::ENABLE_FOOTER_MESSAGE): bool |
| 90 | { |
| 91 | if (empty($this->transporter->slackNotification) || !is_object($this->transporter->slackNotification)) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | $this->transporter->slackNotification->setWebhook($webhook) |
| 96 | ->setTitle($title) |
| 97 | ->setIsAddFooterMessage($isAddFooterMessage); |
| 98 | |
| 99 | return $this->transporter->slackNotification->send($message); |
| 100 | } |
| 101 | } |
| 102 |