-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebController.php
196 lines (184 loc) · 6.64 KB
/
WebController.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace common\components\mailing;
use Exception;
use Yii;
use yii\queue\ExecEvent;
use yii\web\Controller;
use yii\web\Response;
/**
* Class WebController
* @package common\components\mailing
*/
abstract class WebController extends Controller
{
/** @var Mailing */
public $mailing;
/**
* @return string
*/
public function actionMailing()
{
$queue = $this->mailing->getQueue();
$result = [];
$attempts = $queue->attempts;
$queue->on(Queue::EVENT_BEFORE_EXEC, function (ExecEvent $event) use (&$result) {
$time = time();
if ($event->job instanceof Mailing) {
$target = $event->job->getTarget();
$id = $target['email'];
} else {
$id = $event->id;
}
$result[] = "[{$time}]\tВыполняется {$id}. Попытка {$event->attempt}.";
});
$queue->on(Queue::EVENT_AFTER_EXEC, function (ExecEvent $event) use (&$result) {
$time = time();
if ($event->job instanceof Mailing) {
$target = $event->job->getTarget();
$id = $target['email'];
} else {
$id = $event->id;
}
$result[] = "[{$time}]\tУспешно завершено {$id}.";
});
$queue->on(Queue::EVENT_AFTER_ERROR, function (ExecEvent $event) use (&$result, $attempts) {
$time = time();
if ($event->job instanceof Mailing) {
$target = $event->job->getTarget();
$id = $target['email'];
} else {
$id = $event->id;
}
if ($event->attempt < $attempts) {
$result[] = "[{$time}]\tОшибка выполнения {$id}. Отложено на {$event->ttr} секунд.";
} else {
$result[] = "[{$time}]\tОшибка выполнения {$id}. Попытки исчерпаны.";
}
});
$queue->run(false);
if (empty($result)) {
$time = time();
$result[] = "[{$time}]\tЕще нечего выполнять.";
}
return implode("\n", array_reverse($result)) . "\n";
}
/**
* @return Response
* @throws Exception
*/
public function actionDelivery()
{
$request = Yii::$app->request;
$delivery = $request->post('delivery', null);
$statuses = $request->post('statuses', []);
$action = $request->post('action', 'update');
$readonly = $request->post('readonly', 'false') !== 'false';
if ($delivery !== null) {
$delivery = $this->mailing->getDelivery($delivery);
$name = $delivery->getName();
$alert = '';
switch ($action) {
case 'start':
$alert = $delivery->start() ? '' : "Невозможно запустить рассылку {$name}";
break;
case 'resume':
$alert = $delivery->resume() ? '' : "Невозможно возобновить рассылку {$name}";
break;
case 'stop':
$alert = $delivery->stop() ? '' : "Невозможно остановить рассылку {$name}";
break;
case 'pause':
$alert = $delivery->pause() ? '' : "Невозможно приостановить рассылку {$name}";
break;
}
}
return $this->responseAjax([
'alert' => empty($alert) ? false : $alert,
'content' => $delivery === null ? '' : DeliveryWidget::widget([
'model' => $delivery->model,
'readonly' => $readonly,
'statuses' => $statuses,
]),
]);
}
/**
* @return Response
*/
public function actionCombinator()
{
$request = Yii::$app->request;
$state = $request->post('state', null);
$query = $request->post('query', '');
$tags = $request->post('tags', []);
$limit = (int) $request->post('limit', Delivery::LIMIT);
return $state === null ? $this->responseSearch($query, $tags, $limit) : $this->responseSerialize($state);
}
public function actionList()
{
$request = Yii::$app->request;
$search = $request->post('search', '');
$selected = array_filter(array_map(function ($item) {
return is_numeric($item) ? (int) $item : null;
}, (array) $request->post('selected', [])));
$list = $request->post('list', '');
switch ($list) {
case 'news': return $this->responseAjax($this->findNews($search, $selected));
default: return $this->responseAjax([]);
}
}
/**
* @param string $search
* @param int[] $selected
* @return mixed
*/
abstract public function findNews($search, $selected);
/**
* @param string $query
* @param array $tags
* @param int $limit
* @return Response
*/
protected function responseSearch($query, $tags, $limit)
{
$selectTags = array_filter($this->mailing->getTags(), function ($tag) use ($tags) {
return in_array($tag, $tags, true);
});
$selectEmails = array_filter($tags, function ($tag) use ($selectTags) {
return !in_array($tag, $selectTags, true);
});
$emails = $this->mailing->searchEmails($query, array_keys($selectTags), $selectEmails, $limit);
if (count($emails) === 0) {
$emails = $this->mailing->searchEmails($query, [], $selectEmails, $limit);
}
return $this->responseAjax($emails);
}
/**
* @param array $state
* @return Response
*/
protected function responseSerialize($state)
{
$selectTags = array_filter($this->mailing->getTags(), function ($tag) use ($state) {
return in_array($tag, $state, true);
});
$selectEmails = array_filter($state, function ($tag) use ($selectTags) {
return !in_array($tag, $selectTags, true);
});
$combiner = new Combinator([
'tags' => array_keys($selectTags),
'emails' => $selectEmails
]);
return $this->responseAjax(['combiner' => $this->mailing->getQueue()->serializer->serialize($combiner)]);
}
/**
* @param mixed $data
* @return Response
*/
protected function responseAjax($data)
{
$response = Yii::$app->response;
$response->format = Response::FORMAT_JSON;
$response->data = $data;
return $response;
}
}