UpdateCampaigns.php
6.18 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
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
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Campaigns;
use App\Models\Pivots\DictionaryCampaign;
use App\Models\Variable;
use App\Service\Requests\DirectRequest;
use App\Service\StrReplaceByVariables;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class UpdateCampaigns extends DirectRequest
{
CONST MAX_COUNT = 10;
protected $timestamp;
/* @var Collection|DictionaryCampaign[] $dictionaryCampaigns */
protected $dictionaryCampaigns;
/* @var Collection|Variable[] $dictionaryCampaigns */
protected $variables;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
function slice($count): ?\App\Service\Contract\APIRequest
{
if ($count < 0)
return null;
//для запросов get ничего не разбиваем, т.к. заранее не знаем сколько чего будет
//для них только после выполнения запроса будет известно есть ли кроме $count еще данные
//а текущий запрос просто устанавливается в $count
//а вот для запросов мутаторов будем разбивать. Но тут уже будет зависеть от запроса
//будем под каждый реализовывать делитель
if (!isset($this->params['Page'])){
$this->params['Page'] = [];
}
$this->params['Page']['Limit'] = $count;
return null;
}
public function handle($response)
{
try {
foreach ($response['result']['UpdateResults'] as $key => $add_result) {
$external_id = $add_result['Id'] ?? '';
if (!$external_id) {
Log::debug("AddCampaigns, empty Id");
Log::debug($add_result);
continue;
}
DictionaryCampaign::forExternal()->needUpdated()
->where('external_id', $external_id)
->update([
'updated_need' => null,
]);
}
} catch (\Exception $e) {
Log::debug($e);
}
}
private function requestPrepare($params)
{
$this->setService('Campaigns');
$this->setMethod('update');
$this->setParams([
'Campaigns' => $params['dictionaryCampaigns']->map(function (DictionaryCampaign $dictionaryCampaign) {
$list = Variable::getListVariablesByDictionaryCampaign($dictionaryCampaign);
$data = [
'Id' => $dictionaryCampaign->external_id,
'Name' => StrReplaceByVariables::getInstance($dictionaryCampaign->name, $list)->get(),
'StartDate' => Carbon::now()->format('Y-m-d'),
'TextCampaign' => [
'BiddingStrategy' => $dictionaryCampaign->campaign->bidding_strategy,
'RelevantKeywords' => $dictionaryCampaign->campaign->relevant_keywords,
'AttributionModel' => $dictionaryCampaign->campaign->attribution_model,
],
];
if ($dictionaryCampaign->campaign->priority_goals && count($dictionaryCampaign->campaign->priority_goals)) {
$data['PriorityGoals'] = $dictionaryCampaign->campaign->priority_goals;
}
if ($dictionaryCampaign->campaign->daily_budget && count($dictionaryCampaign->campaign->daily_budget)) {
$data['DailyBudget'] = $dictionaryCampaign->campaign->daily_budget;
}
if ($dictionaryCampaign->negative_keywords && count($dictionaryCampaign->negative_keywords)) {
$data['NegativeKeywords'] = [
'Items' => array_map(function ($value) use ($list) {
return StrReplaceByVariables::getInstance($value, $list)->get();
}, $dictionaryCampaign->negative_keywords),
];
}
if ($dictionaryCampaign->campaign->blocked_ips && count($dictionaryCampaign->campaign->blocked_ips)) {
$data['BlockedIps'] = [
'Items' => $dictionaryCampaign->campaign->blocked_ips,
];
}
if ($dictionaryCampaign->excluded_sites && count($dictionaryCampaign->excluded_sites)) {
$data['ExcludedSites'] = [
'Items' => array_map(function ($value) use ($list) {
return StrReplaceByVariables::getInstance($value, $list)->get();
}, $dictionaryCampaign->excluded_sites),
];
}
if ($dictionaryCampaign->campaign->settings && count($dictionaryCampaign->campaign->settings)) {
$settingsAllow = Campaigns::getSettingOptionsAllow();
$settings = [];
foreach ($dictionaryCampaign->campaign->settings as $setting) {
if (isset($setting['Option']) && in_array($setting['Option'], $settingsAllow)) {
$settings[] = $setting;
}
}
if (count($settings)) {
$data['TextCampaign']['Settings'] = $settings;
}
}
if ($dictionaryCampaign->campaign->counter_ids && count($dictionaryCampaign->campaign->counter_ids)) {
$data['TextCampaign']['CounterIds'] = [
'Items' => $dictionaryCampaign->campaign->counter_ids,
];
}
return $data;
}),
]);
}
function getObjectsCount(){
$params = $this->getParams();
$cnt = count($params['Campaigns']);
return $cnt > self::MAX_COUNT ? self::MAX_COUNT : $cnt;
}
}