CheckChanges.php
2.46 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
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdGroup;
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;
use Illuminate\Support\Facades\Log;
class CheckChanges extends DirectRequest
{
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function putParams($params)
{
$this->dictionaryCampaigns = $params['dictionaryCampaigns'];
$this->variables = $params['variables'];
}
public function handle($response)
{
if (isset($response['result']['NotFound'])) {
Log::debug($response['result']['NotFound']);
}
if (isset($response['result']['Unprocessed'])) {
Log::debug($response['result']['Unprocessed']);
}
if (isset($response['result']['Modified']['AdGroupIds'])) {
foreach ($response['result']['Modified']['AdGroupIds'] as $ad_group_id) {
$adGroup = AdGroup::where('external_id', $ad_group_id)->first();
if ($adGroup) {
$adGroup->update([
'updated_self' => Carbon::now(),
]);
}
}
}
if (isset($this->getParams()['CampaignIds'])) {
if ($this->getToken()->isMain()) {
Campaigns::whereIn('external_id', $this->getParams()['CampaignIds'])->update([
'updated_children' => null,
]);
} else {
DictionaryCampaign::whereIn('external_id', $this->getParams()['CampaignIds'])->update([
'updated_children' => null,
]);
}
$this->getToken()->update([
'check_changes_ad_group' => Carbon::createFromTimestamp($response['result']['Timestamp']),
]);
} else {
$this->getToken()->update([
'check_changes' => Carbon::createFromTimestamp($response['result']['Timestamp']),
]);
}
$this->setToken($this->getToken()->refresh());
}
private function requestPrepare($params)
{
$this->setService('Changes');
$this->setMethod('check');
$this->setParams($params);
}
}