-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notification.php
132 lines (107 loc) · 3.35 KB
/
Notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
// for CodeIgniter4 Library
namespace App\Libraries;
use CodeIgniter\Config\BaseService;
use App\AppException;
use App\Libraries\Message\Message;
use App\Libraries\Message\SMSSender;
use App\Libraries\Message\MailSender;
use App\Libraries\Message\AlimTalkSender;
class Notification extends BaseService {
protected $sender = [];
protected $messages = [];
protected function _getSender($type)
{
if (!$type || !is_string($type))
throw InvalidArgumentException('invalid type');
if (array_key_exists($type, $this->sender))
return $this->sender[$type];
$config = config('OfficeCheckIN');
switch ($type)
{
case 'sms':
$this->sender[$type] = new SMSSender($config);
return $this->sender[$type];
case 'alimtalk':
$this->sender[$type] = new AlimTalkSender($config);
return $this->sender[$type];
case 'mail':
$this->sender[$type] = new MailSender($config);
return $this->sender[$type];
}
throw new AppException('unsupported type -> ' . $type);
}
protected function _saveJob()
{
throw new \LogicException('not supported reservation');
}
public function send(int $reservation_time = 0)
{
if ($reservation_time != 0)
{
$this->_saveJob();
return;
}
foreach ($this->messages as $msg)
{
$this->_getSender($msg->messageType)->addQueue($msg);
}
$this->messages = [];
foreach ($this->sender as $sender)
{
$sender->send();
$sender->clearQueue();
}
}
public function addSimple($data)
{
switch ($data['type'])
{
case 'sms':
$item = SMSSender::createMessage($data['message']);
break;
case 'alimtalk':
$item = AlimTalkSender::createMessage($data['message']);
break;
case 'mail':
$item = MailSender::createMessage($data['subject'], $data['message']);
break;
default:
throw new AppException('unsupported type -> ' . $data['type']);
}
$this->addMessage($item, $data['receiver']);
return $this;
}
public function addMessage(Message $msg, $to)
{
$msg->to = $to;
$this->messages[] = $msg;
}
private function _increaseLimitCount($receiver, $sendIP)
{
return TRUE;
}
private function _bindData(string $text, array $data)
{
$replaces = [];
foreach ($data as $key => $value) {
if (is_array($value))
continue;
$replaces['[['.strtoupper($key).']]'] = $value;
$replaces['#{'.strtoupper($key).'}'] = $value;
$replaces['${'.strtoupper($key).'}'] = $value;
}
return str_replace(array_keys($replaces), array_values($replaces), $text);
}
}
class NotificationException extends \Exception {
function __construct($code=self::ERROR_UNKNOWN, $message=null)
{
parent::__construct($message);
$this->code = $code;
}
const ERROR_NONE = 0;
const ERROR_UNKNOWN = 1;
const ERROR_EXCESS_SENDCOUNT = 2;
const ERROR_CANNOT_FIND_TEMPLATE = 3;
}