AddBidModifiers.php
5.29 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\BidModifier;
use App\Models\Pivots\GoalBidModifier;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class AddBidModifiers extends DirectRequest
{
protected $max_count = 1000;
protected $timestamp;
/* @var Collection|\stdClass[] */
protected $goalBidModifiers;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['BidModifiers']);
}
public function slice($maxObjects): ?APIRequest
{
$splinter = $this->sliceByKey($maxObjects, 'BidModifiers');
$splinter->putParams([
'goalBidModifiers' => $this->goalBidModifiers->slice($maxObjects)->values(),
]);
$this->putParams([
'goalBidModifiers' => $this->goalBidModifiers->slice(0, $maxObjects),
]);
return $splinter;
}
public function handle($response)
{
try {
if (!isset($response['result']['AddResults'])) {
return;
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
/* @var $goalBidModifier \stdClass */
$goalBidModifier = $this->goalBidModifiers->get($key);
$ids = json_decode($goalBidModifier->ids);
if (!isset($add_result['Ids'])) {
Log::debug("AddBidModifiers, empty Ids");
Log::debug($add_result);
Log::debug($this->getParams()['BidModifiers'][$key]);
GoalBidModifier::whereIn('id', $ids)
->update([
'reserve_create_at' => null,
]);
continue;
}
foreach ($add_result['Ids'] as $id_key => $external_id) {
$external_id = (string)$external_id;
$id = $ids[$id_key];
GoalBidModifier::where('id', $id)
->update([
'external_id' => $external_id,
'external_upload_at' => Carbon::now(),
'reserve_create_at' => null,
]);
}
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
GoalBidModifier::whereIn('id', $this->goalBidModifiers->pluck('ids')->map(function ($ids) {
return json_decode($ids);
})->collapse()->toArray())
->update([
'reserve_create_at' => null,
]);
}
public function putParams($params)
{
$this->goalBidModifiers = $params['goalBidModifiers'];
}
private function requestPrepare($params)
{
$this->setService('BidModifiers');
$this->setMethod('add');
$this->putParams($params);
$this->setParams([
'BidModifiers' => $this->goalBidModifiers->map(function ($goalBidModifier) {
/* @var $goalBidModifier \stdClass */
$data = [];
if ($goalBidModifier->goal_ad_group_external_id) {
$data['AdGroupId'] = $goalBidModifier->goal_ad_group_external_id;
} else {
$data['CampaignId'] = $goalBidModifier->dictionary_campaign_external_id;
}
if ($mobile_adjustment = @json_decode($goalBidModifier->mobile_adjustment, true)) {
$data['MobileAdjustment'] = $mobile_adjustment;
} elseif ($desktop_adjustment = @json_decode($goalBidModifier->desktop_adjustment, true)) {
$data['DesktopAdjustment'] = $desktop_adjustment;
} elseif ($demographics_adjustments = array_map(function ($demographics_adjustment) {
unset($demographics_adjustment['Enabled']);
if (is_null($demographics_adjustment['Gender'])) {
unset($demographics_adjustment['Gender']);
}
if (is_null($demographics_adjustment['Age'])) {
unset($demographics_adjustment['Age']);
}
return $demographics_adjustment;
}, array_filter(@json_decode($goalBidModifier->demographics_adjustments, true)))) {
$data['DemographicsAdjustments'] = $demographics_adjustments;
} elseif ($retargeting_adjustments = array_map(function ($retargeting_adjustment) {
unset($retargeting_adjustment['Accessible']);
unset($retargeting_adjustment['Enabled']);
return $retargeting_adjustment;
}, array_filter(@json_decode($goalBidModifier->retargeting_adjustments, true)))) {
$data['RetargetingAdjustments'] = $retargeting_adjustments;
}
return $data;
})->toArray(),
]);
}
}