1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Batch method file. |
4
|
|
|
* |
5
|
|
|
* @package App |
6
|
|
|
* |
7
|
|
|
* @copyright YetiForce S.A. |
8
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
9
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Batch method class. |
16
|
|
|
*/ |
17
|
|
|
class BatchMethod extends Base |
18
|
|
|
{ |
19
|
|
|
/** Enebled */ |
20
|
|
|
const STATUS_ENABLED = 1; |
21
|
|
|
|
22
|
|
|
/** Running */ |
23
|
|
|
const STATUS_RUNNING = 2; |
24
|
|
|
|
25
|
|
|
/** Halted */ |
26
|
|
|
const STATUS_HALTED = 3; |
27
|
|
|
|
28
|
|
|
/** Completed */ |
29
|
|
|
const STATUS_COMPLETED = 4; |
30
|
|
|
|
31
|
|
|
/** @var array [name => type, ...] */ |
32
|
|
|
protected $allowedFields = [ |
33
|
|
|
'userid' => 'integer', |
34
|
|
|
'status' => 'integer', |
35
|
|
|
'params' => 'string', |
36
|
|
|
'method' => 'string', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** Previous status */ |
40
|
|
|
private $previousStatus; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* BatchMethod constructor. |
44
|
|
|
* |
45
|
|
|
* @param array $values |
46
|
|
|
* @param bool $encode |
47
|
|
|
* |
48
|
5770 |
|
* @throws \App\Exceptions\AppException |
49
|
|
|
*/ |
50
|
5770 |
|
public function __construct($values = [], $encode = true) |
51
|
5770 |
|
{ |
52
|
5770 |
|
$values['status'] = $values['status'] ?? static::STATUS_ENABLED; |
53
|
5770 |
|
$values['userid'] = $values['userid'] ?? User::getCurrentUserId(); |
54
|
|
|
if ($encode) { |
55
|
5770 |
|
$values['params'] = Json::encode($values['params']); |
56
|
5770 |
|
} |
57
|
5770 |
|
parent::__construct($values); |
58
|
|
|
$this->previousStatus = $values['status']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Save. |
63
|
|
|
* |
64
|
5770 |
|
* @return bool |
65
|
|
|
*/ |
66
|
5770 |
|
public function save() |
67
|
5770 |
|
{ |
68
|
|
|
$db = Db::getInstance('admin'); |
69
|
|
|
if ($this->get('id')) { |
70
|
5770 |
|
$result = $db->createCommand()->update('s_#__batchmethod', $this->getData(), ['id' => $this->get('id')])->execute(); |
71
|
5766 |
|
} else { |
72
|
|
|
if ($this->isExists()) { |
73
|
5 |
|
return false; |
74
|
5 |
|
} |
75
|
5 |
|
$this->value['created_time'] = date('Y-m-d H:i:s'); |
76
|
|
|
$result = $db->createCommand()->insert('s_#__batchmethod', $this->getData())->execute(); |
77
|
5 |
|
$this->value['id'] = $db->getLastInsertID('s_#__batchmethod_id_seq'); |
78
|
5 |
|
} |
79
|
|
|
$this->previousStatus = $this->get('status'); |
80
|
|
|
return (bool) $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Function verifies if a duplicate of the entry already exists. |
85
|
|
|
* |
86
|
5770 |
|
* @return bool |
87
|
|
|
*/ |
88
|
5770 |
|
public function isExists(): bool |
89
|
|
|
{ |
90
|
|
|
return (new Db\Query())->from('s_#__batchmethod')->where(['method' => $this->get('method'), 'params' => $this->get('params')])->exists(Db::getInstance('admin')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
1 |
|
* Execute. |
95
|
|
|
*/ |
96
|
|
|
public function execute() |
97
|
1 |
|
{ |
98
|
1 |
|
try { |
99
|
1 |
|
$this->setStatus(static::STATUS_RUNNING); |
100
|
|
|
if (\is_callable($this->get('method'))) { |
101
|
|
|
\call_user_func_array($this->get('method'), Json::decode($this->get('params'))); |
102
|
|
|
} else { |
103
|
1 |
|
throw new Exceptions\AppException("ERR_CONTENTS_VARIABLE_CANT_CALLED_FUNCTION||{$this->get('method')}", 406); |
104
|
|
|
} |
105
|
|
|
$this->setStatus(static::STATUS_COMPLETED); |
106
|
|
|
} catch (\Throwable $ex) { |
107
|
|
|
Log::error($ex->getMessage()); |
108
|
|
|
if ($this->previousStatus === static::STATUS_HALTED) { |
109
|
|
|
$this->log($ex->__toString()); |
110
|
|
|
$this->delete(); |
111
|
|
|
} else { |
112
|
|
|
$this->setStatus(static::STATUS_HALTED); |
113
|
1 |
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set status. |
119
|
|
|
* |
120
|
1 |
|
* @param int $status |
121
|
|
|
*/ |
122
|
1 |
|
public function setStatus(int $status) |
123
|
1 |
|
{ |
124
|
1 |
|
$result = Db::getInstance('admin')->createCommand()->update('s_#__batchmethod', ['status' => $status], ['id' => $this->get('id')])->execute(); |
125
|
|
|
if ($result) { |
126
|
1 |
|
$this->set('status', $status); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Function check is status completed. |
132
|
|
|
* |
133
|
1 |
|
* @return bool |
134
|
|
|
*/ |
135
|
1 |
|
public function isCompleted() |
136
|
|
|
{ |
137
|
|
|
return $this->get('status') === static::STATUS_COMPLETED; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
1 |
|
* Delete. |
142
|
|
|
*/ |
143
|
1 |
|
public function delete() |
144
|
1 |
|
{ |
145
|
|
|
Db::getInstance('admin')->createCommand()->delete('s_#__batchmethod', ['id' => $this->get('id')])->execute(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Delete entries with the same parameters. |
150
|
|
|
*/ |
151
|
|
|
public function deleteDuplicate(): bool |
152
|
|
|
{ |
153
|
|
|
return Db::getInstance('admin')->createCommand()->delete('s_#__batchmethod', ['method' => $this->get('method'), 'params' => $this->get('params')])->execute(); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Delete by method. |
158
|
|
|
* |
159
|
|
|
* @param string $method |
160
|
|
|
*/ |
161
|
|
|
public static function deleteByMethod(string $method): void |
162
|
|
|
{ |
163
|
|
|
Db::getInstance('admin')->createCommand()->delete('s_#__batchmethod', ['method' => $method])->execute(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Log. |
168
|
|
|
* |
169
|
|
|
* @param string $message |
170
|
|
|
*/ |
171
|
|
|
private function log($message) |
172
|
|
|
{ |
173
|
|
|
Db::getInstance('log')->createCommand()->insert('l_#__batchmethod', [ |
174
|
|
|
'status' => $this->get('status'), |
175
|
|
|
'date' => date('Y-m-d H:i:s'), |
176
|
|
|
'method' => $this->get('method'), |
177
|
|
|
'params' => $this->get('params'), |
178
|
|
|
'userid' => $this->get('userid'), |
179
|
|
|
'message' => $message, |
180
|
|
|
])->execute(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|