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