CheckCampaignsChange.php
2.22 KB
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
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
class CheckCampaignsChange extends DirectRequest
{
CONST SELF = 'SELF';
CONST CHILDREN = 'CHILDREN';
CONST STAT = 'STAT';
protected $next;
function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
function handle($response)
{
foreach ($response['result']['Campaigns'] as $campaign_data) {
if (in_array(self::SELF, $campaign_data['ChangesIn']) || in_array(self::CHILDREN, $campaign_data['ChangesIn'])) {
$data = [];
if (in_array(self::SELF, $campaign_data['ChangesIn'])) {
$data['updated_self'] = Carbon::now();
}
if (in_array(self::CHILDREN, $campaign_data['ChangesIn'])) {
$data['updated_children'] = Carbon::now();
}
$external_id = $campaign_data['CampaignId'];
if ($this->getToken()->isMain()) {
$data['token'] = $this->getToken()->getKey();
Campaigns::firstOrCreate([
'external_id' => $external_id
], $data);
} else {
$dictionaryCampaign = DictionaryCampaign::synchronized()->find($external_id);
if ($dictionaryCampaign) {
$data['external_updated_at'] = Carbon::now();
$dictionaryCampaign->update($data);
}
}
}
}
$this->getToken()->update([
'timestamp' => $response['result']['Timestamp'],
]);
$this->setToken($this->getToken()->refresh());
}
private function requestPrepare($params)
{
$this->setService('changes');
$this->setMethod('checkDictionaries');
$this->setParams([
"Timestamp" => $this->getToken()->timestamp
]);
}
}